001 package org.mycore.services.migration;
002
003 import java.net.URISyntaxException;
004 import java.util.ArrayList;
005 import java.util.Collections;
006 import java.util.LinkedList;
007 import java.util.List;
008
009 import org.apache.log4j.Logger;
010 import org.jdom.JDOMException;
011 import org.mycore.access.MCRAccessManager;
012 import org.mycore.common.MCRException;
013 import org.mycore.datamodel.common.MCRXMLTableManager;
014 import org.mycore.datamodel.metadata.MCRDerivate;
015 import org.mycore.datamodel.metadata.MCRObject;
016 import org.mycore.datamodel.metadata.MCRObjectID;
017 import org.mycore.frontend.cli.MCRAbstractCommands;
018 import org.mycore.frontend.cli.MCRCommand;
019
020 public class MCRMigrationCommands extends MCRAbstractCommands {
021 private static Logger LOGGER = Logger.getLogger(MCRMigrationCommands.class);
022
023 public MCRMigrationCommands() {
024 MCRCommand com = null;
025
026 com = new MCRCommand("migrate user", "org.mycore.services.migration.MCRMigrationCommands.migrateUser",
027 "The command migrates the user management to MyCoRe 2.0.");
028 command.add(com);
029 com = new MCRCommand("internal usermigration step {0}", "org.mycore.services.migration.MCRMigrationCommands.migrateUser int",
030 "Internal commands for user migration");
031 command.add(com);
032 com = new MCRCommand("migrate mcraccess", "org.mycore.services.migration.MCRMigrationCommands.migrateAccess",
033 "The command migrates the access system to MyCoRe 2.0.");
034 command.add(com);
035 com = new MCRCommand("internal accessmigration step {0}", "org.mycore.services.migration.MCRMigrationCommands.migrateAccess int",
036 "Internal commands for access system migration");
037 command.add(com);
038 com = new MCRCommand("migrate classifications", "org.mycore.services.migration.MCRMigrationCommands.migrateClassifications",
039 "Migrates the version 1 classification system to the current version");
040 command.add(com);
041 com = new MCRCommand("internal classificationmigration step {0}",
042 "org.mycore.services.migration.MCRMigrationCommands.migrateClassifications int",
043 "Internal commands for classification migration");
044 command.add(com);
045 com = new MCRCommand("migrate MCRXMLTABLE", "org.mycore.services.migration.MCRMigrationCommands.migrateMCRXMLTable",
046 "Migrates of the MCRXMLTable layout to the 2.0 format");
047 command.add(com);
048 com = new MCRCommand("internal migrate MCRXMLTABLE entry {0}",
049 "org.mycore.services.migration.MCRMigrationCommands.migrateMCRXMLTable String",
050 "Internal command for the migration of the MCRXMLTable layout to the 2.0 format");
051 command.add(com);
052 }
053
054 public static List<String> migrateClassifications() throws JDOMException {
055 List<String> cmds = new ArrayList<String>();
056 for (int i = 1; i < 4; i++) {
057 cmds.add("internal classificationmigration step " + i);
058 cmds.add("internal classificationmigration step " + i);
059 }
060 return cmds;
061 }
062
063 public static void migrateClassifications(int step) throws JDOMException, URISyntaxException {
064 switch (step) {
065 case 1:
066 MCRClassificationMigrationHelper.createCategories();
067 break;
068 case 2:
069 MCRClassificationMigrationHelper.migrateCategoryLinks();
070 break;
071 case 3:
072 MCRClassificationMigrationHelper.deleteOldCategoryLinks();
073 break;
074 default:
075 throw new MCRException("Classification migration step " + step + " is unknown.");
076 }
077 }
078
079 public static List<String> migrateUser() {
080 List<String> cmds = new ArrayList<String>();
081 LOGGER.info("User Migration started\n");
082 final int userMigrationSteps = 11;
083 for (int i = 1; i <= userMigrationSteps; i++) {
084 cmds.add("internal usermigration step " + i);
085 }
086 return cmds;
087 }
088
089 public static List<String> migrateUser(int step) throws Exception {
090 switch (step) {
091 case 1:
092 return Collections.nCopies(1, "export all groups to file " + MCRUserMigrationHelper.getGroupFile().getAbsolutePath());
093 case 2:
094 return Collections.nCopies(1, "export all users to file " + MCRUserMigrationHelper.getUserFile().getAbsolutePath());
095 case 3:
096 MCRUserMigrationHelper.dropTables();
097 return Collections.emptyList();
098 case 4:
099 return Collections.nCopies(1, "init hibernate");
100 case 5:
101 return Collections.nCopies(1, "init superuser");
102 case 6:
103 MCRUserMigrationHelper.cleanupGroupFile();
104 return Collections.emptyList();
105 case 7:
106 MCRUserMigrationHelper.cleanupUserFile();
107 return Collections.emptyList();
108 case 8:
109 ArrayList<String> groupImportCommands = new ArrayList<String>(2);
110 //check if super user has the right to modify group (delete permission - if not)
111 if (!MCRAccessManager.checkPermission("modify-group"))
112 groupImportCommands.add("delete permission modify-group for id POOLPRIVILEGE");
113 groupImportCommands.add("import group data from file " + MCRUserMigrationHelper.getGroupFile().getAbsolutePath());
114 return groupImportCommands;
115 case 9:
116 ArrayList<String> userImportCommands = new ArrayList<String>(2);
117 //check if super user has the right to modify user (delete permission - if not)
118 if (!MCRAccessManager.checkPermission("modify-user"))
119 userImportCommands.add("delete permission modify-user for id POOLPRIVILEGE");
120 userImportCommands.add("import user data from file " + MCRUserMigrationHelper.getUserFile().getAbsolutePath());
121 return userImportCommands;
122 case 10:
123 MCRUserMigrationHelper.updateAdmins();
124 return Collections.emptyList();
125 case 11:
126 MCRUserMigrationHelper.deleteTempFiles();
127 return Collections.emptyList();
128 default:
129 throw new MCRException("User migration step " + step + " is unknown.");
130 }
131 }
132
133 public static List<String> migrateAccess() {
134 List<String> cmds = new ArrayList<String>();
135 LOGGER.info("MCRAccess Migration started\n");
136 final int userMigrationSteps = 5;
137 for (int i = 1; i <= userMigrationSteps; i++) {
138 cmds.add("internal accessmigration step " + i);
139 }
140 return cmds;
141 }
142
143 public static List<String> migrateAccess(int step) throws Exception {
144 switch (step) {
145 case 1:
146 return Collections.nCopies(1, "export acl mappings to file " + MCRAccessMigrationHelper.getExportFile().getAbsolutePath());
147 case 2:
148 MCRAccessMigrationHelper.dropTable();
149 return Collections.emptyList();
150 case 3:
151 return Collections.nCopies(1, "init hibernate");
152 case 4:
153 return Collections.nCopies(1, "import acl mappings from file " + MCRAccessMigrationHelper.getExportFile().getAbsolutePath());
154 case 5:
155 MCRAccessMigrationHelper.deleteExportFile();
156 return Collections.emptyList();
157 default:
158 throw new MCRException("MCRACCESS migration step " + step + " is unknown.");
159 }
160 }
161
162 public static List<String> migrateMCRXMLTable() {
163 MCRXMLTableManager tm = MCRXMLTableManager.instance();
164 LinkedList<String> commands = new LinkedList<String>();
165 for (String id : tm.retrieveAllIDs()) {
166 commands.add("internal migrate MCRXMLTABLE entry " + id);
167 }
168 return commands;
169 }
170
171 /**
172 * Migrates the MCRXMLTABLE to the version with last modified attribute.
173 *
174 * @param objectID
175 * the objectID to be migrated
176 * @throws Exception
177 */
178 public static void migrateMCRXMLTable(String objectID) throws Exception {
179 MCRObjectID oid = new MCRObjectID(objectID);
180 if (objectID.indexOf("_derivate_") > -1) {
181 MCRDerivate der=new MCRDerivate();
182 der.receiveFromDatastore(oid);
183 der.setImportMode(true);
184 der.updateXMLInDatastore();
185 } else {
186 MCRObject obj = new MCRObject();
187 obj.receiveFromDatastore(oid);
188 obj.setImportMode(true);
189 obj.updateInDatastore();
190 }
191 }
192
193 }