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.queries.impl;
18  
19  import java.util.GregorianCalendar;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Set;
24  
25  import javax.jcr.Node;
26  import javax.jcr.Session;
27  import javax.jcr.Value;
28  import javax.jcr.ValueFactory;
29  
30  import org.exoplatform.container.component.BaseComponentPlugin;
31  import org.exoplatform.container.xml.InitParams;
32  import org.exoplatform.container.xml.ObjectParameter;
33  import org.exoplatform.container.xml.ValueParam;
34  import org.exoplatform.services.cms.impl.DMSConfiguration;
35  import org.exoplatform.services.cms.impl.DMSRepositoryConfiguration;
36  import org.exoplatform.services.jcr.RepositoryService;
37  import org.exoplatform.services.jcr.ext.common.SessionProvider;
38  
39  public class QueryPlugin extends BaseComponentPlugin {
40  
41    private static String STATEMENT = "jcr:statement" ;
42    private static String LANGUAGE = "jcr:language" ;
43    private static String PERMISSIONS = "exo:accessPermissions" ;
44    private static String CACHED_RESULT = "exo:cachedResult" ;
45  
46    private InitParams params_ ;
47    private boolean autoCreateInNewRepository_ = false;
48    private RepositoryService repositoryService_ ;
49    private DMSConfiguration dmsConfiguration_;
50    private Set<String> configuredQueries_;
51  
52    public QueryPlugin(RepositoryService repositoryService, InitParams params,
53        DMSConfiguration dmsConfiguration) throws Exception {
54      params_ = params ;
55      repositoryService_ = repositoryService ;
56      ValueParam autoInitParam = params.getValueParam("autoCreateInNewRepository") ;
57      if(autoInitParam !=null) {
58        autoCreateInNewRepository_ = Boolean.parseBoolean(autoInitParam.getValue()) ;
59      }
60      dmsConfiguration_ = dmsConfiguration;
61    }
62  
63    public void init(String basedQueriesPath) throws Exception {
64      configuredQueries_ = new HashSet<String>();
65      Iterator<ObjectParameter> it = params_.getObjectParamIterator() ;
66      Session session = null ;
67      if(autoCreateInNewRepository_) {
68        session = getSession();
69        Node queryHomeNode = (Node)session.getItem(basedQueriesPath);
70        while(it.hasNext()) {
71          QueryData data = (QueryData)it.next().getObject() ;
72          addQuery(queryHomeNode,data);
73        }
74        queryHomeNode.save();
75        session.save();
76        session.logout();
77      } else {
78        session = getSession() ;
79        Node queryHomeNode = (Node)session.getItem(basedQueriesPath);
80        while(it.hasNext()) {
81          QueryData data = (QueryData)it.next().getObject() ;
82          addQuery(queryHomeNode,data) ;
83        }
84        queryHomeNode.save();
85        session.save();
86        session.logout();
87      }
88    }
89  
90    private Session getSession() throws Exception {
91      DMSRepositoryConfiguration dmsRepoConfig = dmsConfiguration_.getConfig();
92      SessionProvider sessionProvider = SessionProvider.createSystemProvider();
93      return sessionProvider.getSession(dmsRepoConfig.getSystemWorkspace(), 
94          repositoryService_.getCurrentRepository()) ;
95    }
96  
97    private void addQuery(Node queryHome, QueryData data) throws Exception {
98      configuredQueries_.add(data.getName());
99      if(queryHome.hasNode(data.getName())) return ;
100     ValueFactory vt = queryHome.getSession().getValueFactory() ;
101     Node queryNode = queryHome.addNode(data.getName(), "nt:query");
102 
103     if (!queryNode.isNodeType("exo:datetime")) {
104       queryNode.addMixin("exo:datetime");
105     }
106     queryNode.setProperty("exo:dateCreated",new GregorianCalendar()) ;
107 
108     queryNode.addMixin("mix:sharedQuery") ;
109     queryNode.setProperty(STATEMENT, data.getStatement()) ;
110     queryNode.setProperty(LANGUAGE, data.getLanguage()) ;
111     List<String> queryPermissions = data.getPermissions() ;
112     Value[] vls = new Value[queryPermissions.size()];
113     int i = 0;
114     for(String per : queryPermissions) {
115       Value vl = vt.createValue(per) ;
116       vls[i] = vl ;
117       i++ ;
118     }
119     queryNode.setProperty(PERMISSIONS, vls) ;
120     queryNode.setProperty(CACHED_RESULT, data.getCacheResult()) ;
121   }
122   
123   public Set<String> getAllConfiguredQueries() {
124     return configuredQueries_;
125   }
126 }