1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.watchlist.internal.migration

File R70000XWIKI7339DataMigration.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart1.png
82% of files have more coverage

Code metrics

6
39
6
2
183
109
9
0.23
6.5
3
1.5

Classes

Class Line # Actions
R70000XWIKI7339DataMigration 60 4 0% 3 5
0.285714328.6%
R70000XWIKI7339DataMigration.DoWorkHibernateCallback 95 35 0% 6 44
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20   
21    package org.xwiki.watchlist.internal.migration;
22   
23    import java.util.ArrayList;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.hibernate.HibernateException;
31    import org.hibernate.Query;
32    import org.hibernate.Session;
33    import org.slf4j.Logger;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.model.reference.LocalDocumentReference;
36   
37    import com.xpn.xwiki.XWikiContext;
38    import com.xpn.xwiki.XWikiException;
39    import com.xpn.xwiki.doc.XWikiDocument;
40    import com.xpn.xwiki.objects.DBStringListProperty;
41    import com.xpn.xwiki.objects.LargeStringProperty;
42    import com.xpn.xwiki.objects.PropertyInterface;
43    import com.xpn.xwiki.objects.classes.BaseClass;
44    import com.xpn.xwiki.objects.classes.DBListClass;
45    import com.xpn.xwiki.objects.classes.ListClass;
46    import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback;
47    import com.xpn.xwiki.store.migration.DataMigrationException;
48    import com.xpn.xwiki.store.migration.XWikiDBVersion;
49    import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration;
50   
51    /**
52    * Migration for XWIKI7339: Update the WatchListClass document and its objects to use DBStringList instead of TextArea
53    * fields for the watched elements.
54    *
55    * @version $Id: c5fb41ec5547d9d154c9ebe4f9f6aedfbcc98773 $
56    */
57    @Component
58    @Named("R70000XWIKI7339")
59    @Singleton
 
60    public class R70000XWIKI7339DataMigration extends AbstractHibernateDataMigration
61    {
62   
63    /** WatchList class local reference. */
64    private static final LocalDocumentReference WATCHLIST_CLASS_REFERENCE = new LocalDocumentReference("XWiki",
65    "WatchListClass");
66   
67    /**
68    * Logging framework.
69    */
70    @Inject
71    private Logger logger;
72   
 
73  0 toggle @Override
74    public String getDescription()
75    {
76  0 return "See http://jira.xwiki.org/browse/XWIKI-7339";
77    }
78   
 
79  3 toggle @Override
80    public XWikiDBVersion getVersion()
81    {
82    // XWiki 7.0, first migration.
83  3 return new XWikiDBVersion(70000);
84    }
85   
 
86  0 toggle @Override
87    public void hibernateMigrate() throws DataMigrationException, XWikiException
88    {
89  0 final XWikiContext context = getXWikiContext();
90   
91    // migrate data
92  0 getStore().executeWrite(context, new DoWorkHibernateCallback(context));
93    }
94   
 
95    private final class DoWorkHibernateCallback implements HibernateCallback<Object>
96    {
97    private final XWikiContext context;
98   
 
99  0 toggle private DoWorkHibernateCallback(XWikiContext context)
100    {
101  0 this.context = context;
102    }
103   
 
104  0 toggle @Override
105    public Object doInHibernate(Session session) throws HibernateException, XWikiException
106    {
107    // Migrate the class.
108    // Use XWiki API to edit the BaseClass but save the document directly in the Hibernate session to avoid
109    // useless work that does not apply for our migration.
110   
111  0 XWikiDocument watchListClassDocument = context.getWiki().getDocument(WATCHLIST_CLASS_REFERENCE, context);
112  0 if (watchListClassDocument.isNew()) {
113    // The class document is not yet initialized, it will be initialized by the watchlist module.
114    // Nothing for us to migrate.
115  0 return null;
116    }
117   
118  0 BaseClass watchListClass = watchListClassDocument.getXClass();
119  0 fixClassProperty(watchListClass, "wikis", "Wiki list");
120  0 fixClassProperty(watchListClass, "spaces", "Space list");
121  0 fixClassProperty(watchListClass, "documents", "Document list");
122  0 fixClassProperty(watchListClass, "users", "User list");
123   
124    // Serialize the modified XClass into the document and save it.
125    // Don't format the XML to reduce the size of the stored data as much as possible
126  0 watchListClassDocument.setXClassXML(watchListClass.toXMLString());
127  0 session.save(watchListClassDocument);
128   
129  0 logger.info("Migrated class document [{}]", watchListClassDocument.getDocumentReference());
130   
131    // Migrate existing objects.
132   
133  0 Query q =
134    session.createQuery("SELECT ls FROM BaseObject o, LargeStringProperty ls"
135    + " WHERE o.className='XWiki.WatchListClass' AND o.id=ls.id");
136   
137  0 @SuppressWarnings("unchecked")
138    List<LargeStringProperty> oldProperties = q.list();
139  0 if (oldProperties.size() == 0) {
140    // No watched elements exist that need migrating.
141  0 return null;
142    }
143   
144    // Create a migrated equivalent for each old property.
145  0 List<DBStringListProperty> newProperties = new ArrayList<DBStringListProperty>(oldProperties.size());
146  0 for (LargeStringProperty oldProperty : oldProperties) {
147  0 DBStringListProperty newProperty = new DBStringListProperty();
148  0 newProperty.setId(oldProperty.getId());
149  0 newProperty.setName(oldProperty.getName());
150    // Migrate the data to the new format (1 entry per item).
151  0 newProperty.setList(ListClass.getListFromString(oldProperty.getValue(), ",", false));
152   
153  0 newProperties.add(newProperty);
154    }
155   
156    // First delete the old properties in a separate step, to avoid ID collisions.
157  0 for (LargeStringProperty oldProperty : oldProperties) {
158  0 session.delete(oldProperty);
159    }
160   
161    // Finally, add the new migrated properties (with the old IDs).
162  0 for (DBStringListProperty newProperty : newProperties) {
163  0 session.save(newProperty);
164    }
165   
166  0 logger.info("Migrated [{}] object properties", oldProperties.size());
167   
168  0 return null;
169    }
170   
 
171  0 toggle private void fixClassProperty(BaseClass bclass, String name, String prettyName)
172    {
173  0 PropertyInterface property = bclass.get(name);
174  0 if (!(property instanceof DBListClass)) {
175  0 bclass.removeField(name);
176   
177  0 bclass.addDBListField(name, prettyName, 80, true, null);
178  0 DBListClass fixedProperty = (DBListClass) bclass.get(name);
179  0 fixedProperty.setDisplayType(ListClass.DISPLAYTYPE_INPUT);
180    }
181    }
182    }
183    }