KickJava   Java API By Example, From Geeks To Geeks.

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


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.model.ContentModel;
24 import org.alfresco.repo.admin.patch.AbstractPatch;
25 import org.alfresco.repo.security.permissions.impl.hibernate.PermissionReference;
26 import org.alfresco.repo.security.permissions.impl.hibernate.PermissionReferenceImpl;
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 permission 'Guest' has been renamed to 'Consumer'.
35  *
36  * @author David Caruana
37  */

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