]> git.argeo.org Git - gpl/argeo-jcr.git/blob - org.argeo.cms.jcr/src/org/argeo/jackrabbit/JackrabbitDataModelMigration.java
Make basic search more robust
[gpl/argeo-jcr.git] / org.argeo.cms.jcr / src / org / argeo / jackrabbit / JackrabbitDataModelMigration.java
1 package org.argeo.jackrabbit;
2
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.Reader;
6 import java.net.URL;
7
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Session;
10
11 import org.apache.commons.io.IOUtils;
12 import org.apache.jackrabbit.commons.cnd.CndImporter;
13 import org.apache.jackrabbit.commons.cnd.ParseException;
14 import org.apache.jackrabbit.core.config.RepositoryConfig;
15 import org.apache.jackrabbit.core.fs.FileSystemException;
16 import org.argeo.api.cms.CmsLog;
17 import org.argeo.jcr.JcrCallback;
18 import org.argeo.jcr.JcrException;
19 import org.argeo.jcr.JcrUtils;
20
21 /** Migrate the data in a Jackrabbit repository. */
22 @Deprecated
23 public class JackrabbitDataModelMigration implements Comparable<JackrabbitDataModelMigration> {
24 private final static CmsLog log = CmsLog.getLog(JackrabbitDataModelMigration.class);
25
26 private String dataModelNodePath;
27 private String targetVersion;
28 private URL migrationCnd;
29 private JcrCallback dataModification;
30
31 /**
32 * Expects an already started repository with the old data model to migrate.
33 * Expects to be run with admin rights (Repository.login() will be used).
34 *
35 * @return true if a migration was performed and the repository needs to be
36 * restarted and its caches cleared.
37 */
38 public Boolean migrate(Session session) {
39 long begin = System.currentTimeMillis();
40 Reader reader = null;
41 try {
42 // check if already migrated
43 if (!session.itemExists(dataModelNodePath)) {
44 // log.warn("Node " + dataModelNodePath + " does not exist: nothing to migrate.");
45 return false;
46 }
47 // Node dataModelNode = session.getNode(dataModelNodePath);
48 // if (dataModelNode.hasProperty(ArgeoNames.ARGEO_DATA_MODEL_VERSION)) {
49 // String currentVersion = dataModelNode.getProperty(
50 // ArgeoNames.ARGEO_DATA_MODEL_VERSION).getString();
51 // if (compareVersions(currentVersion, targetVersion) >= 0) {
52 // log.info("Data model at version " + currentVersion
53 // + ", no need to migrate.");
54 // return false;
55 // }
56 // }
57
58 // apply transitional CND
59 if (migrationCnd != null) {
60 reader = new InputStreamReader(migrationCnd.openStream());
61 CndImporter.registerNodeTypes(reader, session, true);
62 session.save();
63 // log.info("Registered migration node types from " + migrationCnd);
64 }
65
66 // modify data
67 dataModification.execute(session);
68
69 // apply changes
70 session.save();
71
72 long duration = System.currentTimeMillis() - begin;
73 // log.info("Migration of data model " + dataModelNodePath + " to " + targetVersion + " performed in "
74 // + duration + "ms");
75 return true;
76 } catch (RepositoryException e) {
77 JcrUtils.discardQuietly(session);
78 throw new JcrException("Migration of data model " + dataModelNodePath + " to " + targetVersion + " failed.",
79 e);
80 } catch (ParseException | IOException e) {
81 JcrUtils.discardQuietly(session);
82 throw new RuntimeException(
83 "Migration of data model " + dataModelNodePath + " to " + targetVersion + " failed.", e);
84 } finally {
85 JcrUtils.logoutQuietly(session);
86 IOUtils.closeQuietly(reader);
87 }
88 }
89
90 protected static int compareVersions(String version1, String version2) {
91 // TODO do a proper version analysis and comparison
92 return version1.compareTo(version2);
93 }
94
95 /** To be called on a stopped repository. */
96 public static void clearRepositoryCaches(RepositoryConfig repositoryConfig) {
97 try {
98 String customeNodeTypesPath = "/nodetypes/custom_nodetypes.xml";
99 // FIXME causes weird error in Eclipse
100 repositoryConfig.getFileSystem().deleteFile(customeNodeTypesPath);
101 if (log.isDebugEnabled())
102 log.debug("Cleared " + customeNodeTypesPath);
103 } catch (RuntimeException e) {
104 throw e;
105 } catch (RepositoryException e) {
106 throw new JcrException(e);
107 } catch (FileSystemException e) {
108 throw new RuntimeException("Cannot clear node types cache.",e);
109 }
110
111 // File customNodeTypes = new File(home.getPath()
112 // + "/repository/nodetypes/custom_nodetypes.xml");
113 // if (customNodeTypes.exists()) {
114 // customNodeTypes.delete();
115 // if (log.isDebugEnabled())
116 // log.debug("Cleared " + customNodeTypes);
117 // } else {
118 // log.warn("File " + customNodeTypes + " not found.");
119 // }
120 }
121
122 /*
123 * FOR USE IN (SORTED) SETS
124 */
125
126 public int compareTo(JackrabbitDataModelMigration dataModelMigration) {
127 // TODO make ordering smarter
128 if (dataModelNodePath.equals(dataModelMigration.dataModelNodePath))
129 return compareVersions(targetVersion, dataModelMigration.targetVersion);
130 else
131 return dataModelNodePath.compareTo(dataModelMigration.dataModelNodePath);
132 }
133
134 @Override
135 public boolean equals(Object obj) {
136 if (!(obj instanceof JackrabbitDataModelMigration))
137 return false;
138 JackrabbitDataModelMigration dataModelMigration = (JackrabbitDataModelMigration) obj;
139 return dataModelNodePath.equals(dataModelMigration.dataModelNodePath)
140 && targetVersion.equals(dataModelMigration.targetVersion);
141 }
142
143 @Override
144 public int hashCode() {
145 return targetVersion.hashCode();
146 }
147
148 public void setDataModelNodePath(String dataModelNodePath) {
149 this.dataModelNodePath = dataModelNodePath;
150 }
151
152 public void setTargetVersion(String targetVersion) {
153 this.targetVersion = targetVersion;
154 }
155
156 public void setMigrationCnd(URL migrationCnd) {
157 this.migrationCnd = migrationCnd;
158 }
159
160 public void setDataModification(JcrCallback dataModification) {
161 this.dataModification = dataModification;
162 }
163
164 public String getDataModelNodePath() {
165 return dataModelNodePath;
166 }
167
168 public String getTargetVersion() {
169 return targetVersion;
170 }
171
172 }