KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > admin > patch > impl > PermissionDataPatch


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.admin.patch.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.alfresco.i18n.I18NUtil;
23 import org.alfresco.repo.admin.patch.AbstractPatch;
24 import org.alfresco.repo.security.permissions.impl.hibernate.PermissionReference;
25 import org.alfresco.repo.security.permissions.impl.hibernate.PermissionReferenceImpl;
26 import org.alfresco.service.namespace.NamespaceService;
27 import org.hibernate.Query;
28 import org.hibernate.Session;
29 import org.hibernate.SessionFactory;
30 import org.springframework.orm.hibernate3.HibernateCallback;
31 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
32
33 /**
34  * The roles defined in permissionsDefinition.xml moved from <b>cm:folder</b> to <b>cm:cmobject</b>.
35  * This effects the data stored in the <b>node_perm_entry</b> table.
36  * <p>
37  * JIRA: {@link http://www.alfresco.org/jira/browse/AR-344 AR-344}
38  *
39  * @author Derek Hulley
40  */

41 public class PermissionDataPatch extends AbstractPatch
42 {
43     private static final String JavaDoc MSG_SUCCESS = "patch.updatePermissionData.result";
44     
45     private HibernateHelper helper;
46     
47     public PermissionDataPatch()
48     {
49         helper = new HibernateHelper();
50     }
51     
52     public void setSessionFactory(SessionFactory sessionFactory)
53     {
54         this.helper.setSessionFactory(sessionFactory);
55     }
56     
57     @Override JavaDoc
58     protected String JavaDoc applyInternal() throws Exception JavaDoc
59     {
60         List JavaDoc<String JavaDoc> createdNames = helper.createPermissionReferences();
61         int updatedEntries = helper.updatePermissionEntries();
62         
63         // build the result message
64
String JavaDoc msg = I18NUtil.getMessage(MSG_SUCCESS, createdNames, updatedEntries);
65         // done
66
return msg;
67     }
68
69     private static class HibernateHelper extends HibernateDaoSupport
70     {
71         private static final String JavaDoc TYPE_NAME_OLD = "folder";
72         private static final String JavaDoc TYPE_NAME_NEW = "cmobject";
73         private static final String JavaDoc[] NAMES = new String JavaDoc[] {"Coordinator", "Contributor", "Editor", "Guest"};
74         private static final String JavaDoc QUERY_UPDATE_PERM_ENTRY_TYPENAME = "permission.patch.UpdatePermissionEntryTypeName";
75         
76         public List JavaDoc<String JavaDoc> createPermissionReferences()
77         {
78             List JavaDoc<String JavaDoc> createdNames = new ArrayList JavaDoc<String JavaDoc>(4);
79             for (String JavaDoc name : NAMES)
80             {
81                 // create permission references as required, double checking for their existence first
82
PermissionReference ref = new PermissionReferenceImpl();
83                 ref.setTypeUri(NamespaceService.CONTENT_MODEL_1_0_URI);
84                 ref.setTypeName(TYPE_NAME_NEW);
85                 ref.setName(name);
86
87                 // it acts as its own key
88
PermissionReference found = (PermissionReference) getHibernateTemplate().get(
89                         PermissionReferenceImpl.class,
90                         ref);
91
92                 if (found == null)
93                 {
94                     // it was not found, so create it
95
getHibernateTemplate().save(ref);
96                     createdNames.add(name);
97                 }
98             }
99             return createdNames;
100         }
101         
102         public int updatePermissionEntries()
103         {
104             HibernateCallback callback = new HibernateCallback()
105             {
106                 public Object JavaDoc doInHibernate(Session session)
107                 {
108                     // flush any outstanding entities
109
session.flush();
110                     
111                     Query query = session.getNamedQuery(HibernateHelper.QUERY_UPDATE_PERM_ENTRY_TYPENAME);
112                     query.setString("typeNameNew", TYPE_NAME_NEW)
113                          .setString("typeNameOld", TYPE_NAME_OLD);
114                     int updateCount = query.executeUpdate();
115                     return new Integer JavaDoc(updateCount);
116                 }
117             };
118             Integer JavaDoc updateCount = (Integer JavaDoc) getHibernateTemplate().execute(callback);
119             // done
120
return updateCount.intValue();
121         }
122     }
123 }
124
Popular Tags