1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
|
56 |
|
@Component |
57 |
|
@Named("R72001XWIKI12228") |
58 |
|
@Singleton |
|
|
| 3.5% |
Uncovered Elements: 55 (57) |
Complexity: 14 |
Complexity Density: 0.36 |
|
59 |
|
public class R72001XWIKI12228DataMigration extends AbstractHibernateDataMigration |
60 |
|
{ |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private static final WikiReference WIKI = new WikiReference("wiki"); |
65 |
|
|
66 |
|
@Inject |
67 |
|
private SpaceReferenceResolver<String> spaceResolver; |
68 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
69 |
0 |
@Override... |
70 |
|
public String getDescription() |
71 |
|
{ |
72 |
0 |
return "Make sure xwikidocument and xwikispace tables are in sync"; |
73 |
|
} |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
206 |
@Override... |
76 |
|
public XWikiDBVersion getVersion() |
77 |
|
{ |
78 |
206 |
return new XWikiDBVersion(72001); |
79 |
|
} |
80 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
81 |
0 |
@Override... |
82 |
|
public void hibernateMigrate() throws DataMigrationException, XWikiException |
83 |
|
{ |
84 |
0 |
getStore().executeWrite(getXWikiContext(), new HibernateCallback<Object>() |
85 |
|
{ |
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
86 |
0 |
@Override... |
87 |
|
public Object doInHibernate(Session session) throws HibernateException, XWikiException |
88 |
|
{ |
89 |
|
|
90 |
0 |
Collection<SpaceReference> visibleSpaces = createVisibleSpaces(session); |
91 |
|
|
92 |
|
|
93 |
0 |
createHiddenSpaces(visibleSpaces, session); |
94 |
|
|
95 |
0 |
return Boolean.TRUE; |
96 |
|
} |
97 |
|
}); |
98 |
|
} |
99 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
100 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
112 |
0 |
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 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
133 |
0 |
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 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
156 |
0 |
private Collection<SpaceReference> createVisibleSpaces(Session session)... |
157 |
|
{ |
158 |
|
|
159 |
0 |
Collection<SpaceReference> spaces = getVisibleSpaces(session); |
160 |
|
|
161 |
|
|
162 |
0 |
createSpaces(spaces, false, session); |
163 |
|
|
164 |
0 |
return spaces; |
165 |
|
} |
166 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
167 |
0 |
private void createHiddenSpaces(Collection<SpaceReference> visibleSpaces, Session session)... |
168 |
|
{ |
169 |
|
|
170 |
0 |
Collection<SpaceReference> spaces = getHiddenSpaces(visibleSpaces, session); |
171 |
|
|
172 |
|
|
173 |
0 |
createSpaces(spaces, true, session); |
174 |
|
} |
175 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
176 |
0 |
private void createSpaces(Collection<SpaceReference> spaces, boolean hidden, Session session)... |
177 |
|
{ |
178 |
|
|
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 |
|
} |