KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
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 /**
36  * Certain content models make extensive use of the d:any datatype, which has led
37  * to storage of simple types as serialized instances.
38  * This patch ensures that all previously serializable values are stored in their
39  * more native form in the database.<br>
40  * e.g. If a property was d:any and a string was written ("ABC"),
41  * then the value was stored in serializable_value. Instead, the newer code stores
42  * the value in string_value. None of the retrieval code is affected, but the values
43  * are made visible to queries, in addition to reducing the size of the node_properties
44  * table. This patch ensures that previously-stored values are changed to conform
45  * to the new storage mechanism.
46  * <p>
47  * JIRA: {@link http://www.alfresco.org/jira/browse/AR-359 AR-359}
48  *
49  * @see org.alfresco.repo.domain.PropertyValue
50  *
51  * @author Derek Hulley
52  */

53 public class NodePropertySerializablePatch extends AbstractPatch
54 {
55     private static final String JavaDoc 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 JavaDoc
70     protected String JavaDoc applyInternal() throws Exception JavaDoc
71     {
72         int updatedEntries = helper.fixSerializableProperties();
73         
74         // build the result message
75
String JavaDoc msg = I18NUtil.getMessage(MSG_SUCCESS, updatedEntries);
76         // done
77
return msg;
78     }
79
80     private static class HibernateHelper extends HibernateDaoSupport
81     {
82         private static final String JavaDoc QUERY_GET_NODES = "node.patch.GetNodesWithPersistedSerializableProperties";
83         
84         public int fixSerializableProperties()
85         {
86             HibernateCallback callback = new HibernateCallback()
87             {
88                 @SuppressWarnings JavaDoc("unchecked")
89                 public Object JavaDoc doInHibernate(Session session)
90                 {
91                     Query query = session.getNamedQuery(HibernateHelper.QUERY_GET_NODES);
92                     Iterator JavaDoc<Node> iterator = query.iterate();
93                     // iterate over the nodes
94
int count = 0;
95                     while (iterator.hasNext())
96                     {
97                         Node node = iterator.next();
98                         // retrieve the node properties
99
Map JavaDoc<QName, PropertyValue> properties = node.getProperties();
100                         // check each property
101
for (Map.Entry JavaDoc<QName, PropertyValue> entry : properties.entrySet())
102                         {
103                             PropertyValue propertyValue = entry.getValue();
104                             if (propertyValue.getSerializableValue() == null)
105                             {
106                                 // the property was not persisted as a serializable - nothing to do
107
continue;
108                             }
109                             else if (propertyValue.isMultiValued())
110                             {
111                                 // this is a persisted collection - nothing to do
112
continue;
113                             }
114                             else if (!"SERIALIZABLE".equals(propertyValue.getActualType()))
115                             {
116                                 // only handle actual types that were pushed in as any old type
117
continue;
118                             }
119                             // make sure that this value is persisted correctly
120
Serializable JavaDoc value = propertyValue.getSerializableValue();
121                             // put it back
122
PropertyValue newPropertyValue = new PropertyValue(DataTypeDefinition.ANY, value);
123                             entry.setValue(newPropertyValue);
124                             count++;
125                         }
126                     }
127                     return new Integer JavaDoc(count);
128                 }
129             };
130             Integer JavaDoc updateCount = (Integer JavaDoc) getHibernateTemplate().execute(callback);
131             // done
132
return updateCount.intValue();
133         }
134     }
135 }
136
Popular Tags