use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRRestAPIUploadHelper method uploadDerivate.
/**
* creates or updates a MyCoRe derivate
* @param info - the Jersey UriInfo object
* @param request - the HTTPServletRequest object
* @param mcrObjID - the MyCoRe Object ID
* @param label - the label of the new derivate
* @param overwriteOnExistingLabel, if true an existing MyCoRe derivate with the given label will be returned
* @return a Jersey Response object
* @throws MCRRestAPIException
*/
public static Response uploadDerivate(UriInfo info, HttpServletRequest request, String mcrObjID, String label, boolean overwriteOnExistingLabel) throws MCRRestAPIException {
Response response = Response.status(Status.INTERNAL_SERVER_ERROR).build();
SignedJWT signedJWT = MCRJSONWebTokenUtil.retrieveAuthenticationToken(request);
// File fXML = null;
MCRObjectID mcrObjIDObj = MCRObjectID.getInstance(mcrObjID);
try (MCRJPATransactionWrapper mtw = new MCRJPATransactionWrapper()) {
MCRSession session = MCRServlet.getSession(request);
MCRUserInformation currentUser = session.getUserInformation();
MCRUserInformation apiUser = MCRUserManager.getUser(MCRJSONWebTokenUtil.retrieveUsernameFromAuthenticationToken(signedJWT));
session.setUserInformation(apiUser);
MCRObject mcrObj = MCRMetadataManager.retrieveMCRObject(mcrObjIDObj);
MCRObjectID derID = null;
if (overwriteOnExistingLabel) {
for (MCRMetaLinkID derLink : mcrObj.getStructure().getDerivates()) {
if (label.equals(derLink.getXLinkLabel()) || label.equals(derLink.getXLinkTitle())) {
derID = derLink.getXLinkHrefID();
}
}
}
if (derID == null) {
derID = MCRObjectID.getNextFreeId(mcrObjIDObj.getProjectId() + "_derivate");
MCRDerivate mcrDerivate = new MCRDerivate();
mcrDerivate.setLabel(label);
mcrDerivate.setId(derID);
mcrDerivate.setSchema("datamodel-derivate.xsd");
mcrDerivate.getDerivate().setLinkMeta(new MCRMetaLinkID("linkmeta", mcrObjIDObj, null, null));
mcrDerivate.getDerivate().setInternals(new MCRMetaIFS("internal", UPLOAD_DIR.resolve(derID.toString()).toString()));
MCRMetadataManager.create(mcrDerivate);
MCRMetadataManager.addOrUpdateDerivateToObject(mcrObjIDObj, new MCRMetaLinkID("derobject", derID, null, label));
}
response = Response.created(info.getBaseUriBuilder().path("v1/objects/" + mcrObjID + "/derivates/" + derID).build()).type("application/xml; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, MCRJSONWebTokenUtil.createJWTAuthorizationHeader(signedJWT)).build();
session.setUserInformation(currentUser);
} catch (Exception e) {
LOGGER.error("Exeption while uploading derivate", e);
}
return response;
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRSolrCommands method rebuildMetadataIndexForObject.
@MCRCommand(syntax = "restricted rebuild solr metadata index for object {0}", help = "rebuilds solr's metadata index for object and all its children", order = 70)
public static void rebuildMetadataIndexForObject(String id) {
MCRObject mcrObject = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(id));
List<MCRObject> objectList = MCRObjectUtils.getDescendantsAndSelf(mcrObject);
List<String> idList = objectList.stream().map(obj -> obj.getId().toString()).collect(Collectors.toList());
MCRSolrIndexer.rebuildMetadataIndex(idList);
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRPersistentIdentifierManagerTest method testRegistrationService.
@Test
public void testRegistrationService() throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
MockMetadataManager mockMetadataManager = new MockMetadataManager();
MCRPIRegistrationService<MCRMockIdentifier> registrationService = MCRPIRegistrationServiceManager.getInstance().getRegistrationService(MOCK_SERVICE);
MCRMockIdentifierRegistrationService casted = (MCRMockIdentifierRegistrationService) registrationService;
Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
Assert.assertFalse("Register should not have been called!", casted.isRegisterCalled());
Assert.assertFalse("Update should not have been called!", casted.isUpdatedCalled());
MCRObject mcrObject = buildMockObject();
mockMetadataManager.put(mcrObject.getId(), mcrObject);
MCRMockIdentifier identifier = registrationService.register(mcrObject, "", true);
Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
Assert.assertTrue("The identifier " + identifier.asString() + " should be registered now!", registrationService.isCreated(mcrObject.getId(), ""));
registrationService.onUpdate(identifier, mcrObject, "");
Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
Assert.assertTrue("The identifier " + identifier.asString() + " should have been updated!", casted.isUpdatedCalled());
registrationService.onDelete(identifier, mcrObject, "");
Assert.assertFalse("The identifier " + identifier.asString() + " should not be registered now!", registrationService.isCreated(mcrObject.getId(), ""));
Assert.assertTrue("There should be one resolver", MCRPersistentIdentifierManager.getInstance().getResolvers().stream().filter(r -> r.getName().equals(MCRMockResolver.NAME)).count() > 0);
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRPersistentIdentifierManagerTest method buildMockObject.
private MCRObject buildMockObject() {
MCRObject mcrObject = new MCRObject();
MCRObjectID id = MCRObjectID.getNextFreeId("test", "mock");
mcrObject.setId(id);
mcrObject.setSchema("http://www.w3.org/2001/XMLSchema");
return mcrObject;
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRPersistentIdentifierManagerTest method testGet.
@Test
public void testGet() throws Exception {
String mockString = MCRMockIdentifier.MOCK_SCHEME + "http://google.de/";
Optional<? extends MCRPersistentIdentifier> mockIdentifierOptional = MCRPersistentIdentifierManager.getInstance().get(mockString).findFirst();
Assert.assertTrue(mockIdentifierOptional.isPresent());
Assert.assertEquals(mockIdentifierOptional.get().asString(), mockString);
// test get(service, id, additional)
MockMetadataManager mockMetadataManager = new MockMetadataManager();
MCRPIRegistrationService<MCRMockIdentifier> registrationService = MCRPIRegistrationServiceManager.getInstance().getRegistrationService(MOCK_SERVICE);
MCRObject mcrObject = buildMockObject();
mockMetadataManager.put(mcrObject.getId(), mcrObject);
registrationService.register(mcrObject, null);
MCRPI mcrpi = MCRPersistentIdentifierManager.getInstance().get(MOCK_SERVICE, mcrObject.getId().toString(), null);
Assert.assertNotNull(mcrpi);
}
Aggregations