use of org.fao.geonet.domain.MetadataResourceVisibility in project core-geonetwork by geonetwork.
the class DefaultResourceUploadHandler method moveFile.
private static void moveFile(final ServiceContext context, final int metadataId, final String fileName, final Path uploadDir, final String access, final String overwrite) throws Exception {
final Store store = context.getBean("resourceStore", Store.class);
final IMetadataUtils metadataUtils = context.getBean(IMetadataUtils.class);
final String uuid = metadataUtils.getMetadataUuid(Integer.toString(metadataId));
Path source = uploadDir.resolve(fileName);
try {
MetadataResourceVisibility visibility = MetadataResourceVisibility.parse(access);
if (overwrite.equals("no") && store.getResourceDescription(context, uuid, visibility, fileName, true) != null) {
throw new Exception("File upload unsuccessful because " + fileName + " already exists and overwrite was not permitted");
}
store.putResource(context, uuid, source, visibility);
} finally {
Files.delete(source);
}
}
use of org.fao.geonet.domain.MetadataResourceVisibility in project core-geonetwork by geonetwork.
the class DefaultResourceUploadHandler method moveFile.
private static void moveFile(final ServiceContext context, final int metadataId, final String fileName, final InputStream is, final String access, final String overwrite) throws Exception {
final Store store = context.getBean("resourceStore", Store.class);
final IMetadataUtils metadataUtils = context.getBean(IMetadataUtils.class);
final String uuid = metadataUtils.getMetadataUuid(Integer.toString(metadataId));
MetadataResourceVisibility visibility = MetadataResourceVisibility.parse(access);
if (overwrite.equals("no") && store.getResourceDescription(context, uuid, visibility, fileName, true) != null) {
throw new Exception("File upload unsuccessful because " + fileName + " already exists and overwrite was not permitted");
}
store.putResource(context, uuid, fileName, is, null, visibility, true);
}
use of org.fao.geonet.domain.MetadataResourceVisibility in project core-geonetwork by geonetwork.
the class Aligner method addMetadata.
private void addMetadata(final RecordInfo ri, final boolean localRating, String uuid) throws Exception {
final String[] id = { null };
final Element[] md = { null };
// --- import metadata from MEF file
Path mefFile = retrieveMEF(ri.uuid);
try {
String fileType = "mef";
MEFLib.Version version = MEFLib.getMEFVersion(mefFile);
if (version != null && version.equals(MEFLib.Version.V2)) {
fileType = "mef2";
}
IVisitor visitor = fileType.equals("mef2") ? new MEF2Visitor() : new MEFVisitor();
MEFLib.visit(mefFile, visitor, new IMEFVisitor() {
public void handleMetadata(Element mdata, int index) throws Exception {
md[index] = mdata;
}
// --------------------------------------------------------------------
public void handleMetadataFiles(DirectoryStream<Path> files, Element info, int index) throws Exception {
// Import valid metadata
Element metadataValidForImport = extractValidMetadataForImport(files, info);
if (metadataValidForImport != null) {
handleMetadata(metadataValidForImport, index);
}
}
// --------------------------------------------------------------------
public void handleInfo(Element info, int index) throws Exception {
final Element metadata = md[index];
String schema = dataMan.autodetectSchema(metadata, null);
if (info != null && info.getContentSize() != 0) {
Element general = info.getChild("general");
if (general != null && general.getContentSize() != 0) {
Element schemaInfo = general.getChild("schema");
if (schemaInfo != null) {
schemaInfo.setText(schema);
}
}
}
if (info != null) {
id[index] = addMetadata(ri, md[index], info, localRating, uuid);
}
}
// --------------------------------------------------------------------
public void handlePublicFile(String file, String changeDate, InputStream is, int index) throws Exception {
handleFile(file, changeDate, is, index, MetadataResourceVisibility.PUBLIC);
}
private void handleFile(String file, String changeDate, InputStream is, int index, MetadataResourceVisibility visibility) throws Exception {
if (id[index] == null)
return;
if (log.isDebugEnabled())
log.debug(" - Adding remote " + visibility + " file with name: " + file);
final Store store = context.getBean("resourceStore", Store.class);
final IMetadataUtils metadataUtils = context.getBean(IMetadataUtils.class);
final String metadataUuid = metadataUtils.getMetadataUuid(id[index]);
store.putResource(context, metadataUuid, file, is, new ISODate(changeDate).toDate(), visibility, true);
}
public void handleFeatureCat(Element md, int index) throws Exception {
// Feature Catalog not managed for harvesting
}
public void handlePrivateFile(String file, String changeDate, InputStream is, int index) throws Exception {
if (params.mefFormatFull) {
handleFile(file, changeDate, is, index, MetadataResourceVisibility.PRIVATE);
}
}
});
} catch (Exception e) {
// --- we ignore the exception here. Maybe the metadata has been removed just now
if (log.isDebugEnabled())
log.debug(" - Skipped unretrievable metadata (maybe has been removed) with uuid:" + ri.uuid);
result.unretrievable++;
log.error(e);
} finally {
try {
Files.deleteIfExists(mefFile);
} catch (IOException e) {
log.warning("Unable to delete mefFile: " + mefFile);
}
}
}
use of org.fao.geonet.domain.MetadataResourceVisibility in project core-geonetwork by geonetwork.
the class CMISStore method delResource.
@Override
public String delResource(final ServiceContext context, final String metadataUuid, final String resourceId, Boolean approved) throws Exception {
int metadataId = canEdit(context, metadataUuid, approved);
for (MetadataResourceVisibility visibility : MetadataResourceVisibility.values()) {
if (tryDelResource(context, metadataUuid, metadataId, visibility, resourceId)) {
Log.info(Geonet.RESOURCES, String.format("MetadataResource '%s' removed.", resourceId));
return String.format("MetadataResource '%s' removed.", resourceId);
}
}
Log.info(Geonet.RESOURCES, String.format("Unable to remove resource '%s'.", resourceId));
return String.format("Unable to remove resource '%s'.", resourceId);
}
use of org.fao.geonet.domain.MetadataResourceVisibility in project core-geonetwork by geonetwork.
the class CMISStore method patchResourceStatus.
@Override
public MetadataResource patchResourceStatus(final ServiceContext context, final String metadataUuid, final String resourceId, final MetadataResourceVisibility visibility, Boolean approved) throws Exception {
int metadataId = canEdit(context, metadataUuid, approved);
// Don't use caching for this process.
OperationContext oc = cmisUtils.createOperationContext();
oc.setCacheEnabled(false);
String sourceKey = null;
CmisObject sourceObject = null;
for (MetadataResourceVisibility sourceVisibility : MetadataResourceVisibility.values()) {
final String key = getKey(context, metadataUuid, metadataId, sourceVisibility, resourceId);
try {
final CmisObject object = cmisConfiguration.getClient().getObjectByPath(key, oc);
if (sourceVisibility != visibility) {
sourceKey = key;
sourceObject = object;
break;
} else {
// already the good visibility
return createResourceDescription(context, metadataUuid, visibility, resourceId, (Document) object, metadataId, approved);
}
} catch (CmisObjectNotFoundException ignored) {
// ignored
}
}
if (sourceKey != null) {
final String destKey = getKey(context, metadataUuid, metadataId, visibility, resourceId);
// Get the parent folder object id.
int lastFolderDelimiterSourceKeyIndex = sourceKey.lastIndexOf(cmisConfiguration.getFolderDelimiter());
String parentSourceKey = sourceKey.substring(0, lastFolderDelimiterSourceKeyIndex);
Folder parentSourceFolder = cmisUtils.getFolderCache(parentSourceKey);
// Get the parent destination folder id.
int lastFolderDelimiterDestKeyIndex = destKey.lastIndexOf(cmisConfiguration.getFolderDelimiter());
String parentDestFolderKey = destKey.substring(0, lastFolderDelimiterDestKeyIndex);
Folder parentDestFolder = cmisUtils.getFolderCache(parentDestFolderKey, true);
// Move the object from source to destination
CmisObject object;
try {
object = ((Document) sourceObject).move(parentSourceFolder, parentDestFolder, oc);
Log.info(Geonet.RESOURCES, String.format("moved resource '%s' to '%s'.", parentSourceFolder.getPaths().get(0), parentDestFolder.getPaths().get(0)));
} catch (CmisPermissionDeniedException e) {
Log.warning(Geonet.RESOURCES, String.format("No permissions to modify metadata resource '%s' for metadata '%s'.", resourceId, metadataUuid));
throw new NotAllowedException(String.format("No permissions to modify metadata resource '%s' for metadata '%s'.", resourceId, metadataUuid));
}
return createResourceDescription(context, metadataUuid, visibility, resourceId, (Document) object, metadataId, approved);
} else {
Log.warning(Geonet.RESOURCES, String.format("Could not update permissions. Metadata resource '%s' not found for metadata '%s'", resourceId, metadataUuid));
throw new ResourceNotFoundException(String.format("Could not update permissions. Metadata resource '%s' not found for metadata '%s'", resourceId, metadataUuid));
}
}
Aggregations