package org.jibeframework.contentmanager.controller;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.lock.LockStatus;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.NodeRef;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.lang.StringUtils;
import org.jibeframework.alfresco.node.CMContent;
import org.jibeframework.alfresco.node.CMObject;
import org.jibeframework.alfresco.service.RepositoryService;
import org.jibeframework.core.Context;
import org.jibeframework.core.annotation.Remote;
import org.jibeframework.core.app.config.ConfigurationConfig;
import org.jibeframework.core.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
@Controller("repo.Document")
public class DocumentController {
@Autowired
private ServiceRegistry serviceRegistry;
@Autowired
private RepositoryService repositoryService;
@Autowired
private ConfigService configService;
@SuppressWarnings("unchecked")
@Remote
public void openDocument(@RequestParam("version") String version, @RequestParam("nodeId") NodeRef nodeRef,
@RequestParam("forceDownload") boolean forceDownload, Context context) {
CMContent node = repositoryService.fetch(CMContent.class, nodeRef);
if (!StringUtils.equals(version, "current")) {
node = node.getVersion(version);
}
if (forceDownload) {
// response.put("url", node.getDownloadUrl());
// response.put("name", node.getName());
} else {
String name = node.getName();
String mimeType = serviceRegistry.getMimetypeService().guessMimetype(name);
HierarchicalConfiguration configuration = configService.getConfig(ConfigurationConfig.class)
.getConfiguration();
List<String> inlineMimeTypes = configuration.getList("inline-openable/type");
boolean inline = inlineMimeTypes.contains(mimeType);
String url = inline ? node.getContentUrl() : node.getDownloadUrl();
// response.put("inline", inline);
// response.put("url", url);
// response.put("name", name);
}
}
public final String getLockIcon(CMObject node, Context context) {
boolean locked = false;
if (node.hasAspect(ContentModel.ASPECT_LOCKABLE)) {
LockStatus lockStatus = serviceRegistry.getLockService().getLockStatus(node.getNodeRef());
if (lockStatus == LockStatus.LOCKED || lockStatus == LockStatus.LOCK_OWNER) {
locked = true;
}
}
if (locked) {
return context.getServletRequest().getContextPath() + "/images/icons/locked.gif";
} else {
return context.getServletRequest().getContextPath() + "/images/parts/spacer.gif";
}
}
public int getSize(CMObject node) {
if (node.isSubTypeOf(ContentModel.TYPE_FOLDER)) {
return -1;
} else {
ContentData content = (ContentData) serviceRegistry.getNodeService().getProperty(node.getNodeRef(),
ContentModel.PROP_CONTENT);
return (int) Math.round((content != null ? content.getSize() : 0L));
}
}
public String getExtension(CMObject node) {
return "*." + StringUtils.substringAfterLast(node.getName(), ".");
}
public String getMimetypeName(CMObject node) {
return serviceRegistry.getMimetypeService().getDisplaysByExtension().get(getExtension(node));
}
}