use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRURNGranularRESTRegistrationService method registerURN.
private MCRDNBURN registerURN(MCRDerivate deriv, String filePath) {
MCRObjectID derivID = deriv.getId();
Function<String, Integer> countCreatedPI = s -> MCRPersistentIdentifierManager.getInstance().getCreatedIdentifiers(derivID, getType(), getRegistrationServiceID()).size();
int seed = Optional.of(filePath).filter(p -> !"".equals(p)).map(countCreatedPI).map(count -> count + 1).orElse(1);
MCRDNBURN derivURN = Optional.ofNullable(deriv.getDerivate()).map(MCRObjectDerivate::getURN).flatMap(new MCRDNBURNParser()::parse).orElseGet(() -> createNewURN(deriv));
String setID = derivID.getNumberAsString();
GranularURNGenerator granularURNGen = new GranularURNGenerator(seed, derivURN, setID);
Function<MCRPath, Supplier<String>> generateURN = p -> granularURNGen.getURNSupplier();
derivateFileStream.apply(deriv).filter(notInIgnoreList().and(matchFile(filePath))).sorted().collect(Collectors.toMap(generateURN, p -> p, (m1, m2) -> m1, LinkedHashMap::new)).forEach(createFileMetadata(deriv).andThen(persistURN(deriv)));
try {
MCRMetadataManager.update(deriv);
} catch (MCRPersistenceException | MCRAccessException e) {
LOGGER.error("Error while updating derivate {}", derivID, e);
}
EntityTransaction transaction = MCREntityManagerProvider.getCurrentEntityManager().getTransaction();
if (!transaction.isActive()) {
transaction.begin();
}
transaction.commit();
return derivURN;
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRAVExtRealHelix method init.
@Override
public void init(MCRFile file) throws MCRPersistenceException {
super.init(file);
MCRConfiguration config = MCRConfiguration.instance();
String prefix = "MCR.IFS.AVExtender." + file.getStoreID() + ".";
baseMetadata = config.getString(prefix + "ViewSourceBaseURL");
try {
String data = getMetadata(baseMetadata + file.getStorageID());
String sSize = getBetween("File Size:</strong>", "Bytes", data, "0");
String sBitRate = getBetween("Bit Rate:</strong>", "Kbps", data, "0.0");
String sFrameRate = getBetween("Frame Rate: </strong>", "fps", data, "0.0");
String sDuration = getBetween("Duration:</strong>", "<br>", data, "0:0.0");
String sType = getBetween("Stream:</strong>", "<br>", data, "");
bitRate = Math.round(1024 * Float.valueOf(sBitRate));
StringTokenizer st1 = new StringTokenizer(sFrameRate, " ,");
while (st1.hasMoreTokens()) {
double value = Double.valueOf(st1.nextToken());
frameRate = Math.max(frameRate, value);
}
hasVideo = frameRate > 0;
StringTokenizer st2 = new StringTokenizer(sDuration, ":.");
durationMinutes = Integer.parseInt(st2.nextToken());
durationSeconds = Integer.parseInt(st2.nextToken());
if (Integer.parseInt(st2.nextToken()) > 499) {
durationSeconds += 1;
if (durationSeconds > 59) {
durationMinutes += 1;
durationSeconds = 0;
}
}
StringTokenizer st3 = new StringTokenizer(sSize, ",");
StringBuilder sb = new StringBuilder();
while (st3.hasMoreTokens()) {
sb.append(st3.nextToken());
}
size = Long.parseLong(sb.toString());
durationHours = durationMinutes / 60;
durationMinutes = durationMinutes - durationHours * 60;
if (sType.contains("MPEG Layer 3")) {
contentTypeID = "mp3";
hasVideo = false;
} else if (sType.contains("3GPP")) {
contentTypeID = "3gp";
hasVideo = true;
} else if (sType.contains("MPEG4")) {
contentTypeID = "mpeg4";
hasVideo = true;
} else if (sType.contains("MPEG")) {
contentTypeID = "mpegvid";
hasVideo = true;
} else if (sType.contains("RealVideo")) {
contentTypeID = "realvid";
} else if (sType.contains("RealAudio")) {
contentTypeID = "realaud";
} else if (sType.contains("Wave File")) {
contentTypeID = "wav";
hasVideo = false;
} else // should be one of "wma" "wmv" "asf"
{
contentTypeID = file.getContentTypeID();
hasVideo = !contentTypeID.equals("wma");
}
if (" wma wmv asf asx ".contains(" " + contentTypeID + " ")) {
basePlayerStarter = config.getString(prefix + "AsxGenBaseURL");
playerDownloadURL = config.getString(prefix + "MediaPlayerURL");
} else {
basePlayerStarter = config.getString(prefix + "RamGenBaseURL");
playerDownloadURL = config.getString(prefix + "RealPlayerURL");
}
URLConnection con = getConnection(basePlayerStarter + file.getStorageID());
playerStarterCT = con.getContentType();
} catch (Exception exc) {
String msg = "Error parsing metadata from Real Server ViewSource: " + file.getStorageID();
LOGGER.warn(msg, exc);
}
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRAudioVideoExtender method getMetadata.
/**
* Helper method that connects to the given URL and returns the response as
* a String
*
* @param url
* the URL to connect to
* @return the response content as a String
*/
protected String getMetadata(String url) throws MCRPersistenceException {
try {
URLConnection connection = getConnection(url);
connection.setConnectTimeout(getConnectTimeout());
String contentType = connection.getContentType();
// defined by RFC 2616 (sec 3.7.1)
Charset charset = StandardCharsets.ISO_8859_1;
if (contentType != null) {
MediaType mediaType = MediaType.parse(contentType);
mediaType.charset().or(StandardCharsets.ISO_8859_1);
}
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
forwardData(connection, out);
return new String(out.toByteArray(), charset);
} catch (IOException exc) {
String msg = "Could not get metadata from Audio/Video Store URL: " + url;
throw new MCRPersistenceException(msg, exc);
}
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRUpdateDerivateServlet method updateDerivateXML.
/**
* Updates derivate xml in the persistence backend
* @param editorSubmission
* MyCoRe derivate as XML
* @return
* MCRObjectID of the MyCoRe object
* @throws SAXParseException
* @throws MCRAccessException
*/
private MCRObjectID updateDerivateXML(Document editorSubmission) throws SAXParseException, IOException, MCRAccessException {
MCRObjectID objectID;
Element root = editorSubmission.getRootElement();
root.setAttribute("noNamespaceSchemaLocation", "datamodel-derivate.xsd", XSI_NAMESPACE);
root.addNamespaceDeclaration(XLINK_NAMESPACE);
root.addNamespaceDeclaration(XSI_NAMESPACE);
byte[] xml = new MCRJDOMContent(editorSubmission).asByteArray();
MCRDerivate der = new MCRDerivate(xml, true);
MCRObjectID derivateID = der.getId();
// store entry of derivate xlink:title in object
objectID = der.getDerivate().getMetaLink().getXLinkHrefID();
MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
MCRObjectStructure structure = obj.getStructure();
MCRMetaLinkID linkID = structure.getDerivateLink(derivateID);
linkID.setXLinkTitle(der.getLabel());
try {
MCRMetadataManager.update(obj);
MCRMetadataManager.update(der);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Can't store label of derivate " + derivateID + " in derivate list of object " + objectID + ".", e);
}
return objectID;
}
use of org.mycore.common.MCRPersistenceException in project mycore by MyCoRe-Org.
the class MCRDFGLinkServlet method getOrderNumber.
private static int getOrderNumber(Document metsDoc, String fileHref) {
int orderNumber = -1;
String fileID = null;
try {
Mets mets = new Mets(metsDoc);
List<FileGrp> fileGroups = mets.getFileSec().getFileGroups();
for (FileGrp fileGrp : fileGroups) {
List<File> fileList = fileGrp.getFileList();
for (File file : fileList) {
FLocat fLocat = file.getFLocat();
if (fLocat.getHref().equals(MCRXMLFunctions.encodeURIPath(fileHref)))
fileID = file.getId();
}
}
if (fileID != null) {
PhysicalStructMap structMap = (PhysicalStructMap) mets.getStructMap(PhysicalStructMap.TYPE);
PhysicalDiv rootDiv = structMap.getDivContainer();
List<PhysicalSubDiv> children = rootDiv.getChildren();
for (int index = 0; index < children.size(); index++) {
PhysicalSubDiv physicalSubDiv = children.get(index);
List<Fptr> fptrList = physicalSubDiv.getChildren();
for (Fptr fptr : fptrList) {
if (fptr.getFileId().equals(fileID))
orderNumber = index + 1;
}
}
}
} catch (Exception e) {
throw new MCRPersistenceException("could not parse mets.xml", e);
}
return orderNumber;
}
Aggregations