1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.store.migration.hibernate

File R6079XWIKI1878DataMigration.java

 

Coverage histogram

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

Code metrics

4
29
6
1
150
98
10
0.34
4.83
6
1.67

Classes

Class Line # Actions
R6079XWIKI1878DataMigration 59 29 0% 10 37
0.0512820525.1%
 

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 com.xpn.xwiki.store.migration.hibernate;
22   
23    import java.util.Iterator;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.hibernate.HibernateException;
30    import org.hibernate.Query;
31    import org.hibernate.Session;
32    import org.hibernate.Transaction;
33    import org.slf4j.Logger;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.manager.ComponentLookupException;
36   
37    import com.xpn.xwiki.XWikiContext;
38    import com.xpn.xwiki.XWikiException;
39    import com.xpn.xwiki.doc.rcs.XWikiPatch;
40    import com.xpn.xwiki.doc.rcs.XWikiRCSNodeContent;
41    import com.xpn.xwiki.doc.rcs.XWikiRCSNodeId;
42    import com.xpn.xwiki.store.XWikiHibernateBaseStore;
43    import com.xpn.xwiki.store.XWikiHibernateVersioningStore;
44    import com.xpn.xwiki.store.XWikiVersioningStoreInterface;
45    import com.xpn.xwiki.store.migration.DataMigrationException;
46    import com.xpn.xwiki.store.migration.XWikiDBVersion;
47   
48    /**
49    * Migration for XWIKI1878: Fix xwikircs table isdiff data not matching RCS state of some revisions (when the state says
50    * "full" the isdiff column in the database should be false). Note: This data migration should only be executed if the
51    * R4359XWIKI1459 one has already been executed (i.e. if the database is in version >= 4360). This is because it fixes a
52    * bug in R4359XWIKI1459 which has now been fixed.
53    *
54    * @version $Id: 02a1bf90f88ee02eea82eff7cbc11bbeb8c643a1 $
55    */
56    @Component
57    @Named("R6079XWIKI1878")
58    @Singleton
 
59    public class R6079XWIKI1878DataMigration extends AbstractHibernateDataMigration
60    {
61    /**
62    * Logger.
63    */
64    @Inject
65    private Logger logger;
66   
 
67  0 toggle @Override
68    public String getDescription()
69    {
70  0 return "See http://jira.xwiki.org/jira/browse/XWIKI-1878";
71    }
72   
 
73  206 toggle @Override
74    public XWikiDBVersion getVersion()
75    {
76  206 return new XWikiDBVersion(6079);
77    }
78   
 
79  0 toggle @Override
80    public boolean shouldExecute(XWikiDBVersion startupVersion)
81    {
82  0 return (startupVersion.getVersion() >= 4360);
83    }
84   
85    /**
86    * @return version store system for execute store-specific actions.
87    * @throws XWikiException if the store could not be reached
88    */
 
89  0 toggle private XWikiHibernateVersioningStore getVersioningStore() throws XWikiException
90    {
91  0 try {
92  0 return (XWikiHibernateVersioningStore) this.componentManager
93    .getInstance(XWikiVersioningStoreInterface.class, "hibernate");
94    } catch (ComponentLookupException e) {
95  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
96    XWikiException.ERROR_XWIKI_STORE_MIGRATION,
97    String.format("Unable to reach the versioning store for database %s", getXWikiContext().getWikiId()),
98    e);
99    }
100    }
101   
 
102  0 toggle @Override
103    public void hibernateMigrate() throws DataMigrationException, XWikiException
104    {
105    // migrate data
106  0 getStore().executeWrite(getXWikiContext(), true,
107    new XWikiHibernateBaseStore.HibernateCallback<Object>()
108    {
 
109  0 toggle @Override
110    public Object doInHibernate(Session session) throws HibernateException, XWikiException
111    {
112  0 try {
113  0 Query query = session.createQuery("select rcs.id, rcs.patch, doc.fullName "
114    + "from XWikiDocument as doc, XWikiRCSNodeContent as rcs where "
115    + "doc.id = rcs.id.docId and rcs.patch.diff = true and rcs.patch.content like '<?xml%'");
116  0 Iterator it = query.list().iterator();
117   
118  0 XWikiContext context = getXWikiContext();
119  0 XWikiHibernateVersioningStore versioningStore = getVersioningStore();
120  0 Transaction originalTransaction = versioningStore.getTransaction(context);
121  0 versioningStore.setSession(null, context);
122  0 versioningStore.setTransaction(null, context);
123   
124  0 while (it.hasNext()) {
125  0 Object[] result = (Object[]) it.next();
126  0 if (R6079XWIKI1878DataMigration.this.logger.isInfoEnabled()) {
127  0 R6079XWIKI1878DataMigration.this.logger.info("Fixing document [{}]...", result[2]);
128    }
129   
130    // Reconstruct a XWikiRCSNodeContent object with isDiff set to false and update it.
131  0 XWikiRCSNodeId nodeId = (XWikiRCSNodeId) result[0];
132  0 XWikiRCSNodeContent fixedNodeContent = new XWikiRCSNodeContent(nodeId);
133  0 XWikiPatch patch = (XWikiPatch) result[1];
134  0 patch.setDiff(false);
135  0 fixedNodeContent.setPatch(patch);
136   
137  0 session.update(fixedNodeContent);
138    }
139   
140  0 versioningStore.setSession(session, context);
141  0 versioningStore.setTransaction(originalTransaction, context);
142    } catch (Exception e) {
143  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
144    XWikiException.ERROR_XWIKI_STORE_MIGRATION, getName() + " migration failed", e);
145    }
146  0 return Boolean.TRUE;
147    }
148    });
149    }
150    }