diff --git a/core/src/main/java/org/fao/geonet/kernel/datamanager/base/BaseMetadataManager.java b/core/src/main/java/org/fao/geonet/kernel/datamanager/base/BaseMetadataManager.java index d368f6a1982..136ef971912 100644 --- a/core/src/main/java/org/fao/geonet/kernel/datamanager/base/BaseMetadataManager.java +++ b/core/src/main/java/org/fao/geonet/kernel/datamanager/base/BaseMetadataManager.java @@ -1,5 +1,5 @@ //============================================================================= -//=== Copyright (C) 2001-2011 Food and Agriculture Organization of the +//=== Copyright (C) 2001-2020 Food and Agriculture Organization of the //=== United Nations (FAO-UN), United Nations World Food Programme (WFP) //=== and United Nations Environment Programme (UNEP) //=== @@ -23,32 +23,21 @@ package org.fao.geonet.kernel.datamanager.base; -import static org.springframework.data.jpa.domain.Specifications.where; - -import java.nio.file.Files; -import java.nio.file.Path; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.ResourceBundle; -import java.util.Set; -import java.util.UUID; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import javax.annotation.PostConstruct; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.criteria.Root; -import javax.transaction.Transactional; - +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.collect.Collections2; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Maps; +import com.google.common.collect.SetMultimap; +import com.google.common.collect.Sets; +import jeeves.constants.Jeeves; +import jeeves.server.ServiceConfig; +import jeeves.server.UserSession; +import jeeves.server.context.ServiceContext; +import jeeves.transaction.TransactionManager; +import jeeves.transaction.TransactionTask; +import jeeves.xlink.Processor; import org.apache.commons.lang.StringUtils; import org.fao.geonet.ApplicationContextHolder; import org.fao.geonet.constants.Edit; @@ -132,22 +121,30 @@ import org.springframework.data.jpa.domain.Specification; import org.springframework.transaction.TransactionStatus; -import com.google.common.base.Function; -import com.google.common.base.Optional; -import com.google.common.base.Predicate; -import com.google.common.collect.Collections2; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Maps; -import com.google.common.collect.SetMultimap; -import com.google.common.collect.Sets; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.PostConstruct; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.criteria.Root; +import javax.transaction.Transactional; +import java.nio.file.Files; +import java.nio.file.Path; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.ResourceBundle; +import java.util.Set; +import java.util.UUID; -import jeeves.constants.Jeeves; -import jeeves.server.ServiceConfig; -import jeeves.server.UserSession; -import jeeves.server.context.ServiceContext; -import jeeves.transaction.TransactionManager; -import jeeves.transaction.TransactionTask; -import jeeves.xlink.Processor; +import static org.springframework.data.jpa.domain.Specifications.where; public class BaseMetadataManager implements IMetadataManager { @@ -241,8 +238,8 @@ public void init(ServiceContext context, Boolean force) throws Exception { Sort sortByMetadataChangeDate = SortUtils.createSort(Metadata_.dataInfo, MetadataDataInfo_.changeDate); int currentPage = 0; - Page> results = metadataUtils.findAllIdsAndChangeDates( - new PageRequest(currentPage, METADATA_BATCH_PAGE_SIZE, sortByMetadataChangeDate)); + Page> results = metadataUtils + .findAllIdsAndChangeDates(new PageRequest(currentPage, METADATA_BATCH_PAGE_SIZE, sortByMetadataChangeDate)); // index all metadata in DBMS if needed while (results.getNumberOfElements() > 0) { @@ -278,8 +275,8 @@ public void init(ServiceContext context, Boolean force) throws Exception { } currentPage++; - results = metadataRepository.findAllIdsAndChangeDates( - new PageRequest(currentPage, METADATA_BATCH_PAGE_SIZE, sortByMetadataChangeDate)); + results = metadataRepository + .findAllIdsAndChangeDates(new PageRequest(currentPage, METADATA_BATCH_PAGE_SIZE, sortByMetadataChangeDate)); } // if anything to index then schedule it to be done after servlet is @@ -312,8 +309,8 @@ protected SearchManager getSearchManager() { @Deprecated public void flush() { TransactionManager.runInTransaction("DataManager flush()", getApplicationContext(), - TransactionManager.TransactionRequirement.CREATE_ONLY_WHEN_NEEDED, - TransactionManager.CommitBehavior.ALWAYS_COMMIT, false, new TransactionTask() { + TransactionManager.TransactionRequirement.CREATE_ONLY_WHEN_NEEDED, TransactionManager.CommitBehavior.ALWAYS_COMMIT, false, + new TransactionTask() { @Override public Object doInTransaction(TransactionStatus transaction) throws Throwable { _entityManager.flush(); @@ -356,17 +353,15 @@ public javax.persistence.criteria.Path getPath(Root } }; - metadataFileUploadRepository.createBatchUpdateQuery(deletedDatePathSpec, new ISODate().toString(), - MetadataFileUploadSpecs.isNotDeletedForMetadata(intId)); + metadataFileUploadRepository + .createBatchUpdateQuery(deletedDatePathSpec, new ISODate().toString(), MetadataFileUploadSpecs.isNotDeletedForMetadata(intId)); // --- remove metadata getXmlSerializer().delete(id, context); } - private MetaSearcher searcherForReferencingMetadata(ServiceContext context, AbstractMetadata metadata) - throws Exception { - MetaSearcher searcher = context.getBean(SearchManager.class).newSearcher(SearcherType.LUCENE, - Geonet.File.SEARCH_LUCENE); + private MetaSearcher searcherForReferencingMetadata(ServiceContext context, AbstractMetadata metadata) throws Exception { + MetaSearcher searcher = context.getBean(SearchManager.class).newSearcher(SearcherType.LUCENE, Geonet.File.SEARCH_LUCENE); Element parameters = new Element(Jeeves.Elem.REQUEST); parameters.addContent(new Element(Geonet.IndexFieldNames.XLINK).addContent("*" + metadata.getUuid() + "*")); parameters.addContent(new Element(Geonet.SearchResult.BUILD_SUMMARY).setText("false")); @@ -430,10 +425,10 @@ public void deleteMetadataGroup(ServiceContext context, String metadataId) throw */ @Override public String createMetadata(ServiceContext context, String templateId, String groupOwner, String source, int owner, - String parentUuid, String isTemplate, boolean fullRightsForGroup) throws Exception { + String parentUuid, String isTemplate, boolean fullRightsForGroup) throws Exception { - return createMetadata(context, templateId, groupOwner, source, owner, parentUuid, isTemplate, - fullRightsForGroup, UUID.randomUUID().toString()); + return createMetadata(context, templateId, groupOwner, source, owner, parentUuid, isTemplate, fullRightsForGroup, + UUID.randomUUID().toString()); } /** @@ -445,7 +440,7 @@ public String createMetadata(ServiceContext context, String templateId, String g */ @Override public String createMetadata(ServiceContext context, String templateId, String groupOwner, String source, int owner, - String parentUuid, String isTemplate, boolean fullRightsForGroup, String uuid) throws Exception { + String parentUuid, String isTemplate, boolean fullRightsForGroup, String uuid) throws Exception { AbstractMetadata templateMetadata = metadataUtils.findOne(templateId); if (templateMetadata == null) { throw new IllegalArgumentException("Template id not found : " + templateId); @@ -460,8 +455,7 @@ public String createMetadata(ServiceContext context, String templateId, String g boolean isMetadata = templateMetadata.getDataInfo().getType() == MetadataType.METADATA; setMetadataTitle(schema, xml, context.getLanguage(), !isMetadata); if (isMetadata) { - xml = updateFixedInfo(schema, Optional.absent(), uuid, xml, parentUuid, UpdateDatestamp.NO, - context); + xml = updateFixedInfo(schema, Optional.absent(), uuid, xml, parentUuid, UpdateDatestamp.NO, context); } final Metadata newMetadata = new Metadata(); @@ -475,8 +469,8 @@ public String createMetadata(ServiceContext context, String templateId, String g if (group.getDefaultCategory() != null) { newMetadata.getMetadataCategories().add(group.getDefaultCategory()); } - Collection filteredCategories = Collections2.filter(templateMetadata.getCategories(), - new Predicate() { + Collection filteredCategories = Collections2 + .filter(templateMetadata.getCategories(), new Predicate() { @Override public boolean apply(@Nullable MetadataCategory input) { return input != null; @@ -485,8 +479,7 @@ public boolean apply(@Nullable MetadataCategory input) { newMetadata.getMetadataCategories().addAll(filteredCategories); - int finalId = insertMetadata(context, newMetadata, xml, false, true, true, UpdateDatestamp.YES, - fullRightsForGroup, true).getId(); + int finalId = insertMetadata(context, newMetadata, xml, false, true, true, UpdateDatestamp.YES, fullRightsForGroup, true).getId(); return String.valueOf(finalId); } @@ -518,16 +511,14 @@ private void setMetadataTitle(String schema, Element xml, String language, boole for (Object o : titleNodes) { if (o instanceof Element) { Element title = (Element) o; - title.setText(String.format( - messages.getString( - "metadata.title.createdFrom" + (fromTemplate ? "Template" : "Record")), - title.getTextTrim(), - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))); + title.setText(String + .format(messages.getString("metadata.title.createdFrom" + (fromTemplate ? "Template" : "Record")), + title.getTextTrim(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))); } } } catch (JDOMException e) { - LOGGER_DATA_MANAGER.debug("Check xpath '{}' for schema plugin '{}'. Error is '{}'.", - new Object[]{path, schema, e.getMessage()}); + LOGGER_DATA_MANAGER + .debug("Check xpath '{}' for schema plugin '{}'. Error is '{}'.", new Object[] { path, schema, e.getMessage() }); } }); } @@ -556,8 +547,8 @@ private void setMetadataTitle(String schema, Element xml, String language, boole */ @Override public String insertMetadata(ServiceContext context, String schema, Element metadataXml, String uuid, int owner, - String groupOwner, String source, String metadataType, String docType, String category, String createDate, - String changeDate, boolean ufo, boolean index) throws Exception { + String groupOwner, String source, String metadataType, String docType, String category, String createDate, String changeDate, + boolean ufo, boolean index) throws Exception { boolean notifyChange = true; @@ -572,8 +563,8 @@ public String insertMetadata(ServiceContext context, String schema, Element meta newMetadata.setUuid(uuid); final ISODate isoChangeDate = changeDate != null ? new ISODate(changeDate) : new ISODate(); final ISODate isoCreateDate = createDate != null ? new ISODate(createDate) : new ISODate(); - newMetadata.getDataInfo().setChangeDate(isoChangeDate).setCreateDate(isoCreateDate).setSchemaId(schema) - .setDoctype(docType).setRoot(metadataXml.getQualifiedName()).setType(MetadataType.lookup(metadataType)); + newMetadata.getDataInfo().setChangeDate(isoChangeDate).setCreateDate(isoCreateDate).setSchemaId(schema).setDoctype(docType) + .setRoot(metadataXml.getQualifiedName()).setType(MetadataType.lookup(metadataType)); newMetadata.getSourceInfo().setOwner(owner).setSourceId(source); if (StringUtils.isNotEmpty(groupOwner)) { newMetadata.getSourceInfo().setGroupOwner(Integer.valueOf(groupOwner)); @@ -594,26 +585,26 @@ public String insertMetadata(ServiceContext context, String schema, Element meta boolean fullRightsForGroup = false; - int finalId = insertMetadata(context, newMetadata, metadataXml, notifyChange, index, ufo, UpdateDatestamp.NO, - fullRightsForGroup, false).getId(); + int finalId = insertMetadata(context, newMetadata, metadataXml, notifyChange, index, ufo, UpdateDatestamp.NO, fullRightsForGroup, + false).getId(); return String.valueOf(finalId); } @Override public AbstractMetadata insertMetadata(ServiceContext context, AbstractMetadata newMetadata, Element metadataXml, - boolean notifyChange, boolean index, boolean updateFixedInfo, UpdateDatestamp updateDatestamp, - boolean fullRightsForGroup, boolean forceRefreshReaders) throws Exception { + boolean notifyChange, boolean index, boolean updateFixedInfo, UpdateDatestamp updateDatestamp, boolean fullRightsForGroup, + boolean forceRefreshReaders) throws Exception { final String schema = newMetadata.getDataInfo().getSchemaId(); // Check if the schema is allowed by settings String mdImportSetting = settingManager.getValue(Settings.METADATA_IMPORT_RESTRICT); if (mdImportSetting != null && !mdImportSetting.equals("")) { - if (!newMetadata.getHarvestInfo().isHarvested() - && !Arrays.asList(mdImportSetting.split(",")).contains(schema)) { - throw new IllegalArgumentException( - schema + " is not permitted in the database as a non-harvested metadata. " - + "Apply a import stylesheet to convert file to allowed schemas"); + if (!newMetadata.getHarvestInfo().isHarvested() && !Arrays.asList(mdImportSetting.split(",")).contains(schema)) { + throw new IllegalArgumentException("The system setting '" + Settings.METADATA_IMPORT_RESTRICT + + "' doesn't allow to import " + schema + + " metadata records (they can still be harvested). " + + "Apply an import stylesheet to convert file to one of the allowed schemas: " + mdImportSetting); } } @@ -622,8 +613,8 @@ public AbstractMetadata insertMetadata(ServiceContext context, AbstractMetadata if (updateFixedInfo && newMetadata.getDataInfo().getType() == MetadataType.METADATA) { String parentUuid = null; - metadataXml = updateFixedInfo(schema, Optional.absent(), newMetadata.getUuid(), metadataXml, - parentUuid, updateDatestamp, context); + metadataXml = updateFixedInfo(schema, Optional.absent(), newMetadata.getUuid(), metadataXml, parentUuid, + updateDatestamp, context); } // --- store metadata @@ -652,19 +643,18 @@ public AbstractMetadata insertMetadata(ServiceContext context, AbstractMetadata * Retrieves a metadata (in xml) given its id; adds editing information if * requested and validation errors if requested. * - * @param forEditing Add extra element to build metadocument - * {@link EditLib#expandElements(String, Element)} + * @param forEditing Add extra element to build metadocument + * {@link EditLib#expandElements(String, Element)} * @param applyOperationsFilters Filter elements based on operation filters - * eg. Remove WMS if not dynamic. For example, when processing - * a record, the complete records need to be processed and saved (not a filtered version). If editing, set it to false. - * {@link EditLib#expandElements(String, Element)} - * @param keepXlinkAttributes When XLinks are resolved in non edit mode, do not remove XLink - * attributes. + * eg. Remove WMS if not dynamic. For example, when processing + * a record, the complete records need to be processed and saved (not a filtered version). If editing, set it to false. + * {@link EditLib#expandElements(String, Element)} + * @param keepXlinkAttributes When XLinks are resolved in non edit mode, do not remove XLink + * attributes. */ @Override - public Element getMetadata(ServiceContext srvContext, String id, - boolean forEditing, boolean applyOperationsFilters, - boolean withEditorValidationErrors, boolean keepXlinkAttributes) throws Exception { + public Element getMetadata(ServiceContext srvContext, String id, boolean forEditing, boolean applyOperationsFilters, + boolean withEditorValidationErrors, boolean keepXlinkAttributes) throws Exception { boolean doXLinks = getXmlSerializer().resolveXLinks(); Element metadataXml = getXmlSerializer().selectNoXLinkResolver(id, false, applyOperationsFilters); if (metadataXml == null) @@ -693,8 +683,8 @@ public Element getMetadata(ServiceContext srvContext, String id, } if (withEditorValidationErrors) { - final Pair versionAndReport = metadataValidator.doValidate(srvContext.getUserSession(), schema, id, metadataXml, - srvContext.getLanguage(), forEditing); + final Pair versionAndReport = metadataValidator + .doValidate(srvContext.getUserSession(), schema, id, metadataXml, srvContext.getLanguage(), forEditing); version = versionAndReport.two(); // Add the validation report to the record // under a geonet:report element. The report @@ -741,8 +731,7 @@ public Element getMetadata(String id) throws Exception { * For update of owner info. */ @Override - public synchronized void updateMetadataOwner(final int id, final String owner, final String groupOwner) - throws Exception { + public synchronized void updateMetadataOwner(final int id, final String owner, final String groupOwner) throws Exception { metadataRepository.update(id, new Updater() { @Override public void apply(@Nonnull Metadata entity) { @@ -760,9 +749,9 @@ public void apply(@Nonnull Metadata entity) { * @return metadata if the that was updated */ @Override - public synchronized AbstractMetadata updateMetadata(final ServiceContext context, final String metadataId, - final Element md, final boolean validate, final boolean ufo, final boolean index, final String lang, - final String changeDate, final boolean updateDateStamp) throws Exception { + public synchronized AbstractMetadata updateMetadata(final ServiceContext context, final String metadataId, final Element md, + final boolean validate, final boolean ufo, final boolean index, final String lang, final String changeDate, + final boolean updateDateStamp) throws Exception { Log.trace(Geonet.DATA_MANAGER, "Update record with id " + metadataId); Element metadataXml = md; @@ -832,8 +821,7 @@ public synchronized AbstractMetadata updateMetadata(final ServiceContext context private String findUuid(Element metadataXml, String schema, AbstractMetadata metadata) throws Exception { String uuid = null; - if (schemaManager.getSchema(schema).isReadwriteUUID() - && metadata.getDataInfo().getType() != MetadataType.SUB_TEMPLATE + if (schemaManager.getSchema(schema).isReadwriteUUID() && metadata.getDataInfo().getType() != MetadataType.SUB_TEMPLATE && metadata.getDataInfo().getType() != MetadataType.TEMPLATE_OF_SUB_TEMPLATE) { uuid = metadataUtils.extractUUID(schema, metadataXml); } @@ -909,8 +897,7 @@ private Element buildInfoElem(ServiceContext context, String id, String version) */ // Add validity information - List validationInfo = metadataValidationRepository - .findAllById_MetadataId(Integer.parseInt(id)); + List validationInfo = metadataValidationRepository.findAllById_MetadataId(Integer.parseInt(id)); if (validationInfo == null || validationInfo.size() == 0) { addElement(info, Edit.Info.Elem.VALID, "-1"); } else { @@ -924,9 +911,8 @@ private Element buildInfoElem(ServiceContext context, String id, String version) String ratio = "xsd".equals(type) ? "" : vi.getNumFailures() + "/" + vi.getNumTests(); - info.addContent(new Element(Edit.Info.Elem.VALID + "_details") - .addContent(new Element("type").setText(type)).addContent(new Element("status") - .setText(vi.isValid() ? "1" : "0").addContent(new Element("ratio").setText(ratio)))); + info.addContent(new Element(Edit.Info.Elem.VALID + "_details").addContent(new Element("type").setText(type)) + .addContent(new Element("status").setText(vi.isValid() ? "1" : "0").addContent(new Element("ratio").setText(ratio)))); } addElement(info, Edit.Info.Elem.VALID, isValid); } @@ -953,12 +939,11 @@ private Element buildInfoElem(ServiceContext context, String id, String version) * @param updateDatestamp updateDatestamp is not used when running XSL transformation */ @Override - public Element updateFixedInfo(String schema, Optional metadataId, String uuid, Element md, - String parentUuid, UpdateDatestamp updateDatestamp, ServiceContext context) throws Exception { + public Element updateFixedInfo(String schema, Optional metadataId, String uuid, Element md, String parentUuid, + UpdateDatestamp updateDatestamp, ServiceContext context) throws Exception { boolean autoFixing = settingManager.getValueAsBool(Settings.SYSTEM_AUTOFIXING_ENABLE, true); if (autoFixing) { - LOGGER_DATA_MANAGER.debug("Autofixing is enabled, trying update-fixed-info (updateDatestamp: {})", - updateDatestamp.name()); + LOGGER_DATA_MANAGER.debug("Autofixing is enabled, trying update-fixed-info (updateDatestamp: {})", updateDatestamp.name()); AbstractMetadata metadata = null; if (metadataId.isPresent()) { @@ -1029,10 +1014,10 @@ public Element updateFixedInfo(String schema, Optional metadataId, Stri result.addContent(env); // apply update-fixed-info.xsl - Path styleSheet = metadataSchemaUtils.getSchemaDir(schema) - .resolve(metadata != null && metadata.getDataInfo().getType() == MetadataType.SUB_TEMPLATE - ? Geonet.File.UPDATE_FIXED_INFO_SUBTEMPLATE - : Geonet.File.UPDATE_FIXED_INFO); + Path styleSheet = metadataSchemaUtils.getSchemaDir(schema).resolve( + metadata != null && metadata.getDataInfo().getType() == MetadataType.SUB_TEMPLATE ? + Geonet.File.UPDATE_FIXED_INFO_SUBTEMPLATE : + Geonet.File.UPDATE_FIXED_INFO); result = Xml.transform(result, styleSheet); return result; } else { @@ -1055,8 +1040,8 @@ public Element updateFixedInfo(String schema, Optional metadataId, Stri * @param params parameters */ @Override - public Set updateChildren(ServiceContext srvContext, String parentUuid, String[] children, - Map params) throws Exception { + public Set updateChildren(ServiceContext srvContext, String parentUuid, String[] children, Map params) + throws Exception { String parentId = (String) params.get(Params.ID); String parentSchema = (String) params.get(Params.SCHEMA); @@ -1084,16 +1069,14 @@ public Set updateChildren(ServiceContext srvContext, String parentUuid, Element child = getMetadata(srvContext, childId, forEditing, false, withValidationErrors, keepXlinkAttributes); - String childSchema = child.getChild(Edit.RootChild.INFO, Edit.NAMESPACE) - .getChildText(Edit.Info.Elem.SCHEMA); + String childSchema = child.getChild(Edit.RootChild.INFO, Edit.NAMESPACE).getChildText(Edit.Info.Elem.SCHEMA); // Check schema matching. CHECKME : this suppose that parent and // child are in the same schema (even not profil different) if (!childSchema.equals(parentSchema)) { untreatedChildSet.add(childId); - LOGGER_DATA_MANAGER.debug( - "Could not update child ({}) because schema ({}) is different from the parent one ({}).", - new Object[]{childId, childSchema, parentSchema}); + LOGGER_DATA_MANAGER.debug("Could not update child ({}) because schema ({}) is different from the parent one ({}).", + new Object[] { childId, childSchema, parentSchema }); continue; } LOGGER_DATA_MANAGER.debug("Updating child ({}) ...", childId); @@ -1107,8 +1090,7 @@ public Set updateChildren(ServiceContext srvContext, String parentUuid, // --- do an XSL transformation - Path styleSheet = metadataSchemaUtils.getSchemaDir(parentSchema) - .resolve(Geonet.File.UPDATE_CHILD_FROM_PARENT_INFO); + Path styleSheet = metadataSchemaUtils.getSchemaDir(parentSchema).resolve(Geonet.File.UPDATE_CHILD_FROM_PARENT_INFO); Element childForUpdate = Xml.transform(rootEl, styleSheet, params); getXmlSerializer().update(childId, childForUpdate, new ISODate().toString(), true, null, srvContext); @@ -1138,20 +1120,17 @@ public Set updateChildren(ServiceContext srvContext, String parentUuid, * privilege information should be added. */ @Override - public void buildPrivilegesMetadataInfo(ServiceContext context, Map mdIdToInfoMap) - throws Exception { - Collection metadataIds = Collections2.transform(mdIdToInfoMap.keySet(), - new Function() { - @Nullable - @Override - public Integer apply(String input) { - return Integer.valueOf(input); - } - }); + public void buildPrivilegesMetadataInfo(ServiceContext context, Map mdIdToInfoMap) throws Exception { + Collection metadataIds = Collections2.transform(mdIdToInfoMap.keySet(), new Function() { + @Nullable + @Override + public Integer apply(String input) { + return Integer.valueOf(input); + } + }); Specification operationAllowedSpec = OperationAllowedSpecs.hasMetadataIdIn(metadataIds); - final Collection allUserGroups = accessManager.getUserGroups(context.getUserSession(), - context.getIpAddress(), false); + final Collection allUserGroups = accessManager.getUserGroups(context.getUserSession(), context.getIpAddress(), false); final SetMultimap operationsPerMetadata = loadOperationsAllowed(context, where(operationAllowedSpec).and(OperationAllowedSpecs.hasGroupIdIn(allUserGroups))); final Set visibleToAll = loadOperationsAllowed(context, @@ -1159,7 +1138,8 @@ public Integer apply(String input) { final Set downloadableByGuest = loadOperationsAllowed(context, where(operationAllowedSpec).and(OperationAllowedSpecs.hasGroupId(ReservedGroup.guest.getId())) .and(OperationAllowedSpecs.hasOperation(ReservedOperation.download))).keySet(); - final Map allSourceInfo = findAllSourceInfo((Specification) MetadataSpecs.hasMetadataIdIn(metadataIds)); + final Map allSourceInfo = findAllSourceInfo( + (Specification) MetadataSpecs.hasMetadataIdIn(metadataIds)); for (Map.Entry entry : mdIdToInfoMap.entrySet()) { Element infoEl = entry.getValue(); @@ -1198,7 +1178,7 @@ public Integer apply(String input) { } protected SetMultimap loadOperationsAllowed(ServiceContext context, - Specification operationAllowedSpec) { + Specification operationAllowedSpec) { final OperationAllowedRepository operationAllowedRepo = context.getBean(OperationAllowedRepository.class); List operationsAllowed = operationAllowedRepo.findAll(operationAllowedSpec); SetMultimap operationsPerMetadata = HashMultimap.create(); @@ -1295,16 +1275,15 @@ public void delete(Integer id) { @Override public void createBatchUpdateQuery(PathSpec servicesPath, String newUuid, - Specification harvested) { + Specification harvested) { try { - metadataRepository.createBatchUpdateQuery((PathSpec) servicesPath, newUuid, - (Specification) harvested); + metadataRepository + .createBatchUpdateQuery((PathSpec) servicesPath, newUuid, (Specification) harvested); } catch (ClassCastException t) { throw new ClassCastException("Unknown AbstractMetadata subtype: " + servicesPath.getClass().getName()); } } - @Override public Map findAllSourceInfo(Specification specs) { try { diff --git a/core/src/main/java/org/fao/geonet/kernel/mef/Importer.java b/core/src/main/java/org/fao/geonet/kernel/mef/Importer.java index d16629a99bc..f5e03457641 100644 --- a/core/src/main/java/org/fao/geonet/kernel/mef/Importer.java +++ b/core/src/main/java/org/fao/geonet/kernel/mef/Importer.java @@ -1,5 +1,5 @@ //============================================================================= -//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the +//=== Copyright (C) 2001-2020 Food and Agriculture Organization of the //=== United Nations (FAO-UN), United Nations World Food Programme (WFP) //=== and United Nations Environment Programme (UNEP) //=== @@ -23,22 +23,10 @@ package org.fao.geonet.kernel.mef; -import static org.fao.geonet.domain.Localized.translationXmlToLangMap; - -import java.io.InputStream; -import java.nio.file.DirectoryStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; - -import javax.annotation.Nonnull; - +import com.google.common.base.Optional; +import com.google.common.collect.Maps; +import jeeves.server.ServiceConfig; +import jeeves.server.context.ServiceContext; import org.fao.geonet.ApplicationContextHolder; import org.fao.geonet.GeonetContext; import org.fao.geonet.MetadataResourceDatabaseMigration; @@ -81,16 +69,25 @@ import org.jdom.Element; import org.springframework.context.ApplicationContext; -import com.google.common.base.Optional; -import com.google.common.collect.Maps; +import javax.annotation.Nonnull; +import java.io.InputStream; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; -import jeeves.server.ServiceConfig; -import jeeves.server.context.ServiceContext; +import static org.fao.geonet.domain.Localized.translationXmlToLangMap; public class Importer { @Deprecated - public static List doImport(final Element params, final ServiceContext context, - final Path mefFile, final Path stylePath) throws Exception { + public static List doImport(final Element params, final ServiceContext context, final Path mefFile, + final Path stylePath) throws Exception { String fileType = Util.getParam(params, "file_type", "mef"); String style = Util.getParam(params, Params.STYLESHEET, "_none_"); String uuidAction = Util.getParam(params, Params.UUID_ACTION, Params.NOTHING); @@ -101,25 +98,13 @@ public static List doImport(final Element params, final ServiceContext c boolean validate = Util.getParam(params, Params.VALIDATE, "off").equals("on"); boolean assign = Util.getParam(params, "assign", "off").equals("on"); - return doImport( - fileType, - MEFLib.UuidAction.parse(uuidAction), - style, source, isTemplate, - new String[]{category}, groupId, + return doImport(fileType, MEFLib.UuidAction.parse(uuidAction), style, source, isTemplate, new String[] { category }, groupId, validate, assign, context, mefFile); } - public static List doImport(String fileType, - final MEFLib.UuidAction uuidAction, - final String style, - final String source, - final MetadataType isTemplate, - final String[] category, - final String groupId, - final boolean validate, - final boolean assign, - final ServiceContext context, - final Path mefFile) throws Exception { + public static List doImport(String fileType, final MEFLib.UuidAction uuidAction, final String style, final String source, + final MetadataType isTemplate, final String[] category, final String groupId, final boolean validate, final boolean assign, + final ServiceContext context, final Path mefFile) throws Exception { ApplicationContext applicationContext = ApplicationContextHolder.get(); final DataManager dm = applicationContext.getBean(DataManager.class); final SettingManager sm = applicationContext.getBean(SettingManager.class); @@ -154,15 +139,13 @@ else if (fileType.equals("mef2")) final String finalPreferredSchema = preferredSchema; MEFLib.visit(mefFile, visitor, new IMEFVisitor() { - public void handleMetadata(Element metadata, int index) - throws Exception { + public void handleMetadata(Element metadata, int index) throws Exception { if (Log.isDebugEnabled(Geonet.MEF)) Log.debug(Geonet.MEF, "Collecting metadata:\n" + Xml.getString(metadata)); md.add(index, metadata); } - public void handleMetadataFiles(DirectoryStream metadataXmlFiles, Element info, int index) - throws Exception { + public void handleMetadataFiles(DirectoryStream metadataXmlFiles, Element info, int index) throws Exception { String infoSchema = "_none_"; if (info != null && info.getContentSize() != 0) { Element general = info.getChild("general"); @@ -180,10 +163,8 @@ public void handleMetadataFiles(DirectoryStream metadataXmlFiles, Element if (Log.isDebugEnabled(Geonet.MEF)) Log.debug(Geonet.MEF, "info.xml says schema should be " + infoSchema); - Element metadataValidForImport; - Map> mdFiles = new HashMap>(); for (Path file : metadataXmlFiles) { if (file != null && java.nio.file.Files.isRegularFile(file)) { @@ -209,16 +190,16 @@ public void handleMetadataFiles(DirectoryStream metadataXmlFiles, Element } if (mdFiles.size() == 0) { - throw new BadFormatEx("No valid metadata file found" + ((lastUnknownMetadataFolderName == null) ? "" : (" in " + - lastUnknownMetadataFolderName)) + "."); + throw new BadFormatEx("No valid metadata file found" + ((lastUnknownMetadataFolderName == null) ? + "" : + (" in " + lastUnknownMetadataFolderName)) + "."); } // 1st: Select metadata with schema in info file Pair mdInform = mdFiles.get(infoSchema); if (mdInform != null) { if (Log.isDebugEnabled(Geonet.MEF)) { - Log.debug(Geonet.MEF, mdInform.one() - + " with info.xml schema (" + infoSchema + ")."); + Log.debug(Geonet.MEF, mdInform.one() + " with info.xml schema (" + infoSchema + ")."); } metadataValidForImport = mdInform.two(); handleMetadata(metadataValidForImport, index); @@ -229,8 +210,7 @@ public void handleMetadataFiles(DirectoryStream metadataXmlFiles, Element mdInform = mdFiles.get(finalPreferredSchema); if (mdInform != null) { if (Log.isDebugEnabled(Geonet.MEF)) { - Log.debug(Geonet.MEF, mdInform.one() - + " with preferred schema (" + finalPreferredSchema + ")."); + Log.debug(Geonet.MEF, mdInform.one() + " with preferred schema (" + finalPreferredSchema + ")."); } metadataValidForImport = mdInform.two(); handleMetadata(metadataValidForImport, index); @@ -241,8 +221,7 @@ public void handleMetadataFiles(DirectoryStream metadataXmlFiles, Element String metadataSchema = (String) mdFiles.keySet().toArray()[0]; mdInform = mdFiles.get(metadataSchema); if (Log.isDebugEnabled(Geonet.MEF)) { - Log.debug(Geonet.MEF, mdInform.one() - + " with known schema (" + metadataSchema + ")."); + Log.debug(Geonet.MEF, mdInform.one() + " with known schema (" + metadataSchema + ")."); } metadataValidForImport = mdInform.two(); @@ -252,8 +231,7 @@ public void handleMetadataFiles(DirectoryStream metadataXmlFiles, Element // -------------------------------------------------------------------- - public void handleFeatureCat(Element featureCat, int index) - throws Exception { + public void handleFeatureCat(Element featureCat, int index) throws Exception { if (featureCat != null) { if (Log.isDebugEnabled(Geonet.MEF)) Log.debug(Geonet.MEF, "Collecting feature catalog:\n" + Xml.getString(featureCat)); @@ -287,10 +265,8 @@ public void handleInfo(Element info, int index) throws Exception { Element categs = null; final Element privileges; - // Apply a stylesheet transformation if requested - if (!style.equals("_none_")) { FilePathChecker.verify(style); @@ -300,14 +276,10 @@ public void handleInfo(Element info, int index) throws Exception { if (Files.exists(xsltPath)) { md.add(index, Xml.transform(md.get(index), xsltPath)); } else { - throw new Exception(String.format( - "XSL transformation '%s' not found.", - style - )); + throw new Exception(String.format("XSL transformation '%s' not found.", style)); } } - final Element metadata = md.get(index); String schema = dm.autodetectSchema(metadata, null); @@ -320,23 +292,16 @@ public void handleInfo(Element info, int index) throws Exception { categs = new Element("categories"); for (String c : category) { // TODO: convert id to name ? - categs.addContent((new Element("category")) - .setAttribute("name", c)); + categs.addContent((new Element("category")).setAttribute("name", c)); } } privileges = new Element("group"); - privileges.addContent(new Element("operation") - .setAttribute("name", "view")); - privileges.addContent(new Element("operation") - .setAttribute("name", "editing")); - privileges.addContent(new Element("operation") - .setAttribute("name", "download")); - privileges.addContent(new Element("operation") - .setAttribute("name", "notify")); - privileges.addContent(new Element("operation") - .setAttribute("name", "dynamic")); - privileges.addContent(new Element("operation") - .setAttribute("name", "featured")); + privileges.addContent(new Element("operation").setAttribute("name", "view")); + privileges.addContent(new Element("operation").setAttribute("name", "editing")); + privileges.addContent(new Element("operation").setAttribute("name", "download")); + privileges.addContent(new Element("operation").setAttribute("name", "notify")); + privileges.addContent(new Element("operation").setAttribute("name", "dynamic")); + privileges.addContent(new Element("operation").setAttribute("name", "featured")); if (isTemplate == MetadataType.METADATA) { // Get the Metadata uuid if it's not a template. @@ -381,9 +346,7 @@ public void handleInfo(Element info, int index) throws Exception { // In GeoNetwork 3.x, links to resources changed: // * thumbnails contains full URL instead of file name only // * API mode change old URL structure. - MetadataResourceDatabaseMigration.updateMetadataResourcesLink( - metadata, null, sm - ); + MetadataResourceDatabaseMigration.updateMetadataResourcesLink(metadata, null, sm); } if (validate) { @@ -396,14 +359,11 @@ public void handleInfo(Element info, int index) throws Exception { DataManager.validateExternalMetadata(schema, metadata, context, groupIdVal); } - try { - importRecord(uuid, uuidAction, md, schema, index, - source, sourceName, sourceTranslations, - context, metadataIdMap, createDate, - changeDate, groupId, isTemplate); + importRecord(uuid, uuidAction, md, schema, index, source, sourceName, sourceTranslations, context, metadataIdMap, + createDate, changeDate, groupId, isTemplate); } catch (Exception e) { - throw new Exception("Failed to import metadata with uuid '" + uuid + "'. " + e.getLocalizedMessage(), e); + throw new Exception("Failed to import metadata with uuid '" + uuid + "'. " + e.getLocalizedMessage(), e); } if (fc.size() != 0 && fc.get(index) != null) { @@ -418,10 +378,9 @@ public void handleInfo(Element info, int index) throws Exception { int userid = context.getUserSession().getUserIdAsInt(); String group = null, docType = null, title = null, category = null; boolean ufo = false, indexImmediate = false; - String fcId = dm.insertMetadata( - context, "iso19110", fc.get(index), uuid, - userid, group, source, isTemplate.codeString, - docType, category, createDate, changeDate, ufo, indexImmediate); + String fcId = dm + .insertMetadata(context, "iso19110", fc.get(index), uuid, userid, group, source, isTemplate.codeString, docType, + category, createDate, changeDate, ufo, indexImmediate); if (Log.isDebugEnabled(Geonet.MEF)) Log.debug(Geonet.MEF, "Adding Feature catalog with uuid: " + uuid); @@ -447,8 +406,7 @@ public void handleInfo(Element info, int index) throws Exception { final Element finalCategs = categs; final String finalGroupId = groupId; context.getBean(IMetadataManager.class).update(iMetadataId, new Updater() { - @Override - public void apply(@Nonnull final Metadata metadata) { + @Override public void apply(@Nonnull final Metadata metadata) { final MetadataDataInfo dataInfo = metadata.getDataInfo(); if (finalPopularity != null) { dataInfo.setPopularity(Integer.valueOf(finalPopularity)); @@ -474,18 +432,15 @@ public void apply(@Nonnull final Metadata metadata) { allowedRepository.save(allowedSet); } - } }); - dm.indexMetadata(metadataIdMap.get(index), true, null); } // -------------------------------------------------------------------- - public void handlePublicFile(String file, String changeDate, - InputStream is, int index) throws Exception { + public void handlePublicFile(String file, String changeDate, InputStream is, int index) throws Exception { if (Log.isDebugEnabled(Geonet.MEF)) { Log.debug(Geonet.MEF, "Adding public file with name=" + file); } @@ -494,12 +449,10 @@ public void handlePublicFile(String file, String changeDate, // -------------------------------------------------------------------- - public void handlePrivateFile(String file, String changeDate, - InputStream is, int index) throws Exception { + public void handlePrivateFile(String file, String changeDate, InputStream is, int index) throws Exception { if (Log.isDebugEnabled(Geonet.MEF)) Log.debug(Geonet.MEF, "Adding private file with name=" + file); - saveFile(context, metadataIdMap.get(index), MetadataResourceVisibility.PRIVATE, file, changeDate, - is); + saveFile(context, metadataIdMap.get(index), MetadataResourceVisibility.PRIVATE, file, changeDate, is); } }); @@ -530,18 +483,15 @@ public static void addCategoriesToMetadata(AbstractMetadata metadata, Element fi } } - public static void importRecord(String uuid, - MEFLib.UuidAction uuidAction, List md, String schema, int index, - String source, String sourceName, Map sourceTranslations, ServiceContext context, - List id, String createDate, String changeDate, - String groupId, MetadataType isTemplate) throws Exception { + public static void importRecord(String uuid, MEFLib.UuidAction uuidAction, List md, String schema, int index, String source, + String sourceName, Map sourceTranslations, ServiceContext context, List id, String createDate, + String changeDate, String groupId, MetadataType isTemplate) throws Exception { GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); DataManager dm = gc.getBean(DataManager.class); IMetadataManager metadataManager = gc.getBean(IMetadataManager.class); - if (uuid == null || uuid.equals("") - || uuidAction == MEFLib.UuidAction.GENERATEUUID) { + if (uuid == null || uuid.equals("") || uuidAction == MEFLib.UuidAction.GENERATEUUID) { String newuuid = UUID.randomUUID().toString(); source = null; @@ -555,15 +505,13 @@ public static void importRecord(String uuid, sourceName = "???"; if (source == null || source.trim().length() == 0) - throw new Exception( - "Missing siteId parameter from info.xml file"); + throw new Exception("Missing siteId parameter from info.xml file"); // --- only update sources table if source is not current site SourceRepository sourceRepository = context.getBean(SourceRepository.class); Source sourceObject = sourceRepository.findOneByUuid(source); - if (sourceObject == null - || (!source.equals(gc.getBean(SettingManager.class).getSiteId()) - && SourceType.harvester != sourceObject.getType())) { + if (sourceObject == null || (!source.equals(gc.getBean(SettingManager.class).getSiteId()) + && SourceType.harvester != sourceObject.getType())) { Source source1 = new Source(source, sourceName, sourceTranslations, SourceType.externalportal); context.getBean(SourceRepository.class).save(source1); } @@ -588,7 +536,6 @@ public static void importRecord(String uuid, throw new Exception(" Existing metadata with UUID " + uuid + " could not be deleted. Error is: " + e.getMessage()); } - if (Log.isDebugEnabled(Geonet.MEF)) Log.debug(Geonet.MEF, "Adding metadata with uuid:" + uuid); @@ -596,10 +543,12 @@ public static void importRecord(String uuid, // insert metadata // int userid = context.getUserSession().getUserIdAsInt(); - String docType = null, category = null; boolean ufo = false, indexImmediate = false; + String docType = null, category = null; + boolean ufo = false, indexImmediate = false; - String metadataId = metadataManager.insertMetadata(context, schema, md.get(index), uuid, - userid, groupId, source, isTemplate.codeString, docType, category, createDate, changeDate, ufo, indexImmediate); + String metadataId = metadataManager + .insertMetadata(context, schema, md.get(index), uuid, userid, groupId, source, isTemplate.codeString, docType, category, + createDate, changeDate, ufo, indexImmediate); dm.activateWorkflowIfConfigured(context, metadataId, groupId); @@ -609,9 +558,8 @@ public static void importRecord(String uuid, // -------------------------------------------------------------------------- - private static void saveFile(ServiceContext context, String id, - MetadataResourceVisibility access, String file, String changeDate, InputStream is) - throws Exception { + private static void saveFile(ServiceContext context, String id, MetadataResourceVisibility access, String file, String changeDate, + InputStream is) throws Exception { final Store store = context.getBean("resourceStore", Store.class); final IMetadataUtils metadataUtils = context.getBean(IMetadataUtils.class); final String metadataUuid = metadataUtils.getMetadataUuid(id); @@ -622,14 +570,12 @@ private static void saveFile(ServiceContext context, String id, /** * Add privileges according to information file. */ - private static Group addPrivileges(final ServiceContext context, final DataManager dm, final int metadataId, - final Element privil) { + private static Group addPrivileges(final ServiceContext context, final DataManager dm, final int metadataId, final Element privil) { final GroupRepository groupRepository = context.getBean(GroupRepository.class); final OperationAllowedRepository allowedRepository = context.getBean(OperationAllowedRepository.class); - @SuppressWarnings("unchecked") - List list = privil.getChildren("group"); + @SuppressWarnings("unchecked") List list = privil.getChildren("group"); Group owner = null; Set opAllowedToAdd = new HashSet(); @@ -668,9 +614,8 @@ private static Group addPrivileges(final ServiceContext context, final DataManag * Add operations according to information file. */ private static Set addOperations(final ServiceContext context, final DataManager dm, final Element group, - final int metadataId, final int grpId) { - @SuppressWarnings("unchecked") - List operations = group.getChildren("operation"); + final int metadataId, final int grpId) { + @SuppressWarnings("unchecked") List operations = group.getChildren("operation"); Set toAdd = new HashSet(); for (Element operation : operations) {