Skip to content

Commit

Permalink
Directory / Add the capability to synchronize with a subset of the di…
Browse files Browse the repository at this point in the history
…rectory eg. &fq=_groupPublished=IFREMER will only synchronize entries published for group IFREMER.
  • Loading branch information
fxprunayre committed May 20, 2016
1 parent 7f45c41 commit fcc9345
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ public class DirectoryApi {
"and then check if this element is available in the directory. " +
"If Found, the element from the directory update the element " +
"in the record and optionally properties are preserved.<br/><br/>" +
"The identifier XPath is used to find a match. If not, the UUID " +
"is based on the content of the snippet. It is recommended to use " +
"The identifier XPath is used to find a match. An optional filter" +
"can be added to restrict search to a subset of the directory. " +
"If no identifier XPaths is provided, the UUID " +
"is based on the content of the snippet (hash). It is recommended to use " +
"an identifier for better matching (eg. ISO19139 contact with different " +
"roles will not match on the automatic UUID mode).";

Expand All @@ -92,6 +94,8 @@ public class DirectoryApi {
"List of XPath of properties to copy from record to matching entry.";
public static final String APIPARAM_REPLACEWITHXLINK =
"Replace entry by XLink.";
public static final String APIPARAM_DIRECTORYFILTERQUERY =
"Filter query for directory search.";

@ApiOperation(value = "Preview directory entries extracted from records",
nickname = "previewExtractedEntries",
Expand All @@ -108,7 +112,7 @@ public ResponseEntity<Element> previewExtractedEntries(
@RequestParam(required = false)
String[] uuids,
@ApiParam(value = APIPARAM_XPATH,
required = false,
required = true,
example = ".//gmd:CI_ResponsibleParty")
@RequestParam(required = true)
String xpath,
Expand All @@ -118,7 +122,7 @@ public ResponseEntity<Element> previewExtractedEntries(
@RequestParam(required = false)
String identifierXpath
) throws Exception {
return collectEntries(uuids, xpath, identifierXpath, false);
return collectEntries(uuids, xpath, identifierXpath, false, null);
}


Expand All @@ -137,7 +141,7 @@ public ResponseEntity<Element> extractEntries(
@RequestParam(required = false)
String[] uuids,
@ApiParam(value = APIPARAM_XPATH,
required = false,
required = true,
example = ".//gmd:CI_ResponsibleParty")
@RequestParam(required = true)
String xpath,
Expand All @@ -150,15 +154,15 @@ public ResponseEntity<Element> extractEntries(
// TODO: Add an option to set groupOwner ?
// TODO: Add an option to set privileges ?
) throws Exception {
return collectEntries(uuids, xpath, identifierXpath, true);
return collectEntries(uuids, xpath, identifierXpath, true, null);
}


private ResponseEntity<Element> collectEntries(
String[] uuids,
String xpath,
String identifierXpath,
boolean save) throws Exception {
String[] uuids,
String xpath,
String identifierXpath,
boolean save, String directoryFilterQuery) throws Exception {
ServiceContext context = ServiceContext.get();
UserSession session = context.getUserSession();
Profile profile = session.getProfile();
Expand Down Expand Up @@ -260,7 +264,7 @@ public ResponseEntity<Element> previewUpdatedRecordEntries(
@RequestParam(required = false)
String[] uuids,
@ApiParam(value = APIPARAM_XPATH,
required = false,
required = true,
example = ".//gmd:CI_ResponsibleParty")
@RequestParam(required = true)
String xpath,
Expand All @@ -279,10 +283,15 @@ public ResponseEntity<Element> previewUpdatedRecordEntries(
example = "@uuid")
@RequestParam(required = false, defaultValue = "false")
boolean substituteAsXLink,
@ApiParam(value = APIPARAM_DIRECTORYFILTERQUERY,
required = false,
example = "groupPublished:IFREMER")
@RequestParam(required = false)
String fq,
@ApiIgnore
HttpServletRequest httpRequest
) throws Exception {
return updateRecordEntries(uuids, xpath, identifierXpath, propertiesToCopy, substituteAsXLink, false);
return updateRecordEntries(uuids, xpath, identifierXpath, propertiesToCopy, substituteAsXLink, false, fq);
}


Expand All @@ -302,7 +311,7 @@ public ResponseEntity<Element> updateRecordEntries(
@RequestParam(required = false)
String[] uuids,
@ApiParam(value = APIPARAM_XPATH,
required = false,
required = true,
example = ".//gmd:CI_ResponsibleParty")
@RequestParam(required = true)
String xpath,
Expand All @@ -319,9 +328,14 @@ public ResponseEntity<Element> updateRecordEntries(
@ApiParam(value = APIPARAM_REPLACEWITHXLINK,
required = false)
@RequestParam(required = false, defaultValue = "false")
boolean substituteAsXLink
boolean substituteAsXLink,
@ApiParam(value = APIPARAM_DIRECTORYFILTERQUERY,
required = false,
example = "groupPublished:IFREMER")
@RequestParam(required = false)
String fq
) throws Exception {
return updateRecordEntries(uuids, xpath, identifierXpath, propertiesToCopy, substituteAsXLink, true);
return updateRecordEntries(uuids, xpath, identifierXpath, propertiesToCopy, substituteAsXLink, true, fq);
}


Expand All @@ -331,7 +345,7 @@ private ResponseEntity<Element> updateRecordEntries(
String identifierXpath,
List<String> propertiesToCopy,
boolean substituteAsXLink,
boolean save) throws Exception {
boolean save, String directoryFilterQuery) throws Exception {
ServiceContext context = ServiceContext.get();
UserSession session = context.getUserSession();
Profile profile = session.getProfile();
Expand Down Expand Up @@ -383,7 +397,7 @@ private ResponseEntity<Element> updateRecordEntries(
CollectResults collectResults =
DirectoryUtils.synchronizeEntries(
record, xpath, identifierXpath,
propertiesToCopy, substituteAsXLink);
propertiesToCopy, substituteAsXLink, directoryFilterQuery);
listOfRecordInternalId.add(record.getId());
if (save && collectResults.isRecordUpdated()) {
// TODO: Only if there was a change
Expand Down Expand Up @@ -417,11 +431,11 @@ private ResponseEntity<Element> updateRecordEntries(
return new ResponseEntity<>(HttpStatus.CREATED);
} else {
// TODO: Limite size of large response ?
Element response = new Element("list");
Element response = new Element("entries");
for (Element e : listOfUpdatedRecord) {
response.addContent(e);
}
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
import jeeves.server.context.ServiceContext;
import jeeves.xlink.XLink;
import org.apache.commons.lang.StringUtils;
import org.apache.pdfbox.util.StringUtil;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.domain.ISODate;
import org.fao.geonet.domain.Metadata;
import org.fao.geonet.domain.MetadataDataInfo;
import org.fao.geonet.domain.MetadataType;
import org.fao.geonet.kernel.AccessManager;
import org.fao.geonet.kernel.DataManager;
import org.fao.geonet.kernel.UpdateDatestamp;
import org.fao.geonet.kernel.search.MetaSearcher;
import org.fao.geonet.kernel.search.SearchManager;
import org.fao.geonet.kernel.search.SearcherType;
import org.fao.geonet.kernel.setting.SettingManager;
import org.fao.geonet.repository.MetadataRepository;
import org.fao.geonet.services.subtemplate.Get;
import org.fao.geonet.util.Sha1Encoder;
Expand Down Expand Up @@ -120,14 +117,14 @@ public static void saveEntries(CollectResults collectResults,
* @param record
* @param xpath
* @param identifierXpath
* @param directoryFilterQuery
* @return
* @throws Exception
*/
public static CollectResults collectEntries(Metadata record,
String xpath,
String identifierXpath
) throws Exception {
return collectEntries(record, xpath, identifierXpath, null, false, false);
String identifierXpath) throws Exception {
return collectEntries(record, xpath, identifierXpath, null, false, false, null);
}

/**
Expand All @@ -138,29 +135,30 @@ public static CollectResults collectEntries(Metadata record,
* @param record
* @param xpath
* @param identifierXpath
* @param substituteAsXLink
* @param propertiesToCopy
* @param substituteAsXLink
* @param directoryFilterQuery
* @return
* @throws Exception
*/
public static CollectResults synchronizeEntries(Metadata record,
String xpath,
String identifierXpath,
List<String> propertiesToCopy,
boolean substituteAsXLink
) throws Exception {
String xpath,
String identifierXpath,
List<String> propertiesToCopy,
boolean substituteAsXLink,
String directoryFilterQuery) throws Exception {
return collectEntries(record, xpath, identifierXpath,
propertiesToCopy, substituteAsXLink, true);
propertiesToCopy, substituteAsXLink, true, directoryFilterQuery);
}


private static CollectResults collectEntries(Metadata record,
String xpath,
String identifierXpath,
List<String> propertiesToCopy,
boolean substituteAsXLink,
boolean updateFromDirectory
) throws Exception {
String xpath,
String identifierXpath,
List<String> propertiesToCopy,
boolean substituteAsXLink,
boolean updateFromDirectory,
String directoryFilterQuery) throws Exception {
CollectResults collectResults = new CollectResults(record);
Map<String, List<Namespace>> namespaceList = new HashMap<String, List<Namespace>>();
ServiceContext context = ServiceContext.get();
Expand Down Expand Up @@ -210,7 +208,7 @@ private static CollectResults collectEntries(Metadata record,
Element elem = (Element) o;
if (Log.isDebugEnabled(LOGGER)) {
Log.debug(LOGGER, String.format(
"#%d. Entry XML:%n%s",
"#%d. XML entry: %s",
numberOfEntries, Xml.getString(elem)
));
}
Expand Down Expand Up @@ -273,15 +271,28 @@ private static CollectResults collectEntries(Metadata record,
if (StringUtils.isEmpty(searchIndexField)) {
Metadata subTemplate = metadataRepository.findOneByUuid(uuid);
if (subTemplate != null) {
uuid = subTemplate.getUuid();
subTemplateElement = subTemplate.getXmlData(false);
}
} else {
// or search in Lucene
String id = search(searchIndexField, identifier);
Map<String, String> parameters = new HashMap();
if (directoryFilterQuery != null) {
String[] tokens = directoryFilterQuery.split(":");
if (tokens.length == 2) {
parameters.put(tokens[0], tokens[1]);
} else {
Log.warning(LOGGER, String.format(
"Filter query for directory must be field:value format. '%s' is not.",
directoryFilterQuery
));
}
}
parameters.put(searchIndexField, identifier);
String id = search(parameters);
if (id != null) {
Metadata subTemplate = metadataRepository.findOne(id);
if (subTemplate != null) {
uuid = subTemplate.getUuid();
subTemplateElement = subTemplate.getXmlData(false);
}
}
Expand Down Expand Up @@ -403,11 +414,10 @@ private static String getPropertiesAsParameters(Element source,
/**
* Search using Lucene a matching document
*
* @param field
* @param value
* @param searchParameters
* @return The record identifier
*/
private static String search(String field, String value) {
private static String search(Map<String, String> searchParameters) {
ServiceConfig _config = new ServiceConfig();
ServiceContext context = ServiceContext.get();
SearchManager searchMan = context.getBean(SearchManager.class);
Expand All @@ -417,7 +427,10 @@ private static String search(String field, String value) {
Element parameters = new Element(Jeeves.Elem.REQUEST);
parameters.addContent(new Element("fast").addContent("index"));
parameters.addContent(new Element("_isTemplate").addContent("s"));
parameters.addContent(new Element(field).addContent(value));
for (Map.Entry<String, String> e : searchParameters.entrySet()) {
parameters.addContent(
new Element(e.getKey()).addContent(e.getValue()));
}
parameters.addContent(new Element("from").addContent(1 + ""));
parameters.addContent(new Element("to").addContent(1 + ""));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void testSynchronizeEntries() throws Exception {
CollectResults collectResults =
DirectoryUtils.synchronizeEntries(record,
xpath, uuidXpath, propertiesToCopy,
false);
false, null);

Element updateRecord = collectResults.getUpdatedRecord();
recordCity = Xml.selectString(
Expand Down Expand Up @@ -226,7 +226,7 @@ public void testSynchronizeEntriesAndUseXlink() throws Exception {
CollectResults collectResults =
DirectoryUtils.synchronizeEntries(record,
xpath, uuidXpath, propertiesToCopy,
true);
true, null);

Element updateRecord = collectResults.getUpdatedRecord();

Expand Down

0 comments on commit fcc9345

Please sign in to comment.