Search in sources :

Example 1 with MCRMetsSimpleModel

use of org.mycore.mets.model.simple.MCRMetsSimpleModel in project mycore by MyCoRe-Org.

the class MCRJSONSimpleModelConverterTest method testToSimpleModel.

@Test
public void testToSimpleModel() throws Exception {
    MCRMetsSimpleModel metsSimpleModel = MCRJSONSimpleModelConverter.toSimpleModel(json);
    MCRMetsSimpleModel compareSimpleModel = MCRMetsTestUtil.buildMetsSimpleModel();
    MCRMetsSection s1RootSection = metsSimpleModel.getRootSection();
    MCRMetsSection s2RootSection = compareSimpleModel.getRootSection();
    Assert.assertEquals("labels of root must be the same", s1RootSection.getLabel(), s2RootSection.getLabel());
    Assert.assertEquals("types of root must be the same", s1RootSection.getType(), s2RootSection.getType());
    Assert.assertEquals("page count must be the same", metsSimpleModel.getMetsPageList().size(), compareSimpleModel.getMetsPageList().size());
    List<MCRMetsLink> s1SectionPageLinkList = metsSimpleModel.getSectionPageLinkList();
    List<MCRMetsLink> s2SectionPageLinkList = compareSimpleModel.getSectionPageLinkList();
    for (int n = 0; n < 3; n++) {
        Assert.assertEquals("from of " + n + " link must be the same", s1SectionPageLinkList.get(n).getFrom().getLabel(), s2SectionPageLinkList.get(n).getFrom().getLabel());
        Assert.assertEquals("to of " + n + " link must be the same", s1SectionPageLinkList.get(n).getTo().getOrderLabel(), s2SectionPageLinkList.get(n).getTo().getOrderLabel());
    }
}
Also used : MCRMetsLink(org.mycore.mets.model.simple.MCRMetsLink) MCRMetsSimpleModel(org.mycore.mets.model.simple.MCRMetsSimpleModel) MCRMetsSection(org.mycore.mets.model.simple.MCRMetsSection) Test(org.junit.Test)

Example 2 with MCRMetsSimpleModel

use of org.mycore.mets.model.simple.MCRMetsSimpleModel in project mycore by MyCoRe-Org.

the class MetsResource method save.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/crud/{derivateId}")
public String save(@PathParam("derivateId") String derivateId, String data) {
    MCRObjectID derivateIdObject = MCRObjectID.getInstance(derivateId);
    checkDerivateExists(derivateIdObject);
    checkDerivateAccess(derivateIdObject, MCRAccessManager.PERMISSION_WRITE);
    MCRMetsSimpleModel model = MCRJSONSimpleModelConverter.toSimpleModel(data);
    Document document = MCRSimpleModelXMLConverter.toXML(model);
    XMLOutputter o = new XMLOutputter();
    try (OutputStream out = Files.newOutputStream(MCRPath.getPath(derivateId, METS_XML_PATH), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
        o.output(document, out);
    } catch (IOException e) {
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
    return "{ \"success\": true }";
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) WebApplicationException(javax.ws.rs.WebApplicationException) MCRMetsSimpleModel(org.mycore.mets.model.simple.MCRMetsSimpleModel) OutputStream(java.io.OutputStream) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) Document(org.jdom2.Document) Path(javax.ws.rs.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 3 with MCRMetsSimpleModel

use of org.mycore.mets.model.simple.MCRMetsSimpleModel in project mycore by MyCoRe-Org.

the class MCRMetsCommands method fixInvalidMets.

@MCRCommand(syntax = "try fix invalid mets", help = "This Command can be used to fix invalid mets files that was found in any validate selected mets runs.", order = 15)
public static void fixInvalidMets() {
    String selectedObjectID;
    while ((selectedObjectID = invalidMetsQueue.poll()) != null) {
        LOGGER.info("Try to fix METS of {}", selectedObjectID);
        MCRPath metsFile = MCRPath.getPath(selectedObjectID, "/mets.xml");
        SAXBuilder saxBuilder = new SAXBuilder();
        Document metsDocument;
        try (InputStream metsInputStream = Files.newInputStream(metsFile)) {
            metsDocument = saxBuilder.build(metsInputStream);
        } catch (IOException | JDOMException e) {
            LOGGER.error(MessageFormat.format("Cannot fix METS of {0}. Can not parse mets.xml!", selectedObjectID), e);
            return;
        }
        MCRMetsSimpleModel mcrMetsSimpleModel;
        try {
            mcrMetsSimpleModel = MCRXMLSimpleModelConverter.fromXML(metsDocument);
        } catch (Exception e) {
            LOGGER.error(MessageFormat.format("Cannot fix METS of {0}. Can not convert to SimpleModel!", selectedObjectID), e);
            return;
        }
        Document newMets = MCRSimpleModelXMLConverter.toXML(mcrMetsSimpleModel);
        XMLOutputter xmlOutputter = new XMLOutputter();
        try (OutputStream os = Files.newOutputStream(metsFile)) {
            xmlOutputter.output(newMets, os);
        } catch (IOException e) {
            LOGGER.error(MessageFormat.format("Cannot fix METS of {0}. Can not write mets to derivate.", selectedObjectID));
        }
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) SAXBuilder(org.jdom2.input.SAXBuilder) InputStream(java.io.InputStream) MCRMetsSimpleModel(org.mycore.mets.model.simple.MCRMetsSimpleModel) OutputStream(java.io.OutputStream) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) ValidationException(org.mycore.mets.validator.validators.ValidationException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 4 with MCRMetsSimpleModel

use of org.mycore.mets.model.simple.MCRMetsSimpleModel in project mycore by MyCoRe-Org.

the class MCRJSONSimpleModelConverter method toSimpleModel.

public static MCRMetsSimpleModel toSimpleModel(String model) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(MCRMetsLink.class, new MCRMetsLinkTypeAdapter());
    gsonBuilder.registerTypeAdapter(MCRMetsAltoLink.class, new MCRAltoLinkTypeAdapter());
    gsonBuilder.setPrettyPrinting();
    MCRMetsSimpleModel metsSimpleModel = gsonBuilder.create().fromJson(model, MCRMetsSimpleModel.class);
    Hashtable<String, MCRMetsPage> idPageMap = new Hashtable<>();
    metsSimpleModel.getMetsPageList().stream().forEach(page -> idPageMap.put(page.getId(), page));
    final Map<String, MCRMetsFile> idMCRMetsFileMap = extractIdFileMap(metsSimpleModel.getMetsPageList());
    Hashtable<String, MCRMetsSection> idSectionMap = new Hashtable<>();
    processSections(metsSimpleModel.getRootSection(), idSectionMap, idMCRMetsFileMap);
    List<MCRMetsLink> sectionPageLinkList = metsSimpleModel.getSectionPageLinkList();
    List<MCRMetsLink> metsLinks = sectionPageLinkList.stream().map((link) -> {
        if (link instanceof MCRMetsLinkTypeAdapter.MCRMetsLinkPlaceholder) {
            MCRMetsLinkTypeAdapter.MCRMetsLinkPlaceholder placeholder = (MCRMetsLinkTypeAdapter.MCRMetsLinkPlaceholder) link;
            MCRMetsSection metsSection = idSectionMap.get(placeholder.getFromString());
            MCRMetsPage metsPage = idPageMap.get(placeholder.getToString());
            return new MCRMetsLink(metsSection, metsPage);
        } else {
            return link;
        }
    }).collect(toList());
    sectionPageLinkList.clear();
    sectionPageLinkList.addAll(metsLinks);
    return metsSimpleModel;
}
Also used : MCRMetsPage(org.mycore.mets.model.simple.MCRMetsPage) MCRException(org.mycore.common.MCRException) GsonBuilder(com.google.gson.GsonBuilder) MCRMetsFile(org.mycore.mets.model.simple.MCRMetsFile) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Map(java.util.Map) MCRMetsAltoLink(org.mycore.mets.model.simple.MCRMetsAltoLink) MCRMetsFileUse(org.mycore.mets.model.simple.MCRMetsFileUse) MCRMetsSection(org.mycore.mets.model.simple.MCRMetsSection) MCRMetsSimpleModel(org.mycore.mets.model.simple.MCRMetsSimpleModel) Hashtable(java.util.Hashtable) MCRMetsLink(org.mycore.mets.model.simple.MCRMetsLink) GsonBuilder(com.google.gson.GsonBuilder) MCRMetsLink(org.mycore.mets.model.simple.MCRMetsLink) Hashtable(java.util.Hashtable) MCRMetsPage(org.mycore.mets.model.simple.MCRMetsPage) MCRMetsFile(org.mycore.mets.model.simple.MCRMetsFile) MCRMetsSection(org.mycore.mets.model.simple.MCRMetsSection) MCRMetsSimpleModel(org.mycore.mets.model.simple.MCRMetsSimpleModel)

Example 5 with MCRMetsSimpleModel

use of org.mycore.mets.model.simple.MCRMetsSimpleModel in project mycore by MyCoRe-Org.

the class MCRXMLSimpleModelConverter method fromXML.

/**
 * Converts a Document to MetsSimpleModel
 *
 * @param metsDocument the Document which should be converted
 * @return the converted MetsSimpleModel
 * @throws Exception if new Mets(metsDocument) throws exception
 */
public static MCRMetsSimpleModel fromXML(Document metsDocument) throws Exception {
    Mets mets = new Mets(metsDocument);
    MCRMetsSimpleModel msm = new MCRMetsSimpleModel();
    Map<String, MCRMetsPage> idPageMap = new Hashtable<>();
    Map<String, MCRMetsFile> idFileMap = buildidFileMap(mets);
    List<MCRMetsPage> metsPageList = buildPageList(mets, idPageMap, idFileMap);
    msm.getMetsPageList().addAll(metsPageList);
    Map<String, MCRMetsSection> idSectionMap = new Hashtable<>();
    MCRMetsSection rootMetsSection = buidRootSection(mets, idSectionMap, idFileMap);
    msm.setRootSection(rootMetsSection);
    linkPages(mets, idSectionMap, idPageMap, msm);
    return msm;
}
Also used : Mets(org.mycore.mets.model.Mets) MCRMetsSimpleModel(org.mycore.mets.model.simple.MCRMetsSimpleModel) Hashtable(java.util.Hashtable) MCRMetsPage(org.mycore.mets.model.simple.MCRMetsPage) MCRMetsFile(org.mycore.mets.model.simple.MCRMetsFile) MCRMetsSection(org.mycore.mets.model.simple.MCRMetsSection)

Aggregations

MCRMetsSimpleModel (org.mycore.mets.model.simple.MCRMetsSimpleModel)7 MCRMetsSection (org.mycore.mets.model.simple.MCRMetsSection)4 Hashtable (java.util.Hashtable)3 Document (org.jdom2.Document)3 MCRMetsPage (org.mycore.mets.model.simple.MCRMetsPage)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 List (java.util.List)2 Map (java.util.Map)2 XMLOutputter (org.jdom2.output.XMLOutputter)2 MCRException (org.mycore.common.MCRException)2 MCRPath (org.mycore.datamodel.niofs.MCRPath)2 Mets (org.mycore.mets.model.Mets)2 MCRMetsFile (org.mycore.mets.model.simple.MCRMetsFile)2 MCRMetsFileUse (org.mycore.mets.model.simple.MCRMetsFileUse)2 MCRMetsLink (org.mycore.mets.model.simple.MCRMetsLink)2 GsonBuilder (com.google.gson.GsonBuilder)1 InputStream (java.io.InputStream)1 UUID (java.util.UUID)1 Collectors.toList (java.util.stream.Collectors.toList)1