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;
18  
19  import java.util.Calendar;
20  import java.util.GregorianCalendar;
21  
22  import javax.jcr.Node;
23  import javax.jcr.RepositoryException;
24  import javax.jcr.lock.LockException;
25  import javax.jcr.nodetype.ConstraintViolationException;
26  import javax.jcr.nodetype.NoSuchNodeTypeException;
27  import javax.jcr.version.VersionException;
28  
29  import org.exoplatform.services.jcr.access.PermissionType;
30  import org.exoplatform.services.jcr.core.ExtendedNode;
31  import org.exoplatform.services.jcr.ext.common.SessionProvider;
32  import org.exoplatform.services.security.ConversationState;
33  import org.exoplatform.services.security.IdentityConstants;
34  import org.exoplatform.services.wcm.core.BaseWebSchemaHandler;
35  import org.exoplatform.services.wcm.core.NodetypeConstant;
36  
37  /**
38   * Created by The eXo Platform SAS.
39   *
40   * @author : Hoa.Pham hoa.pham@exoplatform.com May 28, 2008
41   */
42  public class PortalFolderSchemaHandler extends BaseWebSchemaHandler {
43    /**
44     * Instantiates a new portal folder schema handler.
45     */
46    public PortalFolderSchemaHandler()  {
47    }
48  
49    /**
50     * Gets the CSS folder.
51     *
52     * @param portalFolder the portal folder
53     * @return the cSS folder
54     * @throws Exception the exception
55     */
56    public Node getCSSFolder(final Node portalFolder) throws Exception {
57      return portalFolder.hasNode("css") ? portalFolder.getNode("css") : null;
58    }
59  
60    /**
61     * Gets the javasscript folder.
62     *
63     * @param portalFolder the portal folder
64     * @return the javasscript folder node
65     * @throws Exception the exception
66     */
67    public Node getJSFolder(final Node portalFolder) throws Exception {
68      return portalFolder.hasNode("js") ? portalFolder.getNode("js") : null;
69    }
70  
71    /**
72     * Gets the multimedia folder.
73     *
74     * @param portalFolder the portal folder
75     * @return the multimedia folder
76     * @throws Exception the exception
77     */
78    public Node getMultimediaFolder(final Node portalFolder) throws Exception {
79      return portalFolder.getNode("medias");
80    }
81  
82    /**
83     * Gets the images folder.
84     *
85     * @param portalFolder the portal folder
86     * @return the images folder
87     * @throws Exception the exception
88     */
89    public Node getImagesFolder(final Node portalFolder) throws Exception {
90      return portalFolder.getNode("medias/images");
91    }
92  
93    /**
94     * Gets the video folder.
95     *
96     * @param portalFolder the portal folder
97     * @return the video folder
98     * @throws Exception the exception
99     */
100   public Node getVideoFolder(final Node portalFolder) throws Exception {
101     return portalFolder.getNode("medias/videos");
102   }
103 
104   /**
105    * Gets the audio folder.
106    *
107    * @param portalFolder the portal folder
108    * @return the audio folder
109    * @throws Exception the exception
110    */
111   public Node getAudioFolder(final Node portalFolder) throws Exception{
112     return portalFolder.getNode("medias/audio");
113   }
114 
115   /**
116    * Gets the document storage.
117    *
118    * @param portalFolder the portal folder
119    * @return the document storage
120    * @throws Exception the exception
121    */
122   public Node getDocumentStorage(Node portalFolder) throws Exception {
123     return portalFolder.getNode("documents");
124   }
125 
126   /**
127    * Gets the link folder.
128    *
129    * @param portalFolder the portal folder
130    * @return the link folder
131    * @throws Exception the exception
132    */
133   public Node getLinkFolder(Node portalFolder) throws Exception {
134     return portalFolder.getNode("links");
135   }
136 
137   /**
138    * Gets the web content storage.
139    *
140    * @param portalFolder the portal folder
141    * @return the web content storage
142    * @throws Exception the exception
143    */
144   public Node getWebContentStorage (final Node portalFolder) throws Exception {
145     return portalFolder.getNode("web contents");
146   }
147 
148   /* (non-Javadoc)
149    * @see org.exoplatform.services.wcm.core.BaseWebSchemaHandler#getHandlerNodeType()
150    */
151   protected String getHandlerNodeType() { return "exo:portalFolder"; }
152 
153   /* (non-Javadoc)
154    * @see org.exoplatform.services.wcm.core.BaseWebSchemaHandler#getParentNodeType()
155    */
156   protected String getParentNodeType() { return "nt:unstructured"; }
157 
158   protected void updateNode(Node node) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
159     addMixin(node, NodetypeConstant.EXO_OWNEABLE); 
160     addMixin(node, NodetypeConstant.EXO_DATETIME); 
161     addMixin(node, NodetypeConstant.EXO_MODIFY);
162     addMixin(node, NodetypeConstant.EXO_SORTABLE);
163     
164     node.setProperty(NodetypeConstant.EXO_DATE_CREATED, new GregorianCalendar());
165     node.setProperty(NodetypeConstant.EXO_LAST_MODIFIED_DATE, new GregorianCalendar());
166     
167     ConversationState conversationState = ConversationState.getCurrent();
168     String userName = (conversationState == null) ? node.getSession().getUserID() :
169                                                     conversationState.getIdentity().getUserId();
170     node.setProperty(NodetypeConstant.EXO_LAST_MODIFIER, userName);
171     
172     node.setProperty(NodetypeConstant.EXO_NAME, node.getName());    
173   }
174   
175   /* (non-Javadoc)
176    * @see org.exoplatform.services.wcm.core.BaseWebSchemaHandler#process(javax.jcr.Node)
177    */
178   public void onCreateNode(SessionProvider sessionProvider, final Node portalFolder) throws Exception {
179     Calendar calendar = new GregorianCalendar();
180     if (!portalFolder.hasNode("js")) {
181       Node jsFolder = portalFolder.addNode("js","exo:jsFolder");
182       updateNode(jsFolder);
183     }
184 
185     if (!portalFolder.hasNode("css")) {
186       Node cssFolder = portalFolder.addNode("css","exo:cssFolder");
187       updateNode(cssFolder);
188     }
189 
190     if (!portalFolder.hasNode("medias")) {
191       Node multimedia = portalFolder.addNode("medias","exo:multimediaFolder");
192       updateNode(multimedia);
193       
194       Node images = multimedia.addNode("images",NT_FOLDER);
195       addMixin(images, "exo:pictureFolder");
196       updateNode(images);
197 
198       Node video = multimedia.addNode("videos",NT_FOLDER);
199       addMixin(video, "exo:videoFolder");
200       updateNode(video);
201       
202       Node audio = multimedia.addNode("audio",NT_FOLDER);
203       addMixin(audio, "exo:musicFolder");
204       updateNode(audio);
205     }
206 
207     if (!portalFolder.hasNode("documents")) {
208       Node document = portalFolder.addNode("documents",NT_UNSTRUCTURED);
209       addMixin(document, "exo:documentFolder");
210       
211       updateNode(document);
212       
213       document.addMixin("exo:privilegeable");
214       ((ExtendedNode)document).setPermission(IdentityConstants.ANY, PermissionType.ALL);
215     }
216 
217     if (!portalFolder.hasNode("web contents")) {
218       Node webContents = portalFolder.addNode("web contents","exo:webFolder");
219       updateNode(webContents);
220 
221       Node themes = webContents.addNode("site artifacts","exo:themeFolder");
222       updateNode(themes);
223     }
224 
225     if (!portalFolder.hasNode("links")) {
226       Node links = portalFolder.addNode("links", "exo:linkFolder");
227       updateNode(links);
228     }
229 
230     if (!portalFolder.hasNode("categories")) {
231       Node categoryFolder = portalFolder.addNode("categories", NT_UNSTRUCTURED);
232       updateNode(categoryFolder);
233     }
234 
235     if (!portalFolder.hasNode("ApplicationData")) {
236       Node applicationDataFolder = portalFolder.addNode("ApplicationData", NT_UNSTRUCTURED);
237       updateNode(applicationDataFolder);      
238       addMixin(applicationDataFolder, "exo:hiddenable");
239 
240       Node newsletterApplicationFolder = applicationDataFolder.addNode("NewsletterApplication", NT_UNSTRUCTURED);
241       updateNode(newsletterApplicationFolder);
242 
243       Node defaultTemplatesFolder = newsletterApplicationFolder.addNode("DefaultTemplates", NT_UNSTRUCTURED);
244       updateNode(defaultTemplatesFolder);
245 
246       Node newsletterCategoriesFolder = newsletterApplicationFolder.addNode("Categories", NT_UNSTRUCTURED);
247       updateNode(newsletterCategoriesFolder);
248 
249       Node newsletterUserFolder = newsletterApplicationFolder.addNode("Users", NT_UNSTRUCTURED);
250       updateNode(newsletterUserFolder);
251     }
252 
253     portalFolder.getSession().save();
254   }
255 }