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

File R72001XWIKI12228DataMigration.java

 

Coverage histogram

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

Code metrics

8
39
10
1
187
118
14
0.36
3.9
10
1.4

Classes

Class Line # Actions
R72001XWIKI12228DataMigration 59 39 0% 14 55
0.035087723.5%
 

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.ArrayList;
24    import java.util.Collection;
25    import java.util.HashSet;
26    import java.util.List;
27    import java.util.Set;
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.Query;
35    import org.hibernate.Session;
36    import org.xwiki.component.annotation.Component;
37    import org.xwiki.model.reference.EntityReference;
38    import org.xwiki.model.reference.SpaceReference;
39    import org.xwiki.model.reference.SpaceReferenceResolver;
40    import org.xwiki.model.reference.WikiReference;
41   
42    import com.xpn.xwiki.XWikiException;
43    import com.xpn.xwiki.doc.XWikiSpace;
44    import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback;
45    import com.xpn.xwiki.store.migration.DataMigrationException;
46    import com.xpn.xwiki.store.migration.XWikiDBVersion;
47   
48    /**
49    * Migration for XWIKI-12228: Provide API and probably storage for optimized space related queries.
50    * <p>
51    * Make sure xwikidocument and xwikispace tables are in sync.
52    *
53    * @version $Id: 174d1c4746609e30b08563f91f8ae92ba0371b00 $
54    * @since 7.2M2
55    */
56    @Component
57    @Named("R72001XWIKI12228")
58    @Singleton
 
59    public class R72001XWIKI12228DataMigration extends AbstractHibernateDataMigration
60    {
61    /**
62    * We don't really care what is the wiki since it's only used to go trough XWikiSpace utility methods.
63    */
64    private static final WikiReference WIKI = new WikiReference("wiki");
65   
66    @Inject
67    private SpaceReferenceResolver<String> spaceResolver;
68   
 
69  0 toggle @Override
70    public String getDescription()
71    {
72  0 return "Make sure xwikidocument and xwikispace tables are in sync";
73    }
74   
 
75  206 toggle @Override
76    public XWikiDBVersion getVersion()
77    {
78  206 return new XWikiDBVersion(72001);
79    }
80   
 
81  0 toggle @Override
82    public void hibernateMigrate() throws DataMigrationException, XWikiException
83    {
84  0 getStore().executeWrite(getXWikiContext(), new HibernateCallback<Object>()
85    {
 
86  0 toggle @Override
87    public Object doInHibernate(Session session) throws HibernateException, XWikiException
88    {
89    // Copy visible spaces
90  0 Collection<SpaceReference> visibleSpaces = createVisibleSpaces(session);
91   
92    // Copy hidden spaces
93  0 createHiddenSpaces(visibleSpaces, session);
94   
95  0 return Boolean.TRUE;
96    }
97    });
98    }
99   
 
100  0 toggle private String createSpaceQuery(boolean hidden)
101    {
102  0 StringBuilder query = new StringBuilder("select DISTINCT doc.space from XWikiDocument as doc where");
103  0 if (hidden) {
104  0 query.append(" doc.space not in (" + createSpaceQuery(false) + ")");
105    } else {
106  0 query.append(" doc.hidden <> true OR doc.hidden IS NULL");
107    }
108   
109  0 return query.toString();
110    }
111   
 
112  0 toggle private Collection<SpaceReference> getVisibleSpaces(Session session)
113    {
114  0 Query query = session.createQuery(createSpaceQuery(false));
115   
116  0 Collection<SpaceReference> databaseSpaces = new ArrayList<>();
117  0 for (String space : (List<String>) query.list()) {
118  0 databaseSpaces.add(this.spaceResolver.resolve(space, WIKI));
119    }
120   
121    // Resolve nested spaces
122  0 Set<SpaceReference> spaces = new HashSet<>(databaseSpaces);
123  0 for (SpaceReference space : databaseSpaces) {
124  0 for (EntityReference parent = space.getParent(); parent instanceof SpaceReference; parent =
125    parent.getParent()) {
126  0 spaces.add((SpaceReference) parent);
127    }
128    }
129   
130  0 return spaces;
131    }
132   
 
133  0 toggle private Collection<SpaceReference> getHiddenSpaces(Collection<SpaceReference> visibleSpaces, Session session)
134    {
135  0 Query query = session.createQuery(createSpaceQuery(true));
136   
137  0 Collection<SpaceReference> databaseSpaces = new ArrayList<>();
138  0 for (String space : (List<String>) query.list()) {
139  0 databaseSpaces.add(this.spaceResolver.resolve(space, WIKI));
140    }
141   
142    // Resolve nested spaces
143  0 Set<SpaceReference> spaces = new HashSet<>(databaseSpaces);
144  0 for (SpaceReference space : databaseSpaces) {
145  0 for (EntityReference parent = space.getParent(); parent instanceof SpaceReference; parent =
146    parent.getParent()) {
147  0 if (!visibleSpaces.contains(parent)) {
148  0 spaces.add((SpaceReference) parent);
149    }
150    }
151    }
152   
153  0 return spaces;
154    }
155   
 
156  0 toggle private Collection<SpaceReference> createVisibleSpaces(Session session)
157    {
158    // Get spaces
159  0 Collection<SpaceReference> spaces = getVisibleSpaces(session);
160   
161    // Create spaces
162  0 createSpaces(spaces, false, session);
163   
164  0 return spaces;
165    }
166   
 
167  0 toggle private void createHiddenSpaces(Collection<SpaceReference> visibleSpaces, Session session)
168    {
169    // Get spaces
170  0 Collection<SpaceReference> spaces = getHiddenSpaces(visibleSpaces, session);
171   
172    // Create spaces
173  0 createSpaces(spaces, true, session);
174    }
175   
 
176  0 toggle private void createSpaces(Collection<SpaceReference> spaces, boolean hidden, Session session)
177    {
178    // Create spaces in the xwikispace table
179  0 for (SpaceReference spaceReference : spaces) {
180  0 XWikiSpace space = new XWikiSpace(spaceReference);
181   
182  0 space.setHidden(hidden);
183   
184  0 session.save(space);
185    }
186    }
187    }