1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.extensions.utils;
18
19 import java.util.Date;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import javax.jcr.Node;
25 import javax.jcr.Session;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.exoplatform.container.xml.InitParams;
29 import org.exoplatform.container.xml.ObjectParameter;
30 import org.exoplatform.container.xml.PortalContainerInfo;
31 import org.exoplatform.services.ecm.publication.PublicationService;
32 import org.exoplatform.services.jcr.RepositoryService;
33 import org.exoplatform.services.jcr.core.ManageableRepository;
34 import org.exoplatform.services.jcr.ext.common.SessionProvider;
35 import org.exoplatform.services.log.ExoLogger;
36 import org.exoplatform.services.log.Log;
37 import org.exoplatform.services.wcm.extensions.deployment.PublicationDeploymentDescriptor;
38 import org.exoplatform.services.wcm.publication.WCMPublicationService;
39 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
40
41
42
43
44
45
46
47 public class PublicationUtils {
48 private static final Log LOG = ExoLogger.getLogger(PublicationUtils.class.getName());
49
50 public static void deployPublicationToPortal(InitParams initParams,
51 RepositoryService repositoryService,
52 WCMPublicationService wcmPublicationService,
53 SessionProvider sessionProvider,
54 String portalName) throws Exception {
55 Iterator iterator = initParams.getObjectParamIterator();
56 PublicationDeploymentDescriptor deploymentDescriptor = null;
57 try {
58 while (iterator.hasNext()) {
59 ObjectParameter objectParameter = (ObjectParameter) iterator.next();
60 deploymentDescriptor = (PublicationDeploymentDescriptor) objectParameter.getObject();
61 List<String> contents = deploymentDescriptor.getContents();
62
63
64
65 HashMap<String, String> context_ = new HashMap<String, String>();
66 PublicationService publicationService = WCMCoreUtils.getService(PublicationService.class);
67 PortalContainerInfo containerInfo = WCMCoreUtils.getService(PortalContainerInfo.class);
68 String containerName = containerInfo.getContainerName();
69 context_.put("containerName", containerName);
70
71
72 for (String sourcePath:contents) {
73 try {
74 if (portalName != null && portalName.length() > 0) {
75 sourcePath = StringUtils.replace(sourcePath, "{portalName}", portalName);
76 }
77 String[] src = sourcePath.split(":");
78
79 if (src.length == 2) {
80 ManageableRepository repository = repositoryService.getCurrentRepository();
81 Session session = sessionProvider.getSession(src[0], repository);
82 Node nodeSrc = (Node) session.getItem(src[1]);
83 if(publicationService.isNodeEnrolledInLifecycle(nodeSrc)) publicationService.unsubcribeLifecycle(nodeSrc);
84 wcmPublicationService.updateLifecyleOnChangeContent(nodeSrc, "default", "__system", "published");
85 nodeSrc.save();
86
87 }
88 if (LOG.isInfoEnabled()) {
89 LOG.info(sourcePath + " has been published.");
90 }
91 } catch (Exception ex) {
92 if (LOG.isErrorEnabled()) {
93 LOG.error("publication for " + sourcePath + " FAILED at "
94 + new Date().toString() + "\n",
95 ex);
96 }
97 }
98 }
99
100
101 }
102 } catch (Exception ex) {
103 if (LOG.isErrorEnabled()) {
104 LOG.error("publication plugin FAILED at "
105 + new Date().toString() + "\n",
106 ex);
107 }
108 throw ex;
109 }
110 }
111
112 }