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

File R73000XWIKI12277DataMigration.java

 

Coverage histogram

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

Code metrics

2
29
7
1
128
80
8
0.28
4.14
7
1.14

Classes

Class Line # Actions
R73000XWIKI12277DataMigration 52 29 0% 8 36
0.052631585.3%
 

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.List;
24   
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.hibernate.HibernateException;
29    import org.hibernate.Query;
30    import org.hibernate.Session;
31    import org.xwiki.component.annotation.Component;
32   
33    import com.xpn.xwiki.XWikiException;
34    import com.xpn.xwiki.objects.BaseObject;
35    import com.xpn.xwiki.objects.IntegerProperty;
36    import com.xpn.xwiki.objects.StringProperty;
37    import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback;
38    import com.xpn.xwiki.store.migration.DataMigrationException;
39    import com.xpn.xwiki.store.migration.XWikiDBVersion;
40   
41    /**
42    * Migration for XWIKI-12277: Remove the 'type' xproperty from TemplateProviderClass
43    * <p>
44    * Migrate TemplateProviderClass' removed 'type' property values to the new 'terminal' property.
45    *
46    * @version $Id: eb62b007a7df549ce923b9c1a4382243ce52421c $
47    * @since 7.3RC1
48    */
49    @Component
50    @Named("R73000XWIKI12277")
51    @Singleton
 
52    public class R73000XWIKI12277DataMigration extends AbstractHibernateDataMigration
53    {
 
54  0 toggle @Override
55    public String getDescription()
56    {
57  0 return "Migrate TemplateProviderClass' removed 'type' property values to the new 'terminal' property.";
58    }
59   
 
60  206 toggle @Override
61    public XWikiDBVersion getVersion()
62    {
63  206 return new XWikiDBVersion(73000);
64    }
65   
 
66  0 toggle @Override
67    public void hibernateMigrate() throws DataMigrationException, XWikiException
68    {
69  0 getStore().executeWrite(getXWikiContext(), new HibernateCallback<Object>()
70    {
 
71  0 toggle @Override
72    public Object doInHibernate(Session session) throws HibernateException, XWikiException
73    {
74  0 return doWork(session);
75    }
76   
77    });
78    }
79   
 
80  0 toggle private Object doWork(Session session) throws HibernateException
81    {
82  0 Query query = session.createQuery(createQueryString());
83   
84  0 List results = query.list();
85  0 for (Object result : results) {
86  0 Object[] resultLine = (Object[]) result;
87  0 BaseObject object = (BaseObject) resultLine[0];
88  0 StringProperty typeProperty = (StringProperty) resultLine[1];
89   
90    // Migrate each property.
91  0 migrateProperty(typeProperty, object, session);
92    }
93   
94  0 return Boolean.TRUE;
95    }
96   
 
97  0 toggle private String createQueryString()
98    {
99  0 StringBuilder query = new StringBuilder();
100  0 query.append("SELECT templateProviderObj, typeProp ");
101  0 query.append("FROM BaseObject templateProviderObj, StringProperty typeProp ");
102  0 query.append("WHERE templateProviderObj.className='XWiki.TemplateProviderClass'");
103  0 query.append(" AND templateProviderObj.name<>'XWiki.TemplateProviderTemplate'");
104  0 query.append(" AND typeProp.id.id=templateProviderObj.id");
105  0 query.append(" AND typeProp.name='type'");
106   
107  0 return query.toString();
108    }
109   
 
110  0 toggle private void migrateProperty(StringProperty typeProperty, BaseObject object, Session session)
111    throws HibernateException
112    {
113    // Create the new property value and assign it to the owning object.
114  0 IntegerProperty terminalProperty = new IntegerProperty();
115  0 int value = 1;
116  0 if ("space".equals(typeProperty.getValue())) {
117  0 value = 0;
118    }
119  0 terminalProperty.setValue(value);
120  0 terminalProperty.setName("terminal");
121  0 terminalProperty.setObject(object);
122   
123    // Save the new property.
124  0 session.saveOrUpdate(terminalProperty);
125    // Delete the old property.
126  0 session.delete(typeProperty);
127    }
128    }