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 java.util.Collection;
20  import java.util.concurrent.CopyOnWriteArraySet;
21  
22  import org.chromattic.api.annotations.Create;
23  import org.chromattic.api.annotations.MappedBy;
24  import org.chromattic.api.annotations.OneToOne;
25  import org.chromattic.api.annotations.Owner;
26  import org.chromattic.api.annotations.PrimaryType;
27  import org.exoplatform.wiki.WikiException;
28  import org.exoplatform.wiki.mow.api.Wiki;
29  import org.exoplatform.wiki.mow.core.api.wiki.WikiNodeType;
30  import org.exoplatform.wiki.mow.core.api.wiki.WikiStore;
31  import org.exoplatform.wiki.mow.api.WikiType;
32  import org.exoplatform.wiki.mow.core.api.wiki.*;
33  
34  /**
35   * A Wiki store for portal, group and user wikis
36   *
37   * @version $Revision$
38   */
39  @PrimaryType(name = WikiNodeType.WIKI_STORE)
40  public abstract class WikiStoreImpl implements WikiStore {
41  
42    private MOWService mowService;
43  
44    public void setMOWService(MOWService mowService) {
45      this.mowService = mowService;
46    }
47  
48    public WikiImpl addWiki(WikiType wikiType, String name) throws WikiException {
49      return getWikiContainer(wikiType).addWiki(new Wiki(wikiType.toString(), name));
50    }
51  
52    public WikiImpl getWiki(WikiType wikiType, String name) {
53      return getWikiContainer(wikiType).getWiki(name);
54    }
55  
56    public Collection<WikiImpl> getWikis() {
57      Collection<WikiImpl> col = new CopyOnWriteArraySet<>();
58      col.addAll(getPortalWikiContainer().getAllWikis());
59      col.addAll(getGroupWikiContainer().getAllWikis());
60      col.addAll(getUserWikiContainer().getAllWikis());
61      return col;
62    }
63  
64    @SuppressWarnings("unchecked")
65    public  <W extends WikiImpl>WikiContainer<W> getWikiContainer(WikiType wikiType) {
66      boolean created = mowService.startSynchronization();
67  
68      WikiContainer wikiContainer;
69      if (wikiType == WikiType.PORTAL) {
70        wikiContainer = getPortalWikiContainer();
71      } else if (wikiType == WikiType.GROUP) {
72        wikiContainer = getGroupWikiContainer();
73      } else if (wikiType == WikiType.USER) {
74        wikiContainer = getUserWikiContainer();
75      } else {
76        throw new UnsupportedOperationException();
77      }
78  
79      mowService.stopSynchronization(created);
80  
81      return wikiContainer;
82    }
83  
84    @Create
85    public abstract PageImpl createPage();
86    
87    @Create
88    public abstract HelpPage createHelpPage();
89    
90    public HelpPage getHelpPagesContainer() {
91      boolean created = mowService.startSynchronization();
92  
93      HelpPage page = getHelpPageByChromattic();
94      if (page == null) {
95        page = createHelpPage();
96        setHelpPageByChromattic(page);
97      }
98  
99      mowService.stopSynchronization(created);
100 
101     return page;
102   }
103   
104   public PageImpl getDraftNewPagesContainer() {
105     boolean created = mowService.startSynchronization();
106 
107     PageImpl page = getDraftNewPagesContainerByChromattic();
108     if (page == null) {
109       page = createPage();
110       setDraftNewPagesContainerByChromattic(page);
111     }
112 
113     mowService.stopSynchronization(created);
114 
115     return page;
116   }
117 
118   public PageImpl getEmotionIconsContainer() {
119     boolean created = mowService.startSynchronization();
120 
121     PageImpl page = getEmotionIconsPageByChromattic();
122     if (page == null) {
123       page = createEmotionIconsPage();
124       setEmotionIconsPageByChromattic(page);
125     }
126 
127     mowService.stopSynchronization(created);
128 
129     return page;
130   }
131 
132   @OneToOne
133   @Owner
134   @MappedBy(WikiNodeType.Definition.EMOTION_ICONS_PAGE)
135   protected abstract PageImpl getEmotionIconsPageByChromattic();
136 
137   protected abstract void setEmotionIconsPageByChromattic(PageImpl page);
138 
139   @Create
140   protected abstract PageImpl createEmotionIconsPage();
141 
142   @OneToOne
143   @Owner
144   @MappedBy(WikiNodeType.Definition.PORTAL_WIKI_CONTAINER_NAME)
145   protected abstract PortalWikiContainer getPortalWikiContainerByChromattic();
146 
147   protected abstract void setPortalWikiContainerByChromattic(PortalWikiContainer portalWikiContainer);
148 
149   @Create
150   protected abstract PortalWikiContainer createPortalWikiContainer();
151 
152   @OneToOne
153   @Owner
154   @MappedBy(WikiNodeType.Definition.GROUP_WIKI_CONTAINER_NAME)
155   protected abstract GroupWikiContainer getGroupWikiContainerByChromattic();
156 
157   protected abstract void setGroupWikiContainerByChromattic(GroupWikiContainer groupWikiContainer);
158 
159   @Create
160   protected abstract GroupWikiContainer createGroupWikiContainer();
161 
162   @OneToOne
163   @Owner
164   @MappedBy(WikiNodeType.Definition.USER_WIKI_CONTAINER_NAME)
165   protected abstract UserWikiContainer getUserWikiContainerByChromattic();
166 
167   protected abstract void setUserWikiContainerByChromattic(UserWikiContainer userWikiContainer);
168 
169   @Create
170   protected abstract UserWikiContainer createUserWikiContainer();
171   
172   @OneToOne
173   @Owner
174   @MappedBy(WikiNodeType.Definition.HELP_PAGES)
175   public abstract HelpPage getHelpPageByChromattic();
176   
177   protected abstract void setHelpPageByChromattic(HelpPage page);
178   
179   @OneToOne
180   @Owner
181   @MappedBy(WikiNodeType.Definition.DRAFT_NEW_PAGES)
182   protected abstract PageImpl getDraftNewPagesContainerByChromattic();
183 
184   protected abstract void setDraftNewPagesContainerByChromattic(PageImpl page);
185 
186   private PortalWikiContainer getPortalWikiContainer() {
187     boolean created = mowService.startSynchronization();
188 
189     PortalWikiContainer portalWikiContainer = getPortalWikiContainerByChromattic();
190     if (portalWikiContainer == null) {
191       portalWikiContainer = createPortalWikiContainer();
192       setPortalWikiContainerByChromattic(portalWikiContainer);
193       mowService.persist();
194     }
195 
196     mowService.stopSynchronization(created);
197 
198     return portalWikiContainer;
199   }
200 
201   private GroupWikiContainer getGroupWikiContainer() {
202     GroupWikiContainer groupWikiContainer = getGroupWikiContainerByChromattic();
203     if (groupWikiContainer == null) {
204       groupWikiContainer = createGroupWikiContainer();
205       setGroupWikiContainerByChromattic(groupWikiContainer);
206       mowService.persist();
207     }
208     return groupWikiContainer;
209   }
210 
211   private UserWikiContainer getUserWikiContainer() {
212     UserWikiContainer userWikiContainer = getUserWikiContainerByChromattic();
213     if (userWikiContainer == null) {
214       userWikiContainer = createUserWikiContainer();
215       setUserWikiContainerByChromattic(userWikiContainer);
216       mowService.persist();
217     }
218     return userWikiContainer;
219   }
220   
221 }