public static makePdfUrl(MapNode clientVersion, AlfrescoResolver alfrescoResolver)
{
String pdfurl = null;
if (clientVersion.hasAspect(ADPModel.ASPECT_PDFED))
{
logger.debug("Pdfed node found");
QNameNodeMap assocs = (QNameNodeMap)clientVersion.getAssociations();
List assocRefs = (List)assocs.get(ADPModel.ASSOC_PDF);
if (assocRefs != null && !assocRefs.isEmpty())
{
logger.debug("Valid pdf association found");
try
{
String pdfName = alfrescoResolver.modifyName(clientVersion.getName(), null, "pdf");
pdfurl = DownloadContentServlet.generateBrowserURL(((AssociationRef)assocRefs.get(0)).getTargetRef(), pdfName);
logger.debug("Built hi-res pdf url of " + pdfurl);
}
catch (Throwable e)
{
logger.error("Unable to build pdf url from association", e);
}
}
}
if (pdfurl == null)
{
String name = clientVersion.getName();
int pos = name.lastIndexOf(".");
if ( pos != -1 ) {
name = name.substring(0, pos);
}
name = name.replaceAll("[^-A-Za-z0-9_/\\.]", "_");
pdfurl = "/faces/" + adpServer.getViewDocServletName() + "/" + clientVersion.getId() + "/" + name + ".pdf";
}
logger.debug("PDF url is " + pdfurl);
return pdfurl;
}
here is the whole class. the object "testServer" has now actaully been renamed to "adpServer". i apoligise for the confusion i caused, im sorry.
package com.evolvedthought.alfresco.model.policies;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.transaction.Status;
import javax.transaction.UserTransaction;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.ContentServicePolicies;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.content.ContentServicePolicies.OnContentUpdatePolicy;
import org.alfresco.repo.node.NodeServicePolicies.OnAddAspectPolicy;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.service.cmr.model.FileExistsException;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.AspectMissingException;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.web.app.servlet.DownloadContentServlet;
import org.alfresco.web.bean.repository.MapNode;
import org.alfresco.web.bean.repository.Node;
import org.alfresco.web.bean.repository.QNameNodeMap;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.log4j.Logger;
import com.evolvedthought.adp.ADPException;
import com.evolvedthought.adpeditor.document.ADPDocument;
import com.evolvedthought.adpeditor.document.ADPDocumentFactory;
import com.evolvedthought.adpeditor.resolver.AlfrescoResolver;
import com.evolvedthought.adpeditor.resolver.Resolver;
import com.evolvedthought.adpeditor.server.ADPServer;
import com.evolvedthought.alfresco.model.ADPModel;
public class PdfedAspectPolicy implements OnContentUpdatePolicy, OnAddAspectPolicy
{
private static final Logger logger = Logger.getLogger(PdfedAspectPolicy.class);
/**
* The policy component
*/
private PolicyComponent policyComponent;
/**
* Set the policy component
*
* @param policyComponentthe policy component
*/
public void setPolicyComponent(PolicyComponent policyComponent)
{
this.policyComponent = policyComponent;
}
protected NodeService nodeService = null;
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
protected ContentService contentService = null;
public void setContentService(ContentService contentService)
{
this.contentService = contentService;
}
protected TransactionService transactionService = null;
public void setTransactionService(TransactionService transactionService)
{
this.transactionService = transactionService;
}
protected FileFolderService fileFolderService = null;
public void setFileFolderService(FileFolderService fileFolderService)
{
this.fileFolderService = fileFolderService;
}
protected AlfrescoResolver alfrescoResolver = null;
public void setAlfrescoResolver(AlfrescoResolver alfrescoResolver)
{
this.alfrescoResolver = alfrescoResolver;
}
protected ADPServer adpServer = null;
public void setAdpServer(ADPServer adpServer)
{
this.adpServer = adpServer;
}
public void init()
{
logger.debug("Initializing PdfedAspect");
logger.debug("registering policy behaviours");
this.policyComponent.bindClassBehaviour(ContentServicePolicies.ON_CONTENT_UPDATE, ADPModel.ASPECT_PDFED, new JavaBehaviour(this, "onContentUpdate"));
this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onAddAspect"), ADPModel.ASPECT_PDFED, new JavaBehaviour(this, "onAddAspect"));
// XXX - pdf: does this need to remove the pdf on aspect removal/node deletion?
}
public void onAddAspect(NodeRef nodeRef, QName aspectTypeQName)
{
if (aspectTypeQName.isMatch(ADPModel.ASPECT_PDFED))
{
generatePdf(nodeRef);
}
}
public void onContentUpdate(NodeRef nodeRef, boolean newContent)
{
generatePdf(nodeRef);
}
protected void generatePdf(NodeRef nodeRef)
{
if (alfrescoResolver.getIsADPDocument(nodeRef))
{
try
{
ADPDocument adpDocument = ADPDocumentFactory.getADPDocument(nodeRef.getId(), alfrescoResolver);
List<NameValuePair> options = getPdfParameters(nodeRef);
if (logger.isDebugEnabled())
{
logger.debug("PDF generation params :");
for (NameValuePair nv : options)
{
logger.debug("" + nv.getName() + " : " + nv.getValue());
}
}
UserTransaction trx = transactionService.getUserTransaction();
try
{
trx.begin();
NodeRef pdfRef = getPdfNodeRef(nodeRef);
if (logger.isDebugEnabled())
{
logger.debug("Located pdf node at " + nodeService.getPath(pdfRef));
}
ContentWriter writer = contentService.getWriter(pdfRef, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_PDF);
adpDocument.getTemplate().getConnection().writePdf(writer, adpDocument.getRenditionPath(ADPServer.FORMAT_HIRES_PDF, null), options.toArray(new NameValuePair[options.size()]));
trx.commit();
}
catch(Throwable e)
{
try
{
if (trx.getStatus() == Status.STATUS_ACTIVE)
{
try
{
trx.rollback();
}
catch(Throwable ee)
{
logger.error("Unable to rollback transaction", ee);
}
}
} catch (Throwable eee) {}
throw new ADPException(e);
}
}
catch (ADPException e)
{
logger.error("Exception creating adpDocument instance", e);
}
}
}
private String getPdfTitle(NodeRef nodeRef)
{
String title = (String)nodeService.getProperty(nodeRef, ADPModel.PROP_PDF_TITLE);
if (title == null || ("").equals(title))
{
title = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_TITLE);
if (title == null || ("").equals(title))
{
title = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
}
}
return urlEncode(title);
}
private String getPdfSubject(NodeRef nodeRef)
{
String subject = (String)nodeService.getProperty(nodeRef, ADPModel.PROP_PDF_SUBJECT);
if (subject == null || ("").equals(subject))
{
subject = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_DESCRIPTION);
}
return urlEncode(subject);
}
private String getPdfAuthor(NodeRef nodeRef)
{
String author = (String)nodeService.getProperty(nodeRef, ADPModel.PROP_PDF_AUTHOR);
if (author == null || ("").equals(author))
{
if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_AUTHOR))
{
author = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_AUTHOR);
}
if (author == null || ("").equals(author))
{
author = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_CREATOR);
}
}
return urlEncode(author);
}
private String urlEncode(String raw)
{
String enc = "";
try
{
if (raw != null)
{
enc = URLEncoder.encode(raw, "UTF-8");
}
}
catch (UnsupportedEncodingException e)
{
logger.error("Unable to uriencode value", e);
}
return enc;
}
private List<NameValuePair> getPdfParameters(NodeRef nodeRef) throws ADPException
{
List<NameValuePair> nvpairs = new ArrayList<NameValuePair>();
nvpairs.add(new NameValuePair("title", getPdfTitle(nodeRef)));
nvpairs.add(new NameValuePair("subject", getPdfSubject(nodeRef)));
nvpairs.add(new NameValuePair("author", getPdfAuthor(nodeRef)));
String options = (String)nodeService.getProperty(nodeRef, ADPModel.PROP_PDF_OPTIONS);
if (options != null && options.trim().length() > 0)
{
String[] params = options.split("&");
int count = params.length;
for (int i = 0; i < count; i++)
{
String[] nv = params[i].split("=", 2);
if (nv.length == 2)
{
nvpairs.add(new NameValuePair(nv[0], nv[1]));
}
}
}
return nvpairs;
}
private NodeRef getPdfNodeRef(NodeRef sourceRef) throws AspectMissingException, ADPException
{
if (!nodeService.hasAspect(sourceRef, ADPModel.ASPECT_PDFED))
{
throw new AspectMissingException(ADPModel.ASPECT_PDFED, sourceRef);
}
NodeRef pdfRef = null;
List<AssociationRef> assocRefs = nodeService.getTargetAssocs(sourceRef, ADPModel.ASSOC_PDF);
if (assocRefs != null && !assocRefs.isEmpty())
{
pdfRef = assocRefs.get(0).getTargetRef();
}
else
{
NodeRef pdfFolderRef = getPDFFolderRef(sourceRef);
if (pdfFolderRef != null && nodeService.exists(pdfFolderRef))
{
String sourceName = (String)nodeService.getProperty(sourceRef, ContentModel.PROP_NAME);
String pdfName = alfrescoResolver.modifyName(sourceName, "_" + sourceRef.getId(), "pdf");
try
{
// create the pdf node
FileInfo fileInfo = fileFolderService.create(pdfFolderRef, pdfName, ContentModel.TYPE_CONTENT);
if (fileInfo != null)
{
pdfRef = fileInfo.getNodeRef();
//store the node description
Map<QName, Serializable> aspectProps = new HashMap<QName, Serializable>(1);
aspectProps.put(ContentModel.PROP_DESCRIPTION, "Created from " + sourceName);
nodeService.addAspect(pdfRef, ContentModel.ASPECT_TITLED, aspectProps);
//associate back to the source node
nodeService.createAssociation(sourceRef, pdfRef, ADPModel.ASSOC_PDF);
}
}
catch (FileExistsException e)
{
throw new ADPException(e);
}
}
}
return pdfRef;
}
protected NodeRef getPDFFolderRef(NodeRef sourceRef) throws ADPException
{
NodeRef parentRef = nodeService.getPrimaryParent(sourceRef).getParentRef();
if (parentRef == null || !nodeService.getType(parentRef).isMatch(ContentModel.TYPE_FOLDER))
{
throw new ADPException("Operation only valid for documents directly located in folders.");
}
String folderName = "PDF";
QName assocQName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, QName.createValidLocalName(folderName));
NodeRef pdfFolderRef = null;
List<ChildAssociationRef> childAssocRefs = nodeService.getChildAssocs(parentRef, ContentModel.ASSOC_CONTAINS, assocQName);
if (childAssocRefs != null && !childAssocRefs.isEmpty())
{
pdfFolderRef = childAssocRefs.get(0).getChildRef();
}
else
{
// create the pdf folder
Map<QName, Serializable> nodeProperties = new HashMap<QName, Serializable>(1);
nodeProperties.put(ContentModel.PROP_NAME, folderName);
ChildAssociationRef childAssocRef = nodeService.createNode(parentRef,
ContentModel.ASSOC_CONTAINS,
assocQName,
ContentModel.TYPE_FOLDER,
nodeProperties);
if (childAssocRef != null)
{
pdfFolderRef = childAssocRef.getChildRef();
}
}
return pdfFolderRef;
}
public static makePdfUrl(MapNode clientVersion, AlfrescoResolver alfrescoResolver)
{
String pdfurl = null;
if (clientVersion.hasAspect(ADPModel.ASPECT_PDFED))
{
logger.debug("Pdfed node found");
QNameNodeMap assocs = (QNameNodeMap)clientVersion.getAssociations();
List assocRefs = (List)assocs.get(ADPModel.ASSOC_PDF);
if (assocRefs != null && !assocRefs.isEmpty())
{
logger.debug("Valid pdf association found");
try
{
String pdfName = alfrescoResolver.modifyName(clientVersion.getName(), null, "pdf");
pdfurl = DownloadContentServlet.generateBrowserURL(((AssociationRef)assocRefs.get(0)).getTargetRef(), pdfName);
logger.debug("Built hi-res pdf url of " + pdfurl);
}
catch (Throwable e)
{
logger.error("Unable to build pdf url from association", e);
}
}
}
if (pdfurl == null)
{
String name = clientVersion.getName();
int pos = name.lastIndexOf(".");
if ( pos != -1 ) {
name = name.substring(0, pos);
}
name = name.replaceAll("[^-A-Za-z0-9_/\\.]", "_");
pdfurl = "/faces/" + adpServer.getViewDocServletName() + "/" + clientVersion.getId() + "/" + name + ".pdf";
}
logger.debug("PDF url is " + pdfurl);
return pdfurl;
}
}
you do not use this method (maybe u use it somewhere else)
public void setAdpServer(ADPServer adpServer) {
this.adpServer = adpServer;
}
there for adpServer = null;
what the error means is that adpServer.getViewDocServletName() should be ADPServer.getViewDocServletName() and that the class method should be static - hence the static method call
im no expert with static things, so i cant tell you how to get around the error
You get this error because static members don't require an instance of the object to be accessed; they belong to the class. But a non-static member belongs to an instance (an individual object). There's no way for the static method to know which instance's variable to use or method to call, and thus, the compiler happily tells you that you can't access an instance member from a class (static) method.
~
> You get this error because static members don't
> require an instance of the object to be accessed;
> they belong to the class. But a non-static member
> belongs to an instance (an individual object).
> There's no way for the static method to know which
> instance's variable to use or method to call, and
> thus, the compiler happily tells you that you can't
> access an instance member from a class (static)
> method.
>
> ~
Are you saying then it is imposible to access the data stored in "adpServer.getViewDocServletName()" inside a static method.