View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (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 General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.wcm.portal.impl;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  import java.util.Set;
23  import java.util.concurrent.ConcurrentHashMap;
24  
25  import javax.jcr.Node;
26  import javax.jcr.NodeIterator;
27  import javax.jcr.PathNotFoundException;
28  import javax.jcr.Session;
29  import javax.jcr.query.Query;
30  import javax.jcr.query.QueryResult;
31  
32  import org.exoplatform.portal.config.model.PortalConfig;
33  import org.exoplatform.services.jcr.RepositoryService;
34  import org.exoplatform.services.jcr.core.ExtendedNode;
35  import org.exoplatform.services.jcr.core.ManageableRepository;
36  import org.exoplatform.services.jcr.ext.common.SessionProvider;
37  import org.exoplatform.services.log.ExoLogger;
38  import org.exoplatform.services.log.Log;
39  import org.exoplatform.services.wcm.core.NodeLocation;
40  import org.exoplatform.services.wcm.core.WCMConfigurationService;
41  import org.exoplatform.services.wcm.core.WebSchemaConfigService;
42  import org.exoplatform.services.wcm.portal.LivePortalManagerService;
43  import org.picocontainer.Startable;
44  
45  /*
46   * Created by The eXo Platform SAS
47   * @author : Hoa.Pham
48   *          hoa.pham@exoplatform.com
49   * Jun 19, 2008
50   */
51  
52  /**
53   * The Class LivePortalManagerServiceImpl.
54   */
55  public class LivePortalManagerServiceImpl implements LivePortalManagerService, Startable {
56  
57    private final String                      PORTAL_FOLDER   = "exo:portalFolder";
58  
59    private static final Log LOG = ExoLogger.getLogger(LivePortalManagerServiceImpl.class.getName());
60  
61    private ConcurrentHashMap<String, String> livePortalPaths = new ConcurrentHashMap<String, String>();
62  
63    private RepositoryService                 repositoryService;
64  
65    private WCMConfigurationService           wcmConfigService;
66  
67    /**
68     * Instantiates a new live portal manager service impl.
69     *
70     * @param webSchemaConfigService the web schema config service
71     * @param wcmConfigurationService the wcm config service
72     * @param repositoryService the repository service
73     */
74    public LivePortalManagerServiceImpl(
75        WebSchemaConfigService webSchemaConfigService,
76        WCMConfigurationService wcmConfigurationService,
77        RepositoryService repositoryService) {
78      this.wcmConfigService = wcmConfigurationService;
79      this.repositoryService = repositoryService;
80    }
81  
82    /*
83     * (non-Javadoc)
84     * @see
85     * org.exoplatform.services.wcm.portal.LivePortalManagerService#getLivePortal
86     * (java.lang.String, org.exoplatform.services.jcr.ext.common.SessionProvider)
87     */
88    public final Node getLivePortal(final SessionProvider sessionProvider, final String portalName) throws Exception {
89      String currentRepository = repositoryService.getCurrentRepository().getConfiguration().getName();
90      return getLivePortal(sessionProvider, currentRepository, portalName);
91    }
92  
93    /*
94     * (non-Javadoc)
95     * @see
96     * org.exoplatform.services.wcm.portal.LivePortalManagerService#getLivePortals
97     * (org.exoplatform.services.jcr.ext.common.SessionProvider)
98     */
99    public final List<Node> getLivePortals(final SessionProvider sessionProvider) throws Exception {
100     String currentRepository = repositoryService.getCurrentRepository().getConfiguration().getName();
101     return getLivePortals(sessionProvider, currentRepository);
102   }
103 
104   /*
105    * (non-Javadoc)
106    * @seeorg.exoplatform.services.wcm.portal.LivePortalManagerService#
107    * getLiveSharedPortal
108    * (org.exoplatform.services.jcr.ext.common.SessionProvider)
109    */
110   public final Node getLiveSharedPortal(final SessionProvider sessionProvider) throws Exception {
111     String currentRepository = repositoryService.getCurrentRepository().getConfiguration().getName();
112     return getLiveSharedPortal(sessionProvider, currentRepository);
113   }
114 
115   /*
116    * (non-Javadoc)
117    * @see
118    * org.exoplatform.services.wcm.portal.LivePortalManagerService#getLivePortal
119    * (java.lang.String, java.lang.String,
120    * org.exoplatform.services.jcr.ext.common.SessionProvider)
121    */
122   public final Node getLivePortal(final SessionProvider sessionProvider,
123                                   final String repository,
124                                   final String portalName) throws Exception {
125     Node portalsStorage = getLivePortalsStorage(sessionProvider);
126     return portalsStorage.getNode(portalName);
127   }
128 
129   /*
130    * (non-Javadoc)
131    * @see
132    * org.exoplatform.services.wcm.portal.LivePortalManagerService#getLivePortals
133    * (java.lang.String, org.exoplatform.services.jcr.ext.common.SessionProvider)
134    */
135   public final List<Node> getLivePortals(final SessionProvider sessionProvider, final String repository) throws Exception {
136     List<Node> list = new ArrayList<Node>();
137     Node portalsStorage = getLivePortalsStorage(sessionProvider);
138     for (NodeIterator iterator = portalsStorage.getNodes(); iterator.hasNext(); ) {
139       Node node = iterator.nextNode();
140       if (node.isNodeType(PORTAL_FOLDER)) {
141         list.add(node);
142       }
143     }
144     return list;
145   }
146 
147   /*
148    * (non-Javadoc)
149    * @seeorg.exoplatform.services.wcm.portal.LivePortalManagerService#
150    * getLiveSharedPortal(java.lang.String,
151    * org.exoplatform.services.jcr.ext.common.SessionProvider)
152    */
153   public final Node getLiveSharedPortal(final SessionProvider sessionProvider,
154                                         final String repository) throws Exception {
155     Node portalsStorage = getLivePortalsStorage(sessionProvider);
156     String sharePortalName = wcmConfigService.getSharedPortalName();
157     try {
158       return portalsStorage.getNode(sharePortalName);
159     } catch (PathNotFoundException e) {
160       return null;
161     }
162   }
163 
164   private Node getLivePortalsStorage(final SessionProvider sessionProvider) throws Exception {
165     NodeLocation locationEntry = wcmConfigService.getLivePortalsLocation();
166     String workspace = locationEntry.getWorkspace();
167     String portalsStoragePath = locationEntry.getPath();
168     ManageableRepository manageableRepository = repositoryService.getCurrentRepository();
169     Session session = sessionProvider.getSession(workspace, manageableRepository);
170     return (Node)session.getItem(portalsStoragePath);
171   }
172 
173   /*
174    * (non-Javadoc)
175    * @see
176    * org.exoplatform.services.wcm.portal.LivePortalManagerService#addLivePortal
177    * (org.exoplatform.portal.config.model.PortalConfig,
178    * org.exoplatform.services.jcr.ext.common.SessionProvider)
179    */
180   public final void addLivePortal(final SessionProvider sessionProvider, final PortalConfig portalConfig)
181   throws Exception {
182     Node livePortalsStorage = getLivePortalsStorage(sessionProvider) ;
183     String portalName = portalConfig.getName();
184     if(livePortalsStorage.hasNode(portalName)) {
185       return;
186     }
187     ExtendedNode newPortal = (ExtendedNode)livePortalsStorage.addNode(portalName,PORTAL_FOLDER);
188     if (!newPortal.isNodeType("exo:owneable"))
189       newPortal.addMixin("exo:owneable");
190     if(newPortal.canAddMixin("metadata:siteMetadata")) {
191       newPortal.addMixin("metadata:siteMetadata");
192       newPortal.setProperty("siteTitle",portalName);
193       newPortal.setProperty("keywords",portalName);
194       newPortal.setProperty("robots","index,follow");
195     }
196     if(newPortal.canAddMixin("dc:elementSet")) {
197       newPortal.addMixin("dc:elementSet");
198     }
199     //Need set some other property for the portal node from portal config like access permission ..
200     newPortal.getSession().save();
201     //put sharedPortal path to the map at the first time when run this method
202     if(livePortalPaths.size() == 0) {
203       String sharedPortalName = wcmConfigService.getSharedPortalName();
204       NodeLocation nodeLocation = wcmConfigService.getLivePortalsLocation();
205       livePortalPaths.put(sharedPortalName,nodeLocation.getPath() + "/"+ sharedPortalName);
206     }
207     livePortalPaths.put(portalName,newPortal.getPath());
208   }
209 
210   /*
211    * (non-Javadoc)
212    * @see
213    * org.exoplatform.services.wcm.portal.LivePortalManagerService#removeLivePortal
214    * (org.exoplatform.portal.config.model.PortalConfig,
215    * org.exoplatform.services.jcr.ext.common.SessionProvider)
216    */
217   public void removeLivePortal(final SessionProvider sessionProvider, final PortalConfig portalConfig)
218   throws Exception {
219     //Remove site content folder for the portal in this version
220     //for next version, we will move it to backup ws
221     Node node = getLivePortal(sessionProvider, portalConfig.getName());
222     Session session = node.getSession();
223     node.remove();
224     session.save();
225     livePortalPaths.remove(portalConfig.getName());
226   }
227 
228   /* (non-Javadoc)
229    * @see org.exoplatform.services.wcm.portal.LivePortalManagerService#getLivePortalsPath()
230    */
231   public Collection<String> getLivePortalsPath() throws Exception {
232     return livePortalPaths.values();
233   }
234 
235   public String getPortalNameByPath(String portalPath) throws Exception {
236     Set<String> keys = livePortalPaths.keySet();
237     for(String portalName: keys.toArray(new String[keys.size()])) {
238       if(livePortalPaths.get(portalName).equalsIgnoreCase(portalPath)) {
239         return portalName;
240       }
241     }
242     return null;
243   }
244 
245   public Node getLivePortalByChild(Node childNode) throws Exception {
246     for(String portalPath: livePortalPaths.values()) {
247       if(childNode.getPath().startsWith(portalPath)) {
248         return (Node)childNode.getSession().getItem(portalPath);
249       }
250     }
251     return null;
252   }
253 
254   public void start() {
255     if (LOG.isInfoEnabled()) {
256       LOG.info("Start LivePortalManagementService....");
257     }
258     SessionProvider sessionProvider = null;
259     Session session = null;
260     try {
261       sessionProvider = SessionProvider.createSystemProvider();
262       ManageableRepository repository = repositoryService.getCurrentRepository();
263       NodeLocation nodeLocation = wcmConfigService.getLivePortalsLocation();
264       session = sessionProvider.getSession(nodeLocation.getWorkspace(),repository);
265       String statement = "select * from exo:portalFolder where jcr:path like '" + nodeLocation.getPath() + "/%'";
266       Query query = session.getWorkspace().getQueryManager().createQuery(statement,Query.SQL);
267       QueryResult result = query.execute();
268       for(NodeIterator iterator = result.getNodes(); iterator.hasNext();) {
269         Node portalNode = iterator.nextNode();
270         livePortalPaths.putIfAbsent(portalNode.getName(),portalNode.getPath());
271       }
272     } catch (Exception e) {
273       if (LOG.isErrorEnabled()) {
274         LOG.error("Error when starting LivePortalManagerService: ", e);
275       }
276     } finally {
277       sessionProvider.close();
278     }
279   }
280 
281   public void stop() {
282   }
283 
284   public String getPortalPathByName(String portalName) throws Exception {
285     return livePortalPaths.get(portalName);
286   }
287 }