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.sql.PreparedStatement; |
24 |
|
import java.sql.ResultSet; |
25 |
|
import java.sql.SQLException; |
26 |
|
import java.sql.Statement; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Named; |
30 |
|
import javax.inject.Singleton; |
31 |
|
|
32 |
|
import org.hibernate.HibernateException; |
33 |
|
import org.hibernate.Session; |
34 |
|
import org.hibernate.Transaction; |
35 |
|
import org.slf4j.Logger; |
36 |
|
import org.xwiki.component.annotation.Component; |
37 |
|
import org.xwiki.component.manager.ComponentLookupException; |
38 |
|
|
39 |
|
import com.xpn.xwiki.XWikiContext; |
40 |
|
import com.xpn.xwiki.XWikiException; |
41 |
|
import com.xpn.xwiki.doc.XWikiDocumentArchive; |
42 |
|
import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback; |
43 |
|
import com.xpn.xwiki.store.XWikiHibernateVersioningStore; |
44 |
|
import com.xpn.xwiki.store.XWikiVersioningStoreInterface; |
45 |
|
import com.xpn.xwiki.store.migration.DataMigrationException; |
46 |
|
import com.xpn.xwiki.store.migration.XWikiDBVersion; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
@version |
52 |
|
|
53 |
|
@Component |
54 |
|
@Named("R4359XWIKI1459") |
55 |
|
@Singleton |
|
|
| 4.1% |
Uncovered Elements: 47 (49) |
Complexity: 12 |
Complexity Density: 0.32 |
|
56 |
|
public class R4359XWIKI1459DataMigration extends AbstractHibernateDataMigration |
57 |
|
{ |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@Inject |
62 |
|
private Logger logger; |
63 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
0 |
@Override... |
65 |
|
public String getDescription() |
66 |
|
{ |
67 |
0 |
return "See http://jira.xwiki.org/jira/browse/XWIKI-1459"; |
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
70 |
206 |
@Override... |
71 |
|
public XWikiDBVersion getVersion() |
72 |
|
{ |
73 |
206 |
return new XWikiDBVersion(4359); |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
@return |
78 |
|
@throws |
79 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
80 |
0 |
private XWikiHibernateVersioningStore getVersioningStore() throws XWikiException... |
81 |
|
{ |
82 |
0 |
try { |
83 |
0 |
return (XWikiHibernateVersioningStore) this.componentManager |
84 |
|
.getInstance(XWikiVersioningStoreInterface.class, "hibernate"); |
85 |
|
} catch (ComponentLookupException e) { |
86 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
87 |
|
XWikiException.ERROR_XWIKI_STORE_MIGRATION, |
88 |
|
String.format("Unable to reach the versioning store for database %s", getXWikiContext().getWikiId()), |
89 |
|
e); |
90 |
|
} |
91 |
|
} |
92 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
93 |
0 |
@Override... |
94 |
|
public void hibernateMigrate() throws DataMigrationException, XWikiException |
95 |
|
{ |
96 |
|
|
97 |
0 |
getStore().executeWrite(getXWikiContext(), true, new HibernateCallback<Object>() |
98 |
|
{ |
|
|
| 0% |
Uncovered Elements: 38 (38) |
Complexity: 7 |
Complexity Density: 0.22 |
|
99 |
0 |
@Override... |
100 |
|
public Object doInHibernate(Session session) throws HibernateException, XWikiException |
101 |
|
{ |
102 |
0 |
try { |
103 |
0 |
Statement stmt = session.connection().createStatement(); |
104 |
0 |
ResultSet rs; |
105 |
0 |
try { |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
0 |
rs = |
113 |
|
stmt |
114 |
|
.executeQuery( |
115 |
|
"select XWD_ID, XWD_ARCHIVE, XWD_FULLNAME from xwikidoc" |
116 |
|
+ " where (XWD_ARCHIVE is not null and XWD_ARCHIVE <> ' ') order by XWD_VERSION"); |
117 |
|
} catch (SQLException e) { |
118 |
|
|
119 |
|
|
120 |
0 |
return null; |
121 |
|
} |
122 |
|
|
123 |
0 |
XWikiContext context = getXWikiContext(); |
124 |
0 |
XWikiHibernateVersioningStore versioningStore = getVersioningStore(); |
125 |
0 |
Transaction originalTransaction = versioningStore.getTransaction(context); |
126 |
0 |
versioningStore.setSession(null, context); |
127 |
0 |
versioningStore.setTransaction(null, context); |
128 |
0 |
PreparedStatement deleteStatement = |
129 |
|
session.connection().prepareStatement("update xwikidoc set XWD_ARCHIVE=' ' where XWD_ID=?"); |
130 |
|
|
131 |
0 |
while (rs.next()) { |
132 |
0 |
if (R4359XWIKI1459DataMigration.this.logger.isInfoEnabled()) { |
133 |
0 |
R4359XWIKI1459DataMigration.this.logger.info("Updating document [{}]...", rs.getString(3)); |
134 |
|
} |
135 |
0 |
long docId = Long.parseLong(rs.getString(1)); |
136 |
0 |
String sArchive = rs.getString(2); |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
0 |
if (sArchive.trim().length() != 0) { |
142 |
0 |
XWikiDocumentArchive docArchive = new XWikiDocumentArchive(docId); |
143 |
0 |
try { |
144 |
0 |
docArchive.setArchive(sArchive); |
145 |
|
} catch (XWikiException e) { |
146 |
0 |
R4359XWIKI1459DataMigration.this.logger.warn( |
147 |
|
"The RCS archive for [{}] is broken. Internal error [{}]." |
148 |
|
+ " The history for this document has been reset.", |
149 |
|
rs.getString(3), e.getMessage()); |
150 |
|
} |
151 |
0 |
getVersioningStore().saveXWikiDocArchive(docArchive, true, context); |
152 |
|
} else { |
153 |
0 |
R4359XWIKI1459DataMigration.this.logger.warn( |
154 |
|
"Empty revision found for document [{}]. Ignoring non-fatal error.", |
155 |
|
rs.getString(3)); |
156 |
|
} |
157 |
0 |
deleteStatement.setLong(1, docId); |
158 |
0 |
deleteStatement.executeUpdate(); |
159 |
|
} |
160 |
0 |
deleteStatement.close(); |
161 |
0 |
stmt.close(); |
162 |
0 |
versioningStore.setSession(session, context); |
163 |
0 |
versioningStore.setTransaction(originalTransaction, context); |
164 |
|
} catch (SQLException e) { |
165 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
166 |
|
XWikiException.ERROR_XWIKI_STORE_MIGRATION, getName() + " migration failed", e); |
167 |
|
} |
168 |
|
|
169 |
0 |
return Boolean.TRUE; |
170 |
|
} |
171 |
|
}); |
172 |
|
} |
173 |
|
} |