Commit 98c01db5 authored by Brian Long's avatar Brian Long
Browse files

fixed template handling

parent b974bc8f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@
		<dependency>
			<groupId>com.inteligr8.alfresco</groupId>
			<artifactId>aps-public-rest-api</artifactId>
			<version>2.0.14</version>
			<version>2.0.15</version>
		</dependency>
		<dependency>
			<groupId>com.inteligr8.alfresco</groupId>
+23 −15
Original line number Diff line number Diff line
@@ -66,30 +66,38 @@ public class DownloadTemplateGoal extends ApsTemplateAddressibleGoal {
		try {
	        Map<TemplateType, Map<String, ? extends BaseTemplateLight>> templates = this.findTemplates(tenantId);
	        for (TemplateType ttype : templates.keySet()) {
	            this.getLog().debug("Downloading templates: " + ttype);
	            this.getLog().info("Downloading " + templates.get(ttype).size() + " " + ttype + " templates");
	            
	            for (Entry<String, ? extends BaseTemplateLight> template : templates.get(ttype).entrySet()) {
	                switch (ttype) {
	                    case Document:
	                        ObjectNode djson = ModelUtil.getInstance().readPojo(template.getValue());
	                        File dfile = new File(this.templateDirectory, template.getValue().getId() + ".doc-template.json");
	                        ModelUtil.getInstance().writeJson(djson, dfile, this.diffFriendly);
	                        if (this.normalize)
	                            new ApsTemplateJsonNormalizer(this.diffFriendly).normalizeFile(dfile, null);

	                        File dfilebin = new File(this.templateDirectory, template.getValue().getId() + ".doc-template.docx");
	                        
	                        Response response = this.getApsApi().getTemplatesApi().getDocumentTemplate((DocumentTemplateLight) template.getValue());
	                        Response response = this.getApsApi().getTemplatesApi().getDocumentTemplate(
									template.getValue().getId(),
									System.currentTimeMillis());
							try {
								if (response.getStatus() / 100 == 2) {
									InputStream istream = (InputStream) response.getEntity();
									try {
										FileUtils.copyInputStreamToFile(istream, dfilebin);
									} finally {
										istream.close();
									}
								} else {
									this.getLog().warn("The document template could not be downloaded; skipping: " + template.getValue().getName());
									continue;
								}
							} finally {
								response.close();
							}

	                        ObjectNode djson = ModelUtil.getInstance().readPojo(template.getValue());
	                        File dfile = new File(this.templateDirectory, template.getValue().getId() + ".doc-template.json");
	                        ModelUtil.getInstance().writeJson(djson, dfile, this.diffFriendly);
	                        if (this.normalize)
	                            new ApsTemplateJsonNormalizer(this.diffFriendly).normalizeFile(dfile, null);
							
	                        break;
	                    case CustomEmail:
	                        EmailTemplate etemplate = this.getApsApi().getTemplatesApi().getCustomEmailTemplate(template.getValue().getId(), tenantId);
+60 −5
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import org.codehaus.plexus.component.annotations.Component;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.inteligr8.alfresco.activiti.model.DocumentTemplateLight;
import com.inteligr8.alfresco.activiti.model.EmailTemplate;
import com.inteligr8.alfresco.activiti.model.EmailTemplateLight;
import com.inteligr8.alfresco.activiti.model.FileMultipartJersey;
import com.inteligr8.maven.aps.modeling.util.ModelUtil;

@@ -51,6 +52,12 @@ public class UploadTemplateGoal extends ApsAddressibleGoal {
	@Parameter( property = "aps-model.upload.directory", required = true, defaultValue = "${project.build.directory}/aps" )
	protected File templateDirectory;

	@Parameter( required = true, defaultValue = "false" )
	protected boolean alwaysOverwrite;

	@Parameter( property = "aps-model.dryRun", required = true, defaultValue = "false" )
	protected boolean dryRun;
    
    protected final int bufferSize = 128 * 1024;
	
	@Override
@@ -68,6 +75,13 @@ public class UploadTemplateGoal extends ApsAddressibleGoal {
            try {
                if (file.getName().endsWith(".doc-template.json")) {
                    DocumentTemplateLight template = ModelUtil.getInstance().writePojo(ModelUtil.getInstance().readJsonAsMap(file), DocumentTemplateLight.class);
                    if (!this.alwaysOverwrite && template.getId() != null && template.getCreated() != null) {
                        DocumentTemplateLight serverSideTemplate = this.getApsApi().getTemplatesApi().getDocumentTemplate(template.getId());
                        if (serverSideTemplate != null && !serverSideTemplate.getCreated().isBefore(template.getCreated())) {
                            this.getLog().debug("Document template unchanged; not updating: " + template.getId());
                            continue;
                        }
                    }
                    
                    File docxfile = new File(file.getParent(), file.getName().substring(0, file.getName().length() - ".json".length()) + ".docx");
                    if (!docxfile.exists())
@@ -78,25 +92,66 @@ public class UploadTemplateGoal extends ApsAddressibleGoal {
                    try {
                        FileMultipartJersey multipart = FileMultipartJersey.from(docxfile.getName(), bistream);
                        if (template.getId() == null) {
                            if (this.dryRun) {
                                this.getLog().info("[DRYRUN]: Creating document template: " + template.getName());
                            } else {
                                this.getLog().info("Creating document template: " + template.getName());
                                this.getApsApi().getTemplatesJerseyApi().createDocumentTemplate(tenantId, multipart);
                            }
                        } else {
                            if (this.dryRun) {
                                this.getLog().info("[DRYRUN]: Updating document template: " + template.getName());
                            } else {
                                this.getLog().info("Updating document template: " + template.getName());
                                this.getApsApi().getTemplatesJerseyApi().updateDocumentTemplate(template.getId(), tenantId, multipart);
                            }
                        }
                    } finally {
                        bistream.close();
                        fistream.close();
                    }
                } else if (file.getName().endsWith(".custom-email-template.json")) {
                    EmailTemplate template = ModelUtil.getInstance().writePojo(ModelUtil.getInstance().readJsonAsMap(file), EmailTemplate.class);
                    if (!this.alwaysOverwrite && template.getId() != null && template.getCreated() != null) {
                        EmailTemplate serverSideTemplate = this.getApsApi().getTemplatesApi().getCustomEmailTemplate(template.getId(), tenantId);
                        if (serverSideTemplate != null && !serverSideTemplate.getCreated().isBefore(template.getCreated())) {
                            this.getLog().debug("Custom email template unchanged; not updating: " + template.getId());
                            continue;
                        }
                    }

                    if (template.getId() == null) {
                        if (this.dryRun) {
                            this.getLog().info("[DRYRUN]: Creating custom email template: " + template.getName());
                        } else {
                            this.getLog().info("Creating custom email template: " + template.getName());
                            this.getApsApi().getTemplatesJerseyApi().createCustomEmailTemplate(template);
                        }
                    } else {
                        if (this.dryRun) {
                            this.getLog().info("[DRYRUN]: Updating custom email template: " + template.getName());
                        } else {
                            this.getLog().info("Updating custom email template: " + template.getName());
                            this.getApsApi().getTemplatesJerseyApi().updateCustomEmailTemplate(template.getId(), template);
                        }
                    }
                } else if (file.getName().endsWith(".system-email-template.json")) {
                    EmailTemplate template = ModelUtil.getInstance().writePojo(ModelUtil.getInstance().readJsonAsMap(file), EmailTemplate.class);
                    if (!this.alwaysOverwrite) {
                        EmailTemplate serverSideTemplate = this.getApsApi().getTemplatesApi().getSystemEmailTemplate(template.getName(), tenantId);
                        if (serverSideTemplate != null && template.getTemplate().equals(serverSideTemplate.getTemplate())) {
                            this.getLog().debug("System email template unchanged; not updating: " + template.getName());
                            continue;
                        }
                    }

                    if (this.dryRun) {
                        this.getLog().info("[DRYRUN]: Updating system email template: " + template.getName());
                    } else {
                        this.getLog().info("Updating system email template: " + template.getName());
                        this.getApsApi().getTemplatesJerseyApi().updateSystemEmailTemplate(template.getName(), template);
                    }
                }
            } catch (JsonProcessingException jpe) {
                throw new MojoExecutionException("The APS templates JSON files could not be parsed", jpe);
            } catch (IOException ie) {
+2 −2
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ import com.inteligr8.maven.aps.modeling.util.ModelUtil;
/**
 * This class implements an APS template JSON configuration file normalizer.
 * 
 * This will remove the 'created' date of each defined template.
 * This will remove the 'createdBy' of each defined template.
 * 
 * @author brian@inteligr8.com
 */
@@ -66,7 +66,7 @@ public class ApsTemplateJsonNormalizer implements ApsFileNormalizer {
		this.logger.trace("Removing excess template meta-data: {}", jsonModel.get("name"));

		int fields = jsonModel.size();
		jsonModel.remove(Arrays.asList("created", "createdBy"));
		jsonModel.remove(Arrays.asList("createdBy"));
		return jsonModel.size() < fields;
	}