1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package org.xwiki.watchlist.internal.migration; |
22 |
|
|
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.hibernate.HibernateException; |
31 |
|
import org.hibernate.Query; |
32 |
|
import org.hibernate.Session; |
33 |
|
import org.slf4j.Logger; |
34 |
|
import org.xwiki.component.annotation.Component; |
35 |
|
import org.xwiki.model.reference.LocalDocumentReference; |
36 |
|
|
37 |
|
import com.xpn.xwiki.XWikiContext; |
38 |
|
import com.xpn.xwiki.XWikiException; |
39 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
40 |
|
import com.xpn.xwiki.objects.DBStringListProperty; |
41 |
|
import com.xpn.xwiki.objects.LargeStringProperty; |
42 |
|
import com.xpn.xwiki.objects.PropertyInterface; |
43 |
|
import com.xpn.xwiki.objects.classes.BaseClass; |
44 |
|
import com.xpn.xwiki.objects.classes.DBListClass; |
45 |
|
import com.xpn.xwiki.objects.classes.ListClass; |
46 |
|
import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback; |
47 |
|
import com.xpn.xwiki.store.migration.DataMigrationException; |
48 |
|
import com.xpn.xwiki.store.migration.XWikiDBVersion; |
49 |
|
import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
@version |
56 |
|
|
57 |
|
@Component |
58 |
|
@Named("R70000XWIKI7339") |
59 |
|
@Singleton |
|
|
| 28.6% |
Uncovered Elements: 5 (7) |
Complexity: 3 |
Complexity Density: 0.75 |
|
60 |
|
public class R70000XWIKI7339DataMigration extends AbstractHibernateDataMigration |
61 |
|
{ |
62 |
|
|
63 |
|
|
64 |
|
private static final LocalDocumentReference WATCHLIST_CLASS_REFERENCE = new LocalDocumentReference("XWiki", |
65 |
|
"WatchListClass"); |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@Inject |
71 |
|
private Logger logger; |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
0 |
@Override... |
74 |
|
public String getDescription() |
75 |
|
{ |
76 |
0 |
return "See http://jira.xwiki.org/browse/XWIKI-7339"; |
77 |
|
} |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
3 |
@Override... |
80 |
|
public XWikiDBVersion getVersion() |
81 |
|
{ |
82 |
|
|
83 |
3 |
return new XWikiDBVersion(70000); |
84 |
|
} |
85 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
86 |
0 |
@Override... |
87 |
|
public void hibernateMigrate() throws DataMigrationException, XWikiException |
88 |
|
{ |
89 |
0 |
final XWikiContext context = getXWikiContext(); |
90 |
|
|
91 |
|
|
92 |
0 |
getStore().executeWrite(context, new DoWorkHibernateCallback(context)); |
93 |
|
} |
94 |
|
|
|
|
| 0% |
Uncovered Elements: 44 (44) |
Complexity: 6 |
Complexity Density: 0.17 |
|
95 |
|
private final class DoWorkHibernateCallback implements HibernateCallback<Object> |
96 |
|
{ |
97 |
|
private final XWikiContext context; |
98 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
99 |
0 |
private DoWorkHibernateCallback(XWikiContext context)... |
100 |
|
{ |
101 |
0 |
this.context = context; |
102 |
|
} |
103 |
|
|
|
|
| 0% |
Uncovered Elements: 32 (32) |
Complexity: 3 |
Complexity Density: 0.11 |
|
104 |
0 |
@Override... |
105 |
|
public Object doInHibernate(Session session) throws HibernateException, XWikiException |
106 |
|
{ |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
0 |
XWikiDocument watchListClassDocument = context.getWiki().getDocument(WATCHLIST_CLASS_REFERENCE, context); |
112 |
0 |
if (watchListClassDocument.isNew()) { |
113 |
|
|
114 |
|
|
115 |
0 |
return null; |
116 |
|
} |
117 |
|
|
118 |
0 |
BaseClass watchListClass = watchListClassDocument.getXClass(); |
119 |
0 |
fixClassProperty(watchListClass, "wikis", "Wiki list"); |
120 |
0 |
fixClassProperty(watchListClass, "spaces", "Space list"); |
121 |
0 |
fixClassProperty(watchListClass, "documents", "Document list"); |
122 |
0 |
fixClassProperty(watchListClass, "users", "User list"); |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
0 |
watchListClassDocument.setXClassXML(watchListClass.toXMLString()); |
127 |
0 |
session.save(watchListClassDocument); |
128 |
|
|
129 |
0 |
logger.info("Migrated class document [{}]", watchListClassDocument.getDocumentReference()); |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
0 |
Query q = |
134 |
|
session.createQuery("SELECT ls FROM BaseObject o, LargeStringProperty ls" |
135 |
|
+ " WHERE o.className='XWiki.WatchListClass' AND o.id=ls.id"); |
136 |
|
|
137 |
0 |
@SuppressWarnings("unchecked") |
138 |
|
List<LargeStringProperty> oldProperties = q.list(); |
139 |
0 |
if (oldProperties.size() == 0) { |
140 |
|
|
141 |
0 |
return null; |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
0 |
List<DBStringListProperty> newProperties = new ArrayList<DBStringListProperty>(oldProperties.size()); |
146 |
0 |
for (LargeStringProperty oldProperty : oldProperties) { |
147 |
0 |
DBStringListProperty newProperty = new DBStringListProperty(); |
148 |
0 |
newProperty.setId(oldProperty.getId()); |
149 |
0 |
newProperty.setName(oldProperty.getName()); |
150 |
|
|
151 |
0 |
newProperty.setList(ListClass.getListFromString(oldProperty.getValue(), ",", false)); |
152 |
|
|
153 |
0 |
newProperties.add(newProperty); |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
0 |
for (LargeStringProperty oldProperty : oldProperties) { |
158 |
0 |
session.delete(oldProperty); |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
0 |
for (DBStringListProperty newProperty : newProperties) { |
163 |
0 |
session.save(newProperty); |
164 |
|
} |
165 |
|
|
166 |
0 |
logger.info("Migrated [{}] object properties", oldProperties.size()); |
167 |
|
|
168 |
0 |
return null; |
169 |
|
} |
170 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
171 |
0 |
private void fixClassProperty(BaseClass bclass, String name, String prettyName)... |
172 |
|
{ |
173 |
0 |
PropertyInterface property = bclass.get(name); |
174 |
0 |
if (!(property instanceof DBListClass)) { |
175 |
0 |
bclass.removeField(name); |
176 |
|
|
177 |
0 |
bclass.addDBListField(name, prettyName, 80, true, null); |
178 |
0 |
DBListClass fixedProperty = (DBListClass) bclass.get(name); |
179 |
0 |
fixedProperty.setDisplayType(ListClass.DISPLAYTYPE_INPUT); |
180 |
|
} |
181 |
|
} |
182 |
|
} |
183 |
|
} |