Skip to content

Commit 57628e6

Browse files
author
Nathan McMinn
committed
Preparing toolkit service for major refactoring
1 parent 1cf8813 commit 57628e6

File tree

1 file changed

+9
-360
lines changed

1 file changed

+9
-360
lines changed

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

Lines changed: 9 additions & 360 deletions
Original file line numberDiff line numberDiff line change
@@ -18,365 +18,14 @@
1818

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

21-
22-
import java.io.Serializable;
23-
import java.util.HashMap;
24-
import java.util.Map;
25-
26-
import org.alfresco.extension.pdftoolkit.repo.action.executer.PDFAppendActionExecuter;
27-
import org.alfresco.extension.pdftoolkit.repo.action.executer.PDFEncryptionActionExecuter;
28-
import org.alfresco.extension.pdftoolkit.repo.action.executer.PDFInsertAtPageActionExecuter;
29-
import org.alfresco.extension.pdftoolkit.repo.action.executer.PDFSignatureActionExecuter;
30-
import org.alfresco.extension.pdftoolkit.repo.action.executer.PDFSplitActionExecuter;
31-
import org.alfresco.extension.pdftoolkit.repo.action.executer.PDFSplitAtPageActionExecuter;
32-
import org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter;
33-
import org.alfresco.repo.processor.BaseProcessorExtension;
34-
import org.alfresco.service.ServiceException;
35-
import org.alfresco.service.ServiceRegistry;
36-
import org.alfresco.service.cmr.action.Action;
37-
import org.alfresco.service.cmr.action.ActionService;
38-
import org.alfresco.service.cmr.repository.NodeRef;
39-
import org.alfresco.service.cmr.repository.NodeService;
40-
import org.apache.commons.logging.Log;
41-
import org.apache.commons.logging.LogFactory;
42-
import org.mozilla.javascript.NativeObject;
43-
44-
45-
public class PDFToolkitService extends BaseProcessorExtension
21+
public interface PDFToolkitService
4622
{
47-
48-
private ServiceRegistry serviceRegistry;
49-
private static final Log logger = LogFactory.getLog(PDFToolkitService.class);
50-
51-
private String PARAM_TARGET = "target";
52-
53-
public void setServiceRegistry(ServiceRegistry serviceRegistry)
54-
{
55-
this.serviceRegistry = serviceRegistry;
56-
}
57-
58-
public PDFToolkitService()
59-
{
60-
}
61-
62-
63-
public void jsConstructor()
64-
{
65-
}
66-
67-
68-
public String getClassName()
69-
{
70-
return "PDFToolkitService";
71-
}
72-
73-
74-
/**
75-
* Wrapper for the encrypt PDF action. This calls the PDFEncryptionActionExecuter
76-
*
77-
* When used in a JS context, this code expects a JSON object to with the following structure:
78-
*
79-
* {
80-
* target : "workspace:SpacesStore://node-uuid",
81-
* destination-folder : "workspace:SpacesStore://node-uuid",
82-
* user-password : "password",
83-
* owner-password : "password",
84-
* allow-print : true,
85-
* allow-copy : true,
86-
* allow-content-modification : true,
87-
* allow-annotation-modification : true,
88-
* allow-form-fill : true,
89-
* allow-screen-reader : true,
90-
* allow-degraded-print : true,
91-
* allow-assembly : true,
92-
* encryption-level : "0",
93-
* exclude-metadata : true
94-
* }
95-
*
96-
* For the available options for encryption-level, look at the constraint pdfc-encryptionlevel
97-
* in module-context.xml
98-
*/
99-
public void encryptPDF(NativeObject obj)
100-
{
101-
102-
Map<String, Serializable> params = buildParamMap(obj);
103-
NodeRef toEncrypt = getActionTargetNode(params);
104-
this.executePDFAction(PDFEncryptionActionExecuter.NAME, params, toEncrypt);
105-
}
106-
107-
/**
108-
* Wrapper for the sign PDF action. This calls the PDFSignatureActionExecuter
109-
*
110-
* When used in a JS context, this code expects a JSON object to with the following structure:
111-
*
112-
* {
113-
* target : "workspace:SpacesStore://node-uuid",
114-
* destination-folder : "workspace:SpacesStore://node-uuid",
115-
* private-key : "workspace:SpacesStore://node-uuid",
116-
* location : "location",
117-
* reason : "reason",
118-
* key-password : "keypassword",
119-
* width : "200",
120-
* height : "50",
121-
* key-type : "default",
122-
* alias : "alias",
123-
* store-password : "storepassword",
124-
* visibility : "visible",
125-
* position : "center",
126-
* location-x : "50",
127-
* location-y : "50"
128-
* }
129-
*
130-
* For the available options for visibility, look at the constraint pdfc-visibility
131-
* in module-context.xml
132-
*
133-
* For the available options for key-type, look at the constraint pdfc-keytype
134-
* in module-context.xml
135-
*
136-
* For the available options for position, look at the constraint pdfc-position
137-
* in module-context.xml
138-
*/
139-
public void signPDF(NativeObject obj)
140-
{
141-
Map<String, Serializable> params = buildParamMap(obj);
142-
143-
//check and make sure we have a valid ref for the private key
144-
NodeRef key = getDependentNode(params, PDFSignatureActionExecuter.PARAM_PRIVATE_KEY);
145-
params.put(PDFSignatureActionExecuter.PARAM_PRIVATE_KEY, key);
146-
147-
NodeRef toSign = getActionTargetNode(params);
148-
this.executePDFAction(PDFSignatureActionExecuter.NAME, params, toSign);
149-
}
150-
151-
/**
152-
* Wrapper for the watermark PDF action. This calls the PDFWatermarkActionExecuter
153-
*
154-
* When used in a JS context, this code expects a JSON object to with the following structure:
155-
*
156-
* {
157-
* target : "workspace:SpacesStore://node-uuid",
158-
* destination-folder : "workspace:SpacesStore://node-uuid",
159-
* watermark-image : "workspace:SpacesStore://node-uuid",
160-
* position : "center",
161-
* location-x : "50",
162-
* location-y : "50",
163-
* watermark-type : "image",
164-
* watermark-pages : "all",
165-
* watermark-depth : "under",
166-
* watermark-text : "Text to use as watermark",
167-
* watermark-font : "Courier",
168-
* watermark-size : "18"
169-
* }
170-
*
171-
* For the available options for position, look at the constraint pdfc-position
172-
* in module-context.xml
173-
*
174-
* For the available options for watermark-type, look at the constraint pdfc-watermarktype
175-
* in module-context.xml
176-
*
177-
* For the available options for watermark-pages, look at the constraint pdfc-page
178-
* in module-context.xml
179-
*
180-
* For the available options for watermark-depth, look at the constraint pdfc-depth
181-
* in module-context.xml
182-
*
183-
* For the available options for watermark-font, look at the constraint pdfc-font
184-
* in module-context.xml
185-
*/
186-
public void watermarkPDF(NativeObject obj)
187-
{
188-
Map<String, Serializable> params = buildParamMap(obj);
189-
NodeRef toWatermark = getActionTargetNode(params);
190-
191-
//if this is an image watermark, verify that the node exists and add it to the
192-
//params as a noderef instead of a string
193-
if(params.get(PDFWatermarkActionExecuter.PARAM_WATERMARK_TYPE)
194-
.toString().equalsIgnoreCase(PDFWatermarkActionExecuter.TYPE_IMAGE))
195-
{
196-
NodeRef image = getDependentNode(params, PDFWatermarkActionExecuter.PARAM_WATERMARK_IMAGE);
197-
params.put(PDFWatermarkActionExecuter.PARAM_WATERMARK_IMAGE, image);
198-
}
199-
200-
this.executePDFAction(PDFWatermarkActionExecuter.NAME, params, toWatermark);
201-
}
202-
203-
/**
204-
* Wrapper for the split PDF action. This calls the PDFSplitActionExecuter
205-
*
206-
* When used in a JS context, this code expects a JSON object to with the following structure:
207-
*
208-
* {
209-
* target : "workspace:SpacesStore://node-uuid",
210-
* destination-folder : "workspace:SpacesStore://node-uuid",
211-
* split-frequency : "1"
212-
* }
213-
*
214-
*/
215-
public void splitPDF(NativeObject obj)
216-
{
217-
Map<String, Serializable> params = buildParamMap(obj);
218-
NodeRef toSplit = getActionTargetNode(params);
219-
this.executePDFAction(PDFSplitActionExecuter.NAME, params, toSplit);
220-
}
221-
222-
/**
223-
* Wrapper for the split at page PDF action. This calls the PDFSplitAtPageActionExecuter
224-
*
225-
* When used in a JS context, this code expects a JSON object to with the following structure:
226-
*
227-
* {
228-
* target : "workspace:SpacesStore://node-uuid",
229-
* destination-folder : "workspace:SpacesStore://node-uuid",
230-
* split-at-page : "1"
231-
* }
232-
*
233-
*/
234-
public void splitPDFAtPage(NativeObject obj)
235-
{
236-
Map<String, Serializable> params = buildParamMap(obj);
237-
NodeRef toSplit = getActionTargetNode(params);
238-
this.executePDFAction(PDFSplitAtPageActionExecuter.NAME, params, toSplit);
239-
}
240-
241-
/**
242-
* Wrapper for the append PDF action. This calls the PDFAppendActionExecuter
243-
*
244-
* When used in a JS context, this code expects a JSON object to with the following structure:
245-
*
246-
* {
247-
* target : "workspace:SpacesStore://node-uuid",
248-
* destination-folder : "workspace:SpacesStore://node-uuid",
249-
* append-content : "workspace:SpacesStore://node-uuid",
250-
* destination-name : "new_file_name.pdf"
251-
* }
252-
*
253-
*/
254-
public void appendPDF(NativeObject obj)
255-
{
256-
Map<String, Serializable> params = buildParamMap(obj);
257-
NodeRef appendTo = getActionTargetNode(params);
258-
259-
//check and make sure we have a valid ref for the pdf to append
260-
NodeRef toAppend = getDependentNode(params, "append-content");
261-
params.put("append-content", toAppend);
262-
263-
this.executePDFAction(PDFAppendActionExecuter.NAME, params, appendTo);
264-
}
265-
266-
/**
267-
* Wrapper for the insert PDF action. This calls the PDFInsertAtPageActionExecuter
268-
*
269-
* When used in a JS context, this code expects a JSON object to with the following structure:
270-
*
271-
* {
272-
* target : "workspace:SpacesStore://node-uuid",
273-
* destination-folder : "workspace:SpacesStore://node-uuid",
274-
* insert-content : "workspace:SpacesStore://node-uuid",
275-
* destination-name : "new_file_name.pdf",
276-
* insert-at-page : "1"
277-
* }
278-
*
279-
*/
280-
public void insertPDF(NativeObject obj)
281-
{
282-
Map<String, Serializable> params = buildParamMap(obj);
283-
NodeRef insertInto = getActionTargetNode(params);
284-
285-
//check and make sure we have a valid ref for the pdf to insert
286-
NodeRef toInsert= getDependentNode(params, PDFInsertAtPageActionExecuter.PARAM_INSERT_CONTENT);
287-
params.put(PDFInsertAtPageActionExecuter.PARAM_INSERT_CONTENT, toInsert);
288-
289-
this.executePDFAction(PDFInsertAtPageActionExecuter.NAME, params, insertInto);
290-
}
291-
292-
/**
293-
* Executes a specific PDF action called by the service
294-
*
295-
* @param name
296-
* @param params
297-
* @param actioned
298-
*/
299-
private void executePDFAction(String name, Map<String, Serializable> params, NodeRef actioned)
300-
{
301-
ActionService actionService = serviceRegistry.getActionService();
302-
Action toExecute = actionService.createAction(name, params);
303-
actionService.executeAction(toExecute, actioned);
304-
}
305-
306-
/**
307-
* Finds a named String parameter and converts it to a NodeRef
308-
*
309-
* @param params
310-
* @param name
311-
* @return
312-
*/
313-
private NodeRef getDependentNode(Map<String, Serializable> params, String name)
314-
{
315-
NodeService nodeService = serviceRegistry.getNodeService();
316-
317-
//grab the target node and make sure it exists
318-
if(params.get(name) == null)
319-
{
320-
throw new ServiceException("Object property " + name + " must be provided");
321-
}
322-
String nodeString = params.get(name).toString();
323-
324-
325-
NodeRef dep = new NodeRef(nodeString);
326-
if(!nodeService.exists(dep))
327-
{
328-
throw new ServiceException("Object property " + name + " must be a valid node reference");
329-
}
330-
331-
return dep;
332-
}
333-
334-
/**
335-
* Get a NodeRef to the target node, defined by the "node" property of the Javascript object
336-
* passed to the service
337-
*
338-
* @param params
339-
* @return
340-
*/
341-
private NodeRef getActionTargetNode(Map<String, Serializable> params)
342-
{
343-
344-
return getDependentNode(params, PARAM_TARGET);
345-
}
346-
347-
/**
348-
* Build a proper parameters map suitable for passing to the ActionService
349-
*
350-
* @param obj
351-
* @return
352-
*/
353-
private Map<String, Serializable> buildParamMap(NativeObject obj)
354-
{
355-
Map<String, Serializable> params = nativeObjectToMap(obj);
356-
357-
NodeRef destination = getDependentNode(params, PDFEncryptionActionExecuter.PARAM_DESTINATION_FOLDER);
358-
359-
//add the noderef back to the param map
360-
params.put(PDFEncryptionActionExecuter.PARAM_DESTINATION_FOLDER, destination);
361-
362-
return params;
363-
}
364-
365-
/**
366-
* Can't cast to Map, as Alfresco's Rhino version is WAY out of date and
367-
* NativeObject doesn't implement Map. So, we do this instead.
368-
*
369-
* @param obj
370-
*/
371-
private Map<String, Serializable> nativeObjectToMap(NativeObject obj)
372-
{
373-
Map<String, Serializable> map = new HashMap<String, Serializable>();
374-
Object[] keys = obj.getAllIds();
375-
for(Object key : keys)
376-
{
377-
Object value = NativeObject.getProperty(obj, key.toString());
378-
map.put(key.toString(), (Serializable)value);
379-
}
380-
return map;
381-
}
23+
public void encryptPDF();
24+
public void signPDF();
25+
public void watermarkPDF();
26+
public void splitPDF();
27+
public void splitPDFAtPage();
28+
public void insertPDF();
29+
public void deletePagesFromPDF();
30+
public void rotatePDF();
38231
}

0 commit comments

Comments
 (0)