Skip to content

Commit e909931

Browse files
author
Nathan McMinn
committed
More work toward pulling PDF functionality into service layer.
1 parent 9784ad4 commit e909931

File tree

4 files changed

+133
-115
lines changed

4 files changed

+133
-115
lines changed

pdf-toolkit-repo/src/main/amp/config/alfresco/module/org.alfresco.extension.pdftoolkit/context/alfresco-pdf-toolkit-context.xml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,7 @@
112112
<!-- pdfdelete -->
113113
<bean id="pdf-rotate"
114114
class="org.alfresco.extension.pdftoolkit.repo.action.executer.PDFRotateActionExecuter"
115-
parent="action-executer" depends-on="pdftoolkit-messages">
116-
<property name="serviceRegistry">
117-
<ref bean="ServiceRegistry" />
118-
</property>
119-
<property name="applicableTypes">
120-
<list>
121-
<value>{http://www.alfresco.org/model/content/1.0}content</value>
122-
</list>
123-
</property>
124-
<property name="createNew" value="true"/>
115+
parent="org.alfresco.extension.pdf-toolkit-action">
125116
</bean>
126117

127118
<!-- Page count web script -->

pdf-toolkit-repo/src/main/java/org/alfresco/extension/pdftoolkit/repo/action/executer/BasePDFActionExecuter.java

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,20 @@
1919
package org.alfresco.extension.pdftoolkit.repo.action.executer;
2020

2121

22-
import java.io.File;
23-
import java.io.Serializable;
2422
import java.util.List;
2523

26-
import org.alfresco.error.AlfrescoRuntimeException;
2724
import org.alfresco.extension.pdftoolkit.service.PDFToolkitService;
28-
import org.alfresco.model.ContentModel;
2925
import org.alfresco.repo.action.ParameterDefinitionImpl;
3026
import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
3127
import org.alfresco.service.ServiceRegistry;
3228
import org.alfresco.service.cmr.action.ParameterDefinition;
3329
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
34-
import org.alfresco.service.cmr.model.FileInfo;
35-
import org.alfresco.service.cmr.model.FileNotFoundException;
36-
import org.alfresco.service.cmr.repository.ContentReader;
37-
import org.alfresco.service.cmr.repository.ContentService;
38-
import org.alfresco.service.cmr.repository.NodeRef;
39-
import org.alfresco.service.namespace.QName;
40-
import org.alfresco.util.TempFileProvider;
4130

4231

4332
public abstract class BasePDFActionExecuter
4433
extends ActionExecuterAbstractBase
4534
{
4635
private boolean createNew = true;
47-
48-
protected static final String FILE_EXTENSION = ".pdf";
49-
protected static final String FILE_MIMETYPE = "application/pdf";
50-
protected static final String PDF = "pdf";
5136

5237
protected ServiceRegistry serviceRegistry;
5338
protected PDFToolkitService pdfToolkitService;
@@ -90,96 +75,6 @@ protected void addParameterDefinitions(List<ParameterDefinition> paramList)
9075
* @param actionedUponNodeRef
9176
* @return
9277
*/
93-
protected ContentReader getReader(NodeRef nodeRef)
94-
{
95-
// First check that the node is a sub-type of content
96-
QName typeQName = serviceRegistry.getNodeService().getType(nodeRef);
97-
if (serviceRegistry.getDictionaryService().isSubClass(typeQName, ContentModel.TYPE_CONTENT) == false)
98-
{
99-
// it is not content, so can't transform
100-
return null;
101-
}
102-
103-
// Get the content reader
104-
ContentReader contentReader = serviceRegistry.getContentService().getReader(nodeRef, ContentModel.PROP_CONTENT);
105-
106-
return contentReader;
107-
}
108-
109-
/**
110-
* @param ruleAction
111-
* @param filename
112-
* @return
113-
*/
114-
protected NodeRef createDestinationNode(String filename, NodeRef destinationParent, NodeRef target, boolean inplace)
115-
{
116-
117-
NodeRef destinationNode;
118-
119-
// if inplace mode is turned on, the destination for the modified content
120-
// is the original node
121-
if(inplace)
122-
{
123-
return target;
124-
}
125-
126-
if(createNew)
127-
{
128-
//create a file in the right location
129-
FileInfo fileInfo = serviceRegistry.getFileFolderService().create(destinationParent, filename, ContentModel.TYPE_CONTENT);
130-
destinationNode = fileInfo.getNodeRef();
131-
}
132-
else
133-
{
134-
try
135-
{
136-
FileInfo fileInfo = serviceRegistry.getFileFolderService().copy(target, destinationParent, filename);
137-
destinationNode = fileInfo.getNodeRef();
138-
}
139-
catch(FileNotFoundException fnf)
140-
{
141-
throw new AlfrescoRuntimeException(fnf.getMessage(), fnf);
142-
}
143-
}
144-
145-
return destinationNode;
146-
}
147-
148-
protected int getInteger(Serializable val)
149-
{
150-
if(val == null)
151-
{
152-
return 0;
153-
}
154-
try
155-
{
156-
return Integer.parseInt(val.toString());
157-
}
158-
catch(NumberFormatException nfe)
159-
{
160-
return 0;
161-
}
162-
}
163-
164-
protected File getTempFile(NodeRef nodeRef)
165-
{
166-
File alfTempDir = TempFileProvider.getTempDir();
167-
File toolkitTempDir = new File(alfTempDir.getPath() + File.separatorChar + nodeRef.getId());
168-
toolkitTempDir.mkdir();
169-
File file = new File(toolkitTempDir, serviceRegistry.getFileFolderService().getFileInfo(nodeRef).getName());
170-
171-
return file;
172-
}
173-
174-
protected File nodeRefToTempFile(NodeRef nodeRef)
175-
{
176-
ContentService cs = serviceRegistry.getContentService();
177-
File tempFromFile = TempFileProvider.createTempFile("PDFAConverter-", nodeRef.getId()
178-
+ FILE_EXTENSION);
179-
ContentReader reader = cs.getReader(nodeRef, ContentModel.PROP_CONTENT);
180-
reader.getContent(tempFromFile);
181-
return tempFromFile;
182-
}
18378

18479
public void setPDFToolkitService(PDFToolkitService pdfToolkitService)
18580
{

pdf-toolkit-repo/src/main/java/org/alfresco/extension/pdftoolkit/service/PDFToolkitService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@
1818

1919
package org.alfresco.extension.pdftoolkit.service;
2020

21+
import java.io.File;
22+
import java.io.Serializable;
23+
24+
import org.alfresco.service.cmr.repository.ContentReader;
25+
import org.alfresco.service.cmr.repository.NodeRef;
26+
2127
public interface PDFToolkitService
2228
{
29+
// the actual action code
2330
public void encryptPDF();
2431
public void signPDF();
2532
public void watermarkPDF();
@@ -28,4 +35,11 @@ public interface PDFToolkitService
2835
public void insertPDF();
2936
public void deletePagesFromPDF();
3037
public void rotatePDF();
38+
39+
// common support methods
40+
public ContentReader getReader(NodeRef nodeRef);
41+
public NodeRef createDestinationNode(String filename, NodeRef destinationParent, NodeRef target, boolean inplace, boolean createNew);
42+
public int getInteger(Serializable val);
43+
public File getTempFile(NodeRef nodeRef);
44+
public File nodeRefToTempFile(NodeRef nodeRef);
3145
}

pdf-toolkit-repo/src/main/java/org/alfresco/extension/pdftoolkit/service/PDFToolkitServiceImpl.java

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
package org.alfresco.extension.pdftoolkit.service;
22

3+
import java.io.File;
4+
import java.io.Serializable;
5+
6+
import org.alfresco.error.AlfrescoRuntimeException;
7+
import org.alfresco.model.ContentModel;
38
import org.alfresco.service.ServiceRegistry;
9+
import org.alfresco.service.cmr.dictionary.DictionaryService;
10+
import org.alfresco.service.cmr.model.FileFolderService;
11+
import org.alfresco.service.cmr.model.FileInfo;
12+
import org.alfresco.service.cmr.model.FileNotFoundException;
13+
import org.alfresco.service.cmr.repository.ContentReader;
14+
import org.alfresco.service.cmr.repository.ContentService;
15+
import org.alfresco.service.cmr.repository.NodeRef;
16+
import org.alfresco.service.cmr.repository.NodeService;
17+
import org.alfresco.service.namespace.QName;
18+
import org.alfresco.util.TempFileProvider;
419

520
public class PDFToolkitServiceImpl implements PDFToolkitService {
621

22+
23+
public static final String FILE_EXTENSION = ".pdf";
24+
public static final String FILE_MIMETYPE = "application/pdf";
25+
public static final String PDF = "pdf";
26+
727
private ServiceRegistry serviceRegistry;
28+
private NodeService ns;
29+
private ContentService cs;
30+
private FileFolderService ffs;
31+
private DictionaryService ds;
832

933
@Override
1034
public void encryptPDF() {
@@ -54,11 +78,105 @@ public void rotatePDF() {
5478

5579
}
5680

81+
public ContentReader getReader(NodeRef nodeRef)
82+
{
83+
// First check that the node is a sub-type of content
84+
QName typeQName = ns.getType(nodeRef);
85+
if (ds.isSubClass(typeQName, ContentModel.TYPE_CONTENT) == false)
86+
{
87+
// it is not content, so can't transform
88+
return null;
89+
}
90+
91+
// Get the content reader
92+
ContentReader contentReader = cs.getReader(nodeRef, ContentModel.PROP_CONTENT);
93+
94+
return contentReader;
95+
}
96+
97+
/**
98+
* @param ruleAction
99+
* @param filename
100+
* @return
101+
*/
102+
public NodeRef createDestinationNode(String filename, NodeRef destinationParent, NodeRef target, boolean inplace, boolean createNew)
103+
{
104+
105+
NodeRef destinationNode;
106+
107+
// if inplace mode is turned on, the destination for the modified content
108+
// is the original node
109+
if(inplace)
110+
{
111+
return target;
112+
}
113+
114+
if(createNew)
115+
{
116+
//create a file in the right location
117+
FileInfo fileInfo = ffs.create(destinationParent, filename, ContentModel.TYPE_CONTENT);
118+
destinationNode = fileInfo.getNodeRef();
119+
}
120+
else
121+
{
122+
try
123+
{
124+
FileInfo fileInfo = ffs.copy(target, destinationParent, filename);
125+
destinationNode = fileInfo.getNodeRef();
126+
}
127+
catch(FileNotFoundException fnf)
128+
{
129+
throw new AlfrescoRuntimeException(fnf.getMessage(), fnf);
130+
}
131+
}
132+
133+
return destinationNode;
134+
}
135+
136+
public int getInteger(Serializable val)
137+
{
138+
if(val == null)
139+
{
140+
return 0;
141+
}
142+
try
143+
{
144+
return Integer.parseInt(val.toString());
145+
}
146+
catch(NumberFormatException nfe)
147+
{
148+
return 0;
149+
}
150+
}
151+
152+
public File getTempFile(NodeRef nodeRef)
153+
{
154+
File alfTempDir = TempFileProvider.getTempDir();
155+
File toolkitTempDir = new File(alfTempDir.getPath() + File.separatorChar + nodeRef.getId());
156+
toolkitTempDir.mkdir();
157+
File file = new File(toolkitTempDir, ffs.getFileInfo(nodeRef).getName());
158+
159+
return file;
160+
}
161+
162+
public File nodeRefToTempFile(NodeRef nodeRef)
163+
{
164+
File tempFromFile = TempFileProvider.createTempFile("PDFAConverter-", nodeRef.getId()
165+
+ FILE_EXTENSION);
166+
ContentReader reader = cs.getReader(nodeRef, ContentModel.PROP_CONTENT);
167+
reader.getContent(tempFromFile);
168+
return tempFromFile;
169+
}
170+
57171
/**
58172
* @param serviceRegistry
59173
*/
60174
public void setServiceRegistry(ServiceRegistry serviceRegistry)
61175
{
62176
this.serviceRegistry = serviceRegistry;
177+
ns = serviceRegistry.getNodeService();
178+
cs = serviceRegistry.getContentService();
179+
ffs = serviceRegistry.getFileFolderService();
180+
ds = serviceRegistry.getDictionaryService();
63181
}
64182
}

0 commit comments

Comments
 (0)