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.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 41 public class PermissionDataPatch extends AbstractPatch 42 { 43 private static final String 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 58 protected String applyInternal() throws Exception 59 { 60 List <String > createdNames = helper.createPermissionReferences(); 61 int updatedEntries = helper.updatePermissionEntries(); 62 63 String msg = I18NUtil.getMessage(MSG_SUCCESS, createdNames, updatedEntries); 65 return msg; 67 } 68 69 private static class HibernateHelper extends HibernateDaoSupport 70 { 71 private static final String TYPE_NAME_OLD = "folder"; 72 private static final String TYPE_NAME_NEW = "cmobject"; 73 private static final String [] NAMES = new String [] {"Coordinator", "Contributor", "Editor", "Guest"}; 74 private static final String QUERY_UPDATE_PERM_ENTRY_TYPENAME = "permission.patch.UpdatePermissionEntryTypeName"; 75 76 public List <String > createPermissionReferences() 77 { 78 List <String > createdNames = new ArrayList <String >(4); 79 for (String name : NAMES) 80 { 81 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 PermissionReference found = (PermissionReference) getHibernateTemplate().get( 89 PermissionReferenceImpl.class, 90 ref); 91 92 if (found == null) 93 { 94 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 doInHibernate(Session session) 107 { 108 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 (updateCount); 116 } 117 }; 118 Integer updateCount = (Integer ) getHibernateTemplate().execute(callback); 119 return updateCount.intValue(); 121 } 122 } 123 } 124 | Popular Tags |