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.wiki;
18  
19  import org.chromattic.api.annotations.*;
20  import org.exoplatform.portal.config.model.PortalConfig;
21  import org.exoplatform.services.log.ExoLogger;
22  import org.exoplatform.services.log.Log;
23  import org.exoplatform.services.security.Identity;
24  import org.exoplatform.wiki.WikiException;
25  import org.exoplatform.wiki.mow.api.WikiType;
26  import org.exoplatform.wiki.mow.api.PermissionType;
27  import org.exoplatform.wiki.utils.WikiConstants;
28  import org.xwiki.rendering.syntax.Syntax;
29  
30  import java.util.*;
31  
32  /**
33   * @version $Revision$
34   */
35  public abstract class WikiImpl {
36  
37    private static final Log LOG           = ExoLogger.getLogger(WikiImpl.class);
38  
39    private PermissionImpl permission = new PermissionImpl();
40  
41    @Create
42    public abstract PageImpl createWikiPage();
43  
44    public abstract WikiType getWikiType();
45    
46    public WikiHome getWikiHome() {
47      WikiHome home = getHome();
48      if (home == null) {
49        home = createWikiHome();
50        setHome(home);
51        home.makeVersionable();
52        home.setOwner(getOwner());
53        home.setTitle(WikiConstants.WIKI_HOME_TITLE);
54        Date now = GregorianCalendar.getInstance().getTime();
55        home.setCreatedDate(now);
56        home.setUpdatedDate(now);
57        home.setSyntax(Syntax.XWIKI_2_0.toIdString());
58        try {
59          initPermisionForWikiHome(home);
60          home.checkin();
61          home.checkout();
62        } catch (Exception e) {
63          if (LOG.isErrorEnabled()) {
64            LOG.error(e);
65          }
66          return home;
67        }
68      }
69      return home;
70    }
71    
72    private void initPermisionForWikiHome(WikiHome home) throws Exception {
73      List<String> wikiPermission = getWikiPermissions();
74      if (wikiPermission == null) {
75        home.setNonePermission();
76        return;
77      }
78      
79      HashMap<String, String[]> permMap = new HashMap<String, String[]>();
80      for (String perm : wikiPermission) {
81        String[] actions = perm.substring(0, perm.indexOf(":")).split(",");
82        perm = perm.substring(perm.indexOf(":") + 1);
83        String id = perm.substring(perm.indexOf(":") + 1);
84        List<String> jcrActions = new ArrayList<String>();
85        for (String action : actions) {
86          if (PermissionType.VIEWPAGE.toString().equals(action)) {
87            jcrActions.add(org.exoplatform.services.jcr.access.PermissionType.READ);
88          } else if (PermissionType.EDITPAGE.toString().equals(action)) {
89            jcrActions.add(org.exoplatform.services.jcr.access.PermissionType.ADD_NODE);
90            jcrActions.add(org.exoplatform.services.jcr.access.PermissionType.REMOVE);
91            jcrActions.add(org.exoplatform.services.jcr.access.PermissionType.SET_PROPERTY);
92          }
93        }
94        permMap.put(id, jcrActions.toArray(new String[jcrActions.size()]));
95      }
96      home.setPermission(permMap);
97    }
98  
99    public LinkRegistry getLinkRegistry() {
100     LinkRegistry linkRegistry = getLinkRegistryByChromattic();
101     if (linkRegistry == null) {
102       linkRegistry = createLinkRegistry();
103       setLinkRegistryByChromattic(linkRegistry);
104     }
105     return linkRegistry;
106   }
107 
108   public Trash getTrash() {
109     Trash trash = getTrashByChromattic();
110     if (trash == null) {
111       trash = createTrash();
112       setTrashByChromattic(trash);
113     }
114     return trash;
115   }
116   
117   public PreferencesImpl getPreferences()
118   {
119     PreferencesImpl preferences = getPreferencesByChromattic();
120     if (preferences == null) {
121       preferences = createPreferences();
122       setPreferencesByChromattic(preferences);
123     }
124     return preferences;
125   }
126 
127   @Name
128   public abstract String getName();
129 
130   @Property(name = WikiNodeType.Definition.OWNER)
131   public abstract String getOwner();
132 
133   public abstract void setOwner(String wikiOwner);
134 
135   @Path
136   public abstract String getPath();
137   
138   @Property(name = WikiNodeType.Definition.WIKI_PERMISSIONS)
139   public abstract List<String> getWikiPermissions();
140   public abstract void setWikiPermissions(List<String> permissions);
141   
142   @Property(name = WikiNodeType.Definition.DEFAULT_PERMISSIONS_INITED)
143   public abstract boolean getDefaultPermissionsInited();
144   public abstract void setDefaultPermissionsInited(boolean isInited);
145 
146   public PageImpl getPageByID(String id) {
147     throw new UnsupportedOperationException();
148   }
149 
150   public PageImpl getPageByURI(String uri) {
151     throw new UnsupportedOperationException();
152   }
153   
154   public abstract String getType();
155   
156   @OneToOne
157   @Owner
158   @MappedBy(WikiConstants.WIKI_HOME_NAME)
159   protected abstract WikiHome getHome();
160   protected abstract void setHome(WikiHome homePage);
161   
162   @Create
163   protected abstract WikiHome createWikiHome();
164 
165   @OneToOne
166   @Owner
167   @MappedBy(WikiNodeType.Definition.LINK_REGISTRY)
168   protected abstract LinkRegistry getLinkRegistryByChromattic();
169   protected abstract void setLinkRegistryByChromattic(LinkRegistry linkRegistry);
170 
171   @Create
172   protected abstract LinkRegistry createLinkRegistry();
173   
174   @OneToOne
175   @Owner
176   @MappedBy(WikiNodeType.Definition.TRASH_NAME)
177   protected abstract Trash getTrashByChromattic();
178   protected abstract void setTrashByChromattic(Trash trash);
179 
180   @Create
181   protected abstract Trash createTrash();
182   
183   @OneToOne
184   @Owner
185   @MappedBy(WikiNodeType.Definition.PREFERENCES)
186   protected abstract PreferencesImpl getPreferencesByChromattic();
187   protected abstract void setPreferencesByChromattic(PreferencesImpl preferences);
188   
189   @Create
190   protected abstract PreferencesImpl createPreferences();
191   
192 }