1 17 package org.alfresco.repo.admin.patch.impl; 18 19 import java.io.Serializable ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import org.alfresco.i18n.I18NUtil; 24 import org.alfresco.repo.admin.patch.AbstractPatch; 25 import org.alfresco.repo.domain.Node; 26 import org.alfresco.repo.domain.PropertyValue; 27 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 28 import org.alfresco.service.namespace.QName; 29 import org.hibernate.Query; 30 import org.hibernate.Session; 31 import org.hibernate.SessionFactory; 32 import org.springframework.orm.hibernate3.HibernateCallback; 33 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 34 35 53 public class NodePropertySerializablePatch extends AbstractPatch 54 { 55 private static final String MSG_SUCCESS = "patch.fixNodeSerializableValues.result"; 56 57 private HibernateHelper helper; 58 59 public NodePropertySerializablePatch() 60 { 61 helper = new HibernateHelper(); 62 } 63 64 public void setSessionFactory(SessionFactory sessionFactory) 65 { 66 this.helper.setSessionFactory(sessionFactory); 67 } 68 69 @Override 70 protected String applyInternal() throws Exception 71 { 72 int updatedEntries = helper.fixSerializableProperties(); 73 74 String msg = I18NUtil.getMessage(MSG_SUCCESS, updatedEntries); 76 return msg; 78 } 79 80 private static class HibernateHelper extends HibernateDaoSupport 81 { 82 private static final String QUERY_GET_NODES = "node.patch.GetNodesWithPersistedSerializableProperties"; 83 84 public int fixSerializableProperties() 85 { 86 HibernateCallback callback = new HibernateCallback() 87 { 88 @SuppressWarnings ("unchecked") 89 public Object doInHibernate(Session session) 90 { 91 Query query = session.getNamedQuery(HibernateHelper.QUERY_GET_NODES); 92 Iterator <Node> iterator = query.iterate(); 93 int count = 0; 95 while (iterator.hasNext()) 96 { 97 Node node = iterator.next(); 98 Map <QName, PropertyValue> properties = node.getProperties(); 100 for (Map.Entry <QName, PropertyValue> entry : properties.entrySet()) 102 { 103 PropertyValue propertyValue = entry.getValue(); 104 if (propertyValue.getSerializableValue() == null) 105 { 106 continue; 108 } 109 else if (propertyValue.isMultiValued()) 110 { 111 continue; 113 } 114 else if (!"SERIALIZABLE".equals(propertyValue.getActualType())) 115 { 116 continue; 118 } 119 Serializable value = propertyValue.getSerializableValue(); 121 PropertyValue newPropertyValue = new PropertyValue(DataTypeDefinition.ANY, value); 123 entry.setValue(newPropertyValue); 124 count++; 125 } 126 } 127 return new Integer (count); 128 } 129 }; 130 Integer updateCount = (Integer ) getHibernateTemplate().execute(callback); 131 return updateCount.intValue(); 133 } 134 } 135 } 136 | Popular Tags |