1 17 package org.alfresco.repo.admin.patch.impl; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 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 38 public class UpdateGuestPermissionPatch extends AbstractPatch 39 { 40 private static final String 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 55 protected String applyInternal() throws Exception 56 { 57 List <String > createdNames = helper.createPermissionReferences(); 58 int updatedPermEntries = helper.updatePermissionEntries(); 59 60 String msg = I18NUtil.getMessage(MSG_SUCCESS, createdNames, updatedPermEntries); 62 return msg; 64 } 65 66 private static class HibernateHelper extends HibernateDaoSupport 67 { 68 private static final String NAME_OLD = "Guest"; 69 private static final String NAME_NEW = "Consumer"; 70 private static final String [] NAMES = new String [] {"Consumer"}; 71 72 private static final String QUERY_UPDATE_PERM_ENTRY_NAME = "permission.patch.UpdatePermissionName"; 73 74 75 public List <String > createPermissionReferences() 76 { 77 List <String > createdNames = new ArrayList <String >(4); 78 for (String name : NAMES) 79 { 80 PermissionReference ref = new PermissionReferenceImpl(); 82 ref.setTypeUri(ContentModel.TYPE_CMOBJECT.getNamespaceURI()); 83 ref.setTypeName(ContentModel.TYPE_CMOBJECT.getLocalName()); 84 ref.setName(name); 85 86 PermissionReference found = (PermissionReference) getHibernateTemplate().get( 88 PermissionReferenceImpl.class, 89 ref); 90 91 if (found == null) 92 { 93 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 doInHibernate(Session session) 106 { 107 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 (updateCount); 115 } 116 }; 117 Integer updateCount = (Integer ) getHibernateTemplate().execute(callback); 118 return updateCount.intValue(); 120 } 121 122 } 123 } 124 | Popular Tags |