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

File R72000XWIKI12153DataMigration.java

 

Coverage histogram

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

Code metrics

4
20
7
2
130
84
15
0.75
2.86
3.5
2.14

Classes

Class Line # Actions
R72000XWIKI12153DataMigration 57 15 0% 10 23
0.088%
R72000XWIKI12153DataMigration.R72000Work 88 5 0% 5 6
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 com.xpn.xwiki.store.migration.hibernate;
22   
23    import java.sql.Connection;
24    import java.sql.PreparedStatement;
25    import java.sql.ResultSet;
26    import java.sql.SQLException;
27    import java.sql.Statement;
28   
29    import javax.inject.Inject;
30    import javax.inject.Named;
31    import javax.inject.Singleton;
32   
33    import org.hibernate.HibernateException;
34    import org.hibernate.Session;
35    import org.hibernate.jdbc.Work;
36    import org.xwiki.component.annotation.Component;
37    import org.xwiki.model.EntityType;
38    import org.xwiki.model.reference.EntityReference;
39    import org.xwiki.model.reference.EntityReferenceSerializer;
40   
41    import com.xpn.xwiki.XWikiException;
42    import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback;
43    import com.xpn.xwiki.store.migration.DataMigrationException;
44    import com.xpn.xwiki.store.migration.XWikiDBVersion;
45   
46    /**
47    * Migration for XWIKI-12153: Implement nested spaces support at database level.
48    * <p>
49    * Convert existing values in documents space field from single space name to complete space local reference.
50    *
51    * @version $Id: a39c33698af81f5a04eb267662812938524412f2 $
52    * @since 7.2M1
53    */
54    @Component
55    @Named("R72000XWIKI12153")
56    @Singleton
 
57    public class R72000XWIKI12153DataMigration extends AbstractHibernateDataMigration
58    {
59    @Inject
60    private EntityReferenceSerializer<String> serializer;
61   
 
62  0 toggle @Override
63    public String getDescription()
64    {
65  0 return "Convert document space name into space reference";
66    }
67   
 
68  206 toggle @Override
69    public XWikiDBVersion getVersion()
70    {
71  206 return new XWikiDBVersion(72000);
72    }
73   
 
74  0 toggle @Override
75    public void hibernateMigrate() throws DataMigrationException, XWikiException
76    {
77  0 getStore().executeWrite(getXWikiContext(), new HibernateCallback<Object>()
78    {
 
79  0 toggle @Override
80    public Object doInHibernate(Session session) throws HibernateException, XWikiException
81    {
82  0 session.doWork(new R72000Work());
83  0 return Boolean.TRUE;
84    }
85    });
86    }
87   
 
88    private class R72000Work implements Work
89    {
 
90  0 toggle @Override
91    public void execute(Connection connection) throws SQLException
92    {
93    // Search for all document spaces that should be escaped
94  0 try (Statement selectStatement = connection.createStatement()) {
95  0 try (ResultSet result =
96    selectStatement.executeQuery("select DISTINCT XWD_WEB from xwikidoc"
97    + " where XWD_WEB like '%.%' OR XWD_WEB like '%\\\\%' OR XWD_WEB like '%:%'")) {
98  0 convert(connection, result);
99    }
100    }
101    }
102    }
103   
 
104  0 toggle private void convert(Connection connection, ResultSet result) throws SQLException
105    {
106  0 if (result.next()) {
107  0 try (PreparedStatement statement =
108    connection.prepareStatement("UPDATE xwikidoc set XWD_WEB = ? WHERE XWD_WEB = ?")) {
109  0 do {
110  0 addBatch(statement, result.getString(1));
111  0 } while (result.next());
112   
113    // Do all the changes
114  0 statement.executeBatch();
115    }
116    }
117    }
118   
 
119  0 toggle private void addBatch(PreparedStatement statement, String spaceName) throws SQLException
120    {
121    // Convert the space name into a space reference
122  0 String spaceReference = this.serializer.serialize(new EntityReference(spaceName, EntityType.SPACE));
123   
124  0 statement.setString(1, spaceReference);
125  0 statement.setString(2, spaceName);
126   
127    // Add a conversion to the list
128  0 statement.addBatch();
129    }
130    }