View Javadoc
1   /*
2    * Copyright (C) 2003-2009 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.wiki.mow.core.api;
18  
19  import org.chromattic.api.ChromatticSession;
20  import org.chromattic.api.UndeclaredRepositoryException;
21  import org.exoplatform.commons.chromattic.ChromatticManager;
22  import org.exoplatform.wiki.mow.core.api.wiki.WikiNodeType;
23  import org.exoplatform.wiki.mow.core.api.wiki.WikiStore;
24  import org.exoplatform.wiki.service.impl.WikiChromatticLifeCycle;
25  
26  import javax.jcr.Node;
27  import javax.jcr.PathNotFoundException;
28  import javax.jcr.RepositoryException;
29  
30  /**
31   * @version $Revision$
32   */
33  public class MOWService {
34  
35    private WikiChromatticLifeCycle chromatticLifeCycle;
36  
37    public MOWService(ChromatticManager chromatticManager) {
38      this.chromatticLifeCycle = (WikiChromatticLifeCycle) chromatticManager.getLifeCycle(WikiChromatticLifeCycle.WIKI_LIFECYCLE_NAME);
39    }
40  
41    public ChromatticSession getSession() {
42      return chromatticLifeCycle.getSession();
43    }
44  
45    public boolean startSynchronization() {
46      if (chromatticLifeCycle.getManager().getSynchronization() == null) {
47        chromatticLifeCycle.getManager().beginRequest();
48        return true;
49      }
50      return false;
51    }
52  
53    public void stopSynchronization(boolean requestClose) {
54      if (requestClose) {
55        chromatticLifeCycle.getManager().endRequest(true);
56      }
57    }
58  
59    public boolean persist() {
60      return persist(false);
61    }
62  
63    /**
64     * Make the decision to persist JCR Storage and refresh session or not
65     *
66     * @return
67     */
68    public boolean persist(boolean isRefresh) {
69      try {
70        ChromatticSession chromatticSession = chromatticLifeCycle.getSession();
71        if (chromatticSession.getJCRSession().hasPendingChanges()) {
72          chromatticSession.getJCRSession().save();
73          if (isRefresh) {
74            chromatticSession.getJCRSession().refresh(true);
75          }
76  
77        }
78      } catch (Exception e) {
79        return false;
80      }
81      return true;
82    }
83  
84    public WikiStore getWikiStore() {
85      boolean created = this.startSynchronization();
86  
87      ChromatticSession session = chromatticLifeCycle.getSession();
88      WikiStoreImpl store = session.findByPath(WikiStoreImpl.class, "exo:applications" + "/"
89              + WikiNodeType.Definition.WIKI_APPLICATION + "/"
90              + WikiNodeType.Definition.WIKI_STORE_NAME);
91      if (store == null) {
92        try {
93          Node rootNode = session.getJCRSession().getRootNode();
94          Node publicApplicationNode = rootNode.getNode("exo:applications");
95          Node eXoWiki = null;
96          try {
97            eXoWiki = publicApplicationNode.getNode(WikiNodeType.Definition.WIKI_APPLICATION);
98          } catch (PathNotFoundException e) {
99            eXoWiki = publicApplicationNode.addNode(WikiNodeType.Definition.WIKI_APPLICATION);
100           publicApplicationNode.save();
101         }
102         Node wikiMetadata = eXoWiki.addNode(WikiNodeType.Definition.WIKI_STORE_NAME,
103                 WikiNodeType.WIKI_STORE);
104         Node wikis = eXoWiki.addNode("wikis");
105         session.save();
106         store = session.findByNode(WikiStoreImpl.class, wikiMetadata);
107 
108       } catch (RepositoryException e) {
109         throw new UndeclaredRepositoryException(e);
110       } finally {
111         this.stopSynchronization(created);
112       }
113     } else {
114       this.stopSynchronization(created);
115     }
116 
117     return store;
118   }
119 }