View Javadoc
1   /*
2    * Copyright (C) 2003-2007 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.cms.drives.impl;
18  
19  import org.exoplatform.container.component.BaseComponentPlugin;
20  import org.exoplatform.container.xml.InitParams;
21  import org.exoplatform.container.xml.ObjectParameter;
22  import org.exoplatform.services.cms.BasePath;
23  import org.exoplatform.services.cms.drives.DriveData;
24  import org.exoplatform.services.cms.drives.ManageDriveService;
25  import org.exoplatform.services.cms.impl.DMSConfiguration;
26  import org.exoplatform.services.cms.impl.DMSRepositoryConfiguration;
27  import org.exoplatform.services.jcr.RepositoryService;
28  import org.exoplatform.services.jcr.core.ManageableRepository;
29  import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
30  import org.exoplatform.services.log.ExoLogger;
31  import org.exoplatform.services.log.Log;
32  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
33  
34  import javax.jcr.Node;
35  import javax.jcr.Session;
36  import java.util.Iterator;
37  import java.util.Set;
38  
39  public class ManageDrivePlugin extends BaseComponentPlugin {
40  
41    private static String WORKSPACE = "exo:workspace" ;
42    private static String PERMISSIONS = "exo:accessPermissions" ;
43    private static String VIEWS = "exo:views" ;
44    private static String ICON = "exo:icon" ;
45    private static String PATH = "exo:path" ;
46    private static String VIEW_REFERENCES = "exo:viewPreferences" ;
47    private static String VIEW_NON_DOCUMENT = "exo:viewNonDocument" ;
48    private static String VIEW_SIDEBAR = "exo:viewSideBar" ;
49    private static String SHOW_HIDDEN_NODE = "exo:showHiddenNode" ;
50    private static String ALLOW_CREATE_FOLDER = "exo:allowCreateFolders" ;
51  
52    private RepositoryService repositoryService_;
53    private NodeHierarchyCreator nodeHierarchyCreator_;
54    private InitParams params_ ;
55    private DMSConfiguration dmsConfiguration_;
56    private static final Log LOG  = ExoLogger.getLogger(ManageDrivePlugin.class.getName());
57  
58    public ManageDrivePlugin(RepositoryService repositoryService, InitParams params,
59        NodeHierarchyCreator nodeHierarchyCreator, DMSConfiguration dmsConfiguration) throws Exception {
60      repositoryService_ = repositoryService;
61      nodeHierarchyCreator_ = nodeHierarchyCreator ;
62      params_ = params ;
63      dmsConfiguration_ = dmsConfiguration;
64    }
65  
66    /**
67     * Init all drive data
68     * @throws Exception
69     */
70    @SuppressWarnings("unchecked")
71    public void init() throws Exception {
72      Set<String> deletedDriveNames = WCMCoreUtils.getService(ManageDriveService.class).getDeletedDriveNames();
73      Iterator<ObjectParameter> it = params_.getObjectParamIterator() ;
74      while(it.hasNext()){
75        DriveData data = (DriveData)it.next().getObject() ;
76        if (deletedDriveNames.contains(data.getName())) continue;
77        try{
78          Session session  = getSession();
79          addDrive(data, session) ;
80          session.logout();
81        }catch(Exception e) {
82          if (LOG.isErrorEnabled()) {
83            LOG.error("Unexpected error", e);
84          }
85          if (LOG.isWarnEnabled()) {
86            LOG.warn(" ==> Can not init drive '"+ data.getName()
87              +"' in repository '" + repositoryService_.getCurrentRepository().getConfiguration().getName() + "'");
88          }
89        }
90  
91      }
92    }
93  
94    /**
95     * Init data with specified repository
96     * @param repository
97     * @throws Exception
98     */
99    @SuppressWarnings("unchecked")
100   public void init(String repository) throws Exception {
101     Iterator<ObjectParameter> it = params_.getObjectParamIterator() ;
102     DriveData data = null ;
103     Session session = null ;
104     while(it.hasNext()){
105       data = (DriveData)it.next().getObject() ;
106       try {
107         session = getSession() ;
108         addDrive(data, session) ;
109         session.logout();
110       } catch(Exception e) {
111         if (LOG.isWarnEnabled()) {
112           LOG.warn(e.getMessage());
113         }
114       }
115     }
116   }
117 
118   /**
119    * Register new drive node with specified DriveData
120    * @param data Drive data
121    * @param session
122    * @throws Exception
123    */
124   private void addDrive(DriveData data, Session session) throws Exception {
125     String drivesPath = nodeHierarchyCreator_.getJcrPath(BasePath.EXO_DRIVES_PATH);
126     Node driveHome = (Node)session.getItem(drivesPath) ;
127     Node driveNode = null ;
128     if(!driveHome.hasNode(data.getName())){
129       driveNode = driveHome.addNode(data.getName(), "exo:drive");
130       driveNode.setProperty(WORKSPACE, data.getWorkspace()) ;
131       driveNode.setProperty(PERMISSIONS, data.getPermissions()) ;
132       driveNode.setProperty(PATH, data.getHomePath()) ;
133       driveNode.setProperty(VIEWS, data.getViews()) ;
134       driveNode.setProperty(ICON, data.getIcon()) ;
135       driveNode.setProperty(VIEW_REFERENCES, Boolean.toString(data.getViewPreferences())) ;
136       driveNode.setProperty(VIEW_NON_DOCUMENT, Boolean.toString(data.getViewNonDocument())) ;
137       driveNode.setProperty(VIEW_SIDEBAR, Boolean.toString(data.getViewSideBar())) ;
138       driveNode.setProperty(SHOW_HIDDEN_NODE, Boolean.toString(data.getShowHiddenNode())) ;
139       driveNode.setProperty(ALLOW_CREATE_FOLDER, data.getAllowCreateFolders()) ;
140       driveHome.save() ;
141       session.save() ;
142     }
143   }
144 
145   /**
146    * Return Session object with specified repository name
147    * @return Session object
148    * @throws Exception
149    */
150   private Session getSession() throws Exception {
151     ManageableRepository manaRepository = repositoryService_.getCurrentRepository();
152     DMSRepositoryConfiguration dmsRepoConfig = dmsConfiguration_.getConfig();
153     return manaRepository.getSystemSession(dmsRepoConfig.getSystemWorkspace());
154   }
155 }