001 /*
002 *
003 * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
004 *
005 * This file is part of *** M y C o R e ***
006 * See http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, in a file called gpl.txt or license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 */
023
024 package org.mycore.frontend.cli;
025
026 import java.io.ByteArrayInputStream;
027 import java.util.ArrayList;
028 import java.util.List;
029
030 import org.apache.log4j.Logger;
031 import org.jdom.Document;
032 import org.jdom.Element;
033 import org.jdom.output.XMLOutputter;
034 import org.mycore.common.MCRConfiguration;
035 import org.mycore.datamodel.common.MCRXMLTableManager;
036 import org.mycore.datamodel.ifs.MCRDirectory;
037 import org.mycore.datamodel.ifs.MCRFile;
038 import org.mycore.datamodel.ifs.MCRFilesystemNode;
039 import org.mycore.datamodel.metadata.MCRDerivate;
040 import org.mycore.datamodel.metadata.MCRMetaLinkID;
041 import org.mycore.datamodel.metadata.MCRObject;
042 import org.mycore.datamodel.metadata.MCRObjectID;
043 import org.mycore.frontend.metsmods.MCRMetsModsUtil;
044
045 /**
046 * This class builds a google sitemap containing links to all documents and store them to the webapps directory. This can be configured with property variable MCR.GoogleSitemap.Directory. The web.xml file should contain a mapping to /sitemap.xml See http://www.google.com/webmasters/sitemaps/docs/en/protocol.html
047 *
048 * @author Frank Luetzenkirchen
049 * @author Jens Kupferschmidt
050 * @author Thomas Scheffler (yagee)
051 * @author Stefan Freitag (sasf)
052 * @version $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
053 */
054 public final class MCRMetsModsCommands extends MCRAbstractCommands {
055
056 /** The logger */
057 private static Logger LOGGER = Logger.getLogger(MCRMetsModsCommands.class.getName());
058
059 /**
060 * The empty constructor.
061 */
062 public MCRMetsModsCommands() {
063 super();
064
065 MCRCommand com = null;
066
067 com = new MCRCommand("build mets files with BaseURL {0}", "org.mycore.frontend.cli.MCRMetsModsCommands.buildMets String", "Create the mets.xml file in the derivate directory.");
068 command.add(com);
069
070 com = new MCRCommand("remove mets files","org.mycore.frontend.cli.MCRMetsModsCommands.removeMets","Remove all mets files.");
071 command.add(com);
072
073 com = new MCRCommand("remove mets files from zoomify","org.mycore.frontend.cli.MCRMetsModsCommands.removeMetsByZoomify","Remove all mets files in zoomify derivate directorys.");
074 command.add(com);
075
076 com = new MCRCommand("build mets files for Derivat {0} with BaseURL {1}","org.mycore.frontend.cli.MCRMetsModsCommands.buildMetsForMCRDerivateID String String","Create the mets.xml file in the derivate directory of the given derivate.");
077 command.add(com);
078
079 com = new MCRCommand("build mets files for Object {0} with BaseURL {1}","org.mycore.frontend.cli.MCRMetsModsCommands.buildMetsForMCRObjectID String String","Create the mets.xml file for all dfg-derivate's of the given object.");
080 command.add(com);
081 }
082
083 /**
084 * Remove mets.xml files from all derivates.
085 *
086 * @throws Exception
087 */
088 public static final void removeMets() throws Exception {
089 LOGGER.debug("Remove METS file start.");
090 final long start = System.currentTimeMillis();
091 List<String> derlist = MCRXMLTableManager.instance().retrieveAllIDs("derivate");
092 for (String der : derlist) {
093 MCRDirectory difs = MCRDirectory.getRootDirectory(der);
094 if (difs != null) {
095 MCRFilesystemNode mets = difs.getChild("mets.xml");
096 if (mets != null) {
097 LOGGER.info("Mets file found on "+der);
098 mets.delete();
099 }
100 }
101 }
102 LOGGER.debug("Remove METS file request took " + (System.currentTimeMillis() - start) + "ms.");
103 }
104
105 /**
106 * Remove mets.xml files from all derivates typed by zoomify.
107 *
108 * @throws Exception
109 */
110 public static final void removeMetsByZoomify() throws Exception {
111 LOGGER.debug("Remove METS file from zoomify derivates start.");
112 final long start = System.currentTimeMillis();
113 List<String> derlist = MCRXMLTableManager.instance().retrieveAllIDs("derivate");
114 for (String der : derlist) {
115 MCRDirectory difs = MCRDirectory.getRootDirectory(der);
116 if (difs != null) {
117 MCRFilesystemNode mets = difs.getChild("mets.xml");
118
119 MCRFilesystemNode l[] = difs.getChildren();
120
121 boolean zipfound = false;
122 for(int i=0;i<l.length;i++)
123 if(l[i].getName().contains(".zip")) {zipfound=true;break;}
124
125 if (mets != null && zipfound) {
126 LOGGER.info("Mets file found on "+der);
127 mets.delete();
128 }
129 }
130 }
131 LOGGER.debug("Remove METS file from zoomify derivates request took " + (System.currentTimeMillis() - start) + "ms.");
132 }
133
134 public static final void buildMetsForMCRObjectID(String MCRID, String baseurl)
135 {
136 LOGGER.debug("Build METS file start.");
137 final long start = System.currentTimeMillis();
138 MCRObject mcrobj = new MCRObject();
139 mcrobj.receiveFromDatastore(MCRID);
140 for(int i=0;i<mcrobj.getStructure().getDerivateSize();i++)
141 {
142 MCRMetaLinkID mcrder = mcrobj.getStructure().getDerivate(i);
143 LOGGER.debug("found derivate "+ mcrder.getXLinkTitle());
144 String label = mcrder.getXLinkLabel();
145 if(label.contains("dfg"))
146 buildMetsForMCRDerivateID(mcrder.getXLinkHrefID().toString(),baseurl);
147 }
148 LOGGER.debug("Build METS file request took " + (System.currentTimeMillis() - start) + "ms.");
149 }
150
151 public static final void buildMetsForMCRDerivateID(String MCRID, String baseurl)
152 {
153 LOGGER.debug("Build METS file start.");
154 final long start = System.currentTimeMillis();
155 MCRDerivate derxml = new MCRDerivate();
156 derxml.receiveFromDatastore(MCRID);
157 MCRObjectID docid = derxml.getDerivate().getMetaLink().getXLinkHrefID();
158 MCRDirectory difs = MCRDirectory.getRootDirectory(MCRID);
159 if (difs != null) {
160 MCRFilesystemNode mets = difs.getChild("mets.xml");
161 if (mets == null) {
162 LOGGER.debug("No mets.xml file was found for "+MCRID+".");
163 MCRFilesystemNode[] fsnode = difs.getChildren();
164 boolean checkimage = false;
165 for (int i = 0; i < fsnode.length; i++) {
166 if (fsnode[i].getName().endsWith(".jpg")) {
167 checkimage = true;
168 } else {
169 checkimage = false;
170 break;
171 }
172 }
173
174 if (checkimage) {
175
176 LOGGER.info("Build mets.xml for derivate "+MCRID+" with MyCoRe-Object-ID: "+docid);
177
178 ArrayList<String> pic_list = new ArrayList<String>();
179 addPicturesToList(difs, pic_list);
180
181 String project = docid.getProjectId();
182 MCRConfiguration CONFIG = MCRConfiguration.instance();
183 // owner
184 String owner = CONFIG.getString("MCR.Component.MetsMods." + project + ".owner", "");
185 if (owner.trim().length() == 0) {
186 owner = CONFIG.getString("MCR.Component.MetsMods.owner", "");
187 }
188 // logo
189 String ownerLogo = CONFIG.getString("MCR.Component.MetsMods." + project + ".ownerLogo", "");
190 if (ownerLogo.trim().length() == 0) {
191 ownerLogo = CONFIG.getString("MCR.Component.MetsMods.ownerLogo", "");
192 }
193 // site url
194 String ownerSiteURL = CONFIG.getString("MCR.Component.MetsMods." + project + ".ownerSiteURL", "");
195 if (ownerSiteURL.trim().length() == 0) {
196 ownerSiteURL = CONFIG.getString("MCR.Component.MetsMods.ownerSiteURL", "");
197 }
198 // reference url
199 String referenceURL = CONFIG.getString("MCR.Component.MetsMods." + project + ".referenceURL", "");
200 if (referenceURL.trim().length() == 0) {
201 referenceURL = CONFIG.getString("MCR.Component.MetsMods.referenceURL", "");
202 }
203 // presentation url
204 String presentationURL = CONFIG.getString("MCR.Component.MetsMods." + project + ".presentationURL", "");
205 if (presentationURL.trim().length() == 0) {
206 presentationURL = CONFIG.getString("MCR.Component.MetsMods.presentationURL", "");
207 }
208
209 MCRMetsModsUtil mmu = new MCRMetsModsUtil();
210
211 Element new_mets = mmu.init_mets(MCRID);
212 Element amdSec = mmu.init_amdSec(MCRID, owner, ownerLogo, ownerSiteURL, referenceURL, presentationURL);
213
214 new_mets.addContent(amdSec);
215
216 Element mets2 = mmu.createMetsElement(pic_list, new_mets, baseurl + "servlets/MCRFileNodeServlet");
217
218 XMLOutputter xmlout = new XMLOutputter();
219 String full_mets = xmlout.outputString(mets2);
220
221 // save the builded file to IFS
222 try {
223 LOGGER.debug("storing new mets file...");
224 // startTransaction();
225 MCRFile file = new MCRFile("mets.xml", difs);
226 // commitTransaction();
227 ByteArrayInputStream bais = new ByteArrayInputStream(full_mets.getBytes());
228 long sizeDiff = file.setContentFrom(bais, false);
229
230 file.storeContentChange(sizeDiff);
231
232
233 } catch (Exception e) {
234 LOGGER.error("Error while storing new mets file...", e);
235 }
236
237
238 }
239 }
240 }
241
242
243 LOGGER.debug("Build METS file request took " + (System.currentTimeMillis() - start) + "ms.");
244 }
245
246 /**
247 * The build mets.xml files in the derivates if they does not exist and the content are images.
248 */
249 public static final void buildMets(String baseurl) throws Exception {
250 // check time
251 LOGGER.debug("Build METS file start.");
252 final long start = System.currentTimeMillis();
253 // get all derivates
254 List<String> derlist = MCRXMLTableManager.instance().retrieveAllIDs("derivate");
255 for (String der : derlist) {
256 // get metadata MCRObjectID
257 MCRDerivate derxml = new MCRDerivate();
258 derxml.receiveFromDatastore(der);
259 MCRObjectID docid = derxml.getDerivate().getMetaLink().getXLinkHrefID();
260 // receive the IFS informations
261 MCRDirectory difs = MCRDirectory.getRootDirectory(der);
262 if (difs != null) {
263 MCRFilesystemNode mets = difs.getChild("mets.xml");
264 if (mets == null) {
265 LOGGER.debug("No mets.xml file was found for "+der+".");
266 MCRFilesystemNode[] fsnode = difs.getChildren();
267 boolean checkimage = false;
268 for (int i = 0; i < fsnode.length; i++) {
269 if (fsnode[i].getName().endsWith(".jpg")) {
270 checkimage = true;
271 } else {
272 checkimage = false;
273 break;
274 }
275 }
276 if (checkimage) {
277
278 LOGGER.info("Build mets.xml for derivate "+der+" with MyCoRe-Object-ID: "+docid);
279
280 ArrayList<String> pic_list = new ArrayList<String>();
281 addPicturesToList(difs, pic_list);
282
283 String project = docid.getProjectId();
284 MCRConfiguration CONFIG = MCRConfiguration.instance();
285 // owner
286 String owner = CONFIG.getString("MCR.Component.MetsMods." + project + ".owner", "");
287 if (owner.trim().length() == 0) {
288 owner = CONFIG.getString("MCR.Component.MetsMods.owner", "");
289 }
290 // logo
291 String ownerLogo = CONFIG.getString("MCR.Component.MetsMods." + project + ".ownerLogo", "");
292 if (ownerLogo.trim().length() == 0) {
293 ownerLogo = CONFIG.getString("MCR.Component.MetsMods.ownerLogo", "");
294 }
295 // site url
296 String ownerSiteURL = CONFIG.getString("MCR.Component.MetsMods." + project + ".ownerSiteURL", "");
297 if (ownerSiteURL.trim().length() == 0) {
298 ownerSiteURL = CONFIG.getString("MCR.Component.MetsMods.ownerSiteURL", "");
299 }
300 // reference url
301 String referenceURL = CONFIG.getString("MCR.Component.MetsMods." + project + ".referenceURL", "");
302 if (referenceURL.trim().length() == 0) {
303 referenceURL = CONFIG.getString("MCR.Component.MetsMods.referenceURL", "");
304 }
305 // presentation url
306 String presentationURL = CONFIG.getString("MCR.Component.MetsMods." + project + ".presentationURL", "");
307 if (presentationURL.trim().length() == 0) {
308 presentationURL = CONFIG.getString("MCR.Component.MetsMods.presentationURL", "");
309 }
310
311 MCRMetsModsUtil mmu = new MCRMetsModsUtil();
312
313 Element new_mets = mmu.init_mets(der);
314 Element amdSec = mmu.init_amdSec(der, owner, ownerLogo, ownerSiteURL, referenceURL, presentationURL);
315
316 new_mets.addContent(amdSec);
317
318 Element mets2 = mmu.createMetsElement(pic_list, new_mets, baseurl + "servlets/MCRFileNodeServlet");
319
320 XMLOutputter xmlout = new XMLOutputter();
321 String full_mets = xmlout.outputString(mets2);
322
323 // save the builded file to IFS
324 try {
325 LOGGER.debug("storing new mets file...");
326 // startTransaction();
327 MCRFile file = new MCRFile("mets.xml", difs);
328 // commitTransaction();
329 ByteArrayInputStream bais = new ByteArrayInputStream(full_mets.getBytes());
330 long sizeDiff = file.setContentFrom(bais, false);
331
332 file.storeContentChange(sizeDiff);
333
334
335 } catch (Exception e) {
336 LOGGER.error("Error while storing new mets file...", e);
337 }
338
339
340 }
341 }
342 }
343 }
344
345 // check time
346 LOGGER.debug("Build METS file request took " + (System.currentTimeMillis() - start) + "ms.");
347 }
348
349 private static void addPicturesToList(MCRDirectory dir, ArrayList<String> list) {
350 for (int i = 0; i < dir.getChildren().length; i++) {
351 try {
352 dir = (MCRDirectory) dir.getChildren()[i];
353 addPicturesToList(dir, list);
354 } catch (Exception ClassCastException) {
355 if (!list.contains(dir.getPath() + "/" + ((MCRFile) dir.getChildren()[i]).getName()))
356 list.add(dir.getPath() + "/" + ((MCRFile) dir.getChildren()[i]).getName());
357 }
358 }
359 }
360 }