View Javadoc
1   /*
2    * Copyright (C) 2003-2012 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
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   * Created by The eXo Platform SAS
43   * Author : eXoPlatform
44   *          exo@exoplatform.com
45   * Mar 16, 2012
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          // sourcePath should looks like : collaboration:/sites
63          // content/live/acme
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 }