[CommunityAPIExtention] added bulk deletion and addition for datasouces. Added the possibility to insert a pram in the cntext if it is not already present. Useful not to be forced to modify all the profiles by hand to insert new params for fos, sdg and advancedContraints

This commit is contained in:
Miriam Baglioni 2022-09-27 16:06:31 +02:00
parent 3549fdebf9
commit 2a937cc4ae
5 changed files with 172 additions and 47 deletions

View File

@ -20,9 +20,7 @@ public interface ISClient {
Map<String, Context> getContextMap(final List<String> type) throws IOException;
void updateContextParam(String id, String name, String value);
void updateContextParamNoEscape(String id, String name, String value);
void updateContextParam(String id, String name, String value, boolean toEscape);
void updateContextAttribute(String id, String name, String value);

View File

@ -105,28 +105,38 @@ public class ISClientImpl implements ISClient {
}
}
@Override
@CacheEvict(value = {
"context-cache", "context-cache-funder"
}, allEntries = true)
public void updateContextParam(final String id, final String name, final String value) {
try {
_quickSeachProfile(getXQuery(id, name, value));
} catch (final ISLookUpException e) {
throw new DsmRuntimeException(String.format("unable update context param [id: %s, name: %s, value: %s]", id, name, value), e);
public void updateContextParam(final String id, final String name, final String value, boolean toEscape) {
if(getSize(id, name) > 0){
try {
_quickSeachProfile(getXQuery(id, name, value, toEscape));
} catch (final ISLookUpException e) {
throw new DsmRuntimeException(String.format("unable to update context param [id: %s, name: %s, value: %s]", id, name, value), e);
}
}
else{
try {
_quickSeachProfile(getInsertXQuery(id, name, value,toEscape));
} catch (final ISLookUpException e) {
throw new DsmRuntimeException(String.format("unable to insert context param [id: %s, name: %s, value: %s]", id, name, value), e);
}
}
}
@Override
@CacheEvict(value = {
"context-cache", "context-cache-funder"
}, allEntries = true)
public void updateContextParamNoEscape(final String id, final String name, final String value) {
private int getSize(String id, String name) {
int size = 0;
try {
_quickSeachProfile(getXQueryNoEscape(id, name, value));
} catch (final ISLookUpException e) {
throw new DsmRuntimeException(String.format("unable update context param [id: %s, name: %s, value: %s]", id, name, value), e);
size = _quickSeachProfile(String.format("for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') where $x//context[@id='%s']/param[@name='%s'] return $x", id, name)).size();
} catch (ISLookUpException e) {
throw new DsmRuntimeException("unable to execute search query", e);
}
return size;
}
@Override
@ -212,26 +222,45 @@ public class ISClientImpl implements ISClient {
/// HELPERS
private String getXQuery(final String id, final String name, final String value) {
final Escaper esc = XmlEscapers.xmlContentEscaper();
private String getInsertXQuery(final String id, final String paramName, final String paramValue, final boolean toEscape) {
String value;
if(toEscape)
value = escape(XmlEscapers.xmlContentEscaper(), paramValue);
else
value = paramValue;
if (StringUtils.isNotBlank(value)) {
return String.format("update insert <param name='%s'>%s</param> into collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']", paramName, value, id);
} else {
return String.format("update insert <param name='%s'/> into collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']", paramName, id);
}
}
private String getXQuery(final String id, final String name, final String paramValue, final boolean toEscape) {
String value = paramValue;
if (toEscape)
value = escape(XmlEscapers.xmlContentEscaper(), paramValue);
if (StringUtils.isNotBlank(value)) {
return String.format("update replace collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']/param[./@name = '%s'] with <param name='%s'>%s</param>", id, name, name, escape(esc, value));
"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']/param[./@name = '%s'] with <param name='%s'>%s</param>", id, name, name, value);
} else {
return String.format("update replace collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']/param[./@name = '%s'] with <param name='%s'/>", id, name, name);
}
}
private String getXQueryNoEscape(final String id, final String name, final String value) {
if (StringUtils.isNotBlank(value)) {
return String.format("update replace collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']/param[./@name = '%s'] with <param name='%s'>%s</param>", id, name, name, value);
} else {
return String.format("update replace collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']/param[./@name = '%s'] with <param name='%s'/>", id, name, name);
}
}
// private String getXQueryNoEscape(final String id, final String name, final String value) {
// if (StringUtils.isNotBlank(value)) {
// return String.format("update replace collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
// "/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']/param[./@name = '%s'] with <param name='%s'>%s</param>", id, name, name, value);
// } else {
// return String.format("update replace collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
// "/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']/param[./@name = '%s'] with <param name='%s'/>", id, name, name);
// }
// }
private String getConceptXQuery(final String id, final String name, final String value) {
final Escaper esc = XmlEscapers.xmlContentEscaper();

View File

@ -223,6 +223,44 @@ public class CommunityApiController {
communityApiCore.removeCommunityContentProvider(id, contentproviderId);
}
@RequestMapping(value = "/community/{id}/contentprovidersList", produces = {
"application/json"
}, method = RequestMethod.POST)
@Operation(summary = "associate a list of content providers to the community",
description = "associate a list of content providers to the community", tags = {
C_PJ, W
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<CommunityContentprovider> addCommunityContentProvidersList(
@PathVariable final String id,
@RequestBody final List<CommunityContentprovider> contentprovidersList) throws CommunityException, CommunityNotFoundException {
return communityApiCore.addCommunityContentProvidersList(id, contentprovidersList);
}
@RequestMapping(value = "/community/{id}/projectList", produces = {
"application/json"
}, method = RequestMethod.DELETE)
@Operation(summary = "remove a list of content providers from the community",
description = "remove a list of content providers from the community", tags = {
C_PJ, W
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public void deleteCommunityContentProvidersList(
@PathVariable final String id,
@RequestBody final List<Integer> contentProviderIdList) throws CommunityException, CommunityNotFoundException {
communityApiCore.removeCommunityContentProviderList(id, contentProviderIdList);
}
// ADDING CODE FOR COMMUNITY ORGANIZATIONS
@RequestMapping(value = "/community/{id}/organizations", produces = {

View File

@ -47,6 +47,13 @@ public class CommunityApiCore {// implements CommunityClient{
}
private void removeAdvancedConstraint(String id) throws CommunityNotFoundException, CommunityException {
cc.getCommunity(id);
isClient.updateContextParam(id, CPROFILE_ADVANCED_CONSTRAINT, "", false);
cc.removeAdvancedConstraint(id);
}
public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException, CommunityNotFoundException {
cc.getCommunity(id); // ensure the community exists.
@ -56,39 +63,39 @@ public class CommunityApiCore {// implements CommunityClient{
}
if (details.getName() != null) {
isClient.updateContextParam(id, CSUMMARY_NAME, details.getName());
isClient.updateContextParam(id, CSUMMARY_NAME, details.getName(), true);
}
if (details.getDescription() != null) {
isClient.updateContextParam(id, CSUMMARY_DESCRIPTION, details.getDescription());
isClient.updateContextParam(id, CSUMMARY_DESCRIPTION, details.getDescription(), true);
}
if (details.getLogoUrl() != null) {
isClient.updateContextParam(id, CSUMMARY_LOGOURL, details.getLogoUrl());
isClient.updateContextParam(id, CSUMMARY_LOGOURL, details.getLogoUrl(), true);
}
if (details.getStatus() != null) {
isClient.updateContextParam(id, CSUMMARY_STATUS, details.getStatus().name());
isClient.updateContextParam(id, CSUMMARY_STATUS, details.getStatus().name(), true);
}
if (details.getSubjects() != null) {
isClient.updateContextParam(id, CPROFILE_SUBJECT, Joiner.on(CSV_DELIMITER).join(details.getSubjects()));
isClient.updateContextParam(id, CPROFILE_SUBJECT, Joiner.on(CSV_DELIMITER).join(details.getSubjects()), true);
}
if (details.getFos() != null) {
isClient.updateContextParam(id, CPROFILE_FOS, Joiner.on(CSV_DELIMITER).join(details.getFos()));
isClient.updateContextParam(id, CPROFILE_FOS, Joiner.on(CSV_DELIMITER).join(details.getFos()), true);
}
if (details.getSdg() != null) {
isClient.updateContextParam(id, CPROFILE_SDG, Joiner.on(CSV_DELIMITER).join(details.getSdg()));
isClient.updateContextParam(id, CPROFILE_SDG, Joiner.on(CSV_DELIMITER).join(details.getSdg()), true);
}
if (details.getAdvancedConstraint() != null) {
isClient.updateContextParamNoEscape(id, CPROFILE_ADVANCED_CONSTRAINT, "<![CDATA[" + new Gson().toJson(details.getAdvancedConstraint()) + "]]>");
isClient.updateContextParam(id, CPROFILE_ADVANCED_CONSTRAINT, "<![CDATA[" + new Gson().toJson(details.getAdvancedConstraint()) + "]]>", false);
}
if (details.getMainZenodoCommunity() != null) {
isClient.updateContextParam(id, CSUMMARY_ZENODOC, details.getMainZenodoCommunity());
isClient.updateContextParam(id, CSUMMARY_ZENODOC, details.getMainZenodoCommunity(), true);
}
cc.updateCommunity(id, details);
@ -190,6 +197,10 @@ public class CommunityApiCore {// implements CommunityClient{
log.info("content provider to add " + cp.toString());
if (!StringUtils.equalsIgnoreCase(id, cp.getCommunityId())) { throw new CommunityException("parameters 'id' and cp.communityId must be coherent"); }
return updateContentprovider(id, cp);
}
private CommunityContentprovider updateContentprovider(String id, CommunityContentprovider cp) throws CommunityException, CommunityNotFoundException {
final TreeMap<Integer, CommunityContentprovider> cps = getCommunityContentproviderMap(id);
final String concept_id = cp.getId();
if (concept_id != null && cps.keySet().contains(Integer.valueOf(concept_id))) {
@ -225,6 +236,30 @@ public class CommunityApiCore {// implements CommunityClient{
cc.removeFromCategory(id, CONTENTPROVIDERS_ID_SUFFIX, String.valueOf(contentproviderId));
}
public List<CommunityContentprovider> addCommunityContentProvidersList(String id, List<CommunityContentprovider> contentprovidersList) throws CommunityException, CommunityNotFoundException {
if(contentprovidersList == null || contentprovidersList.size() == 0){
throw new CommunityException("parameter 'contentprovidersList' must be present and should contain at least one content provider");
}
if (!StringUtils.equalsIgnoreCase(id, contentprovidersList.get(0).getCommunityId())) {
throw new CommunityException("parameters 'id' and contentprovider.communityId must be coherent");
}
List<CommunityContentprovider> contentproviders = new ArrayList();
for(CommunityContentprovider contentProvider : contentprovidersList){
contentproviders.add(updateContentprovider(id, contentProvider));
}
return contentproviders;
}
public void removeCommunityContentProviderList(final String id, final List<Integer> contentProviderIdList) throws CommunityException, CommunityNotFoundException {
for(Integer contentProviderId: contentProviderIdList){
removeCommunityContentProvider(id, contentProviderId);
}
}
public void removeCommunityOrganization(final String id, final Integer organizationId) throws CommunityException, CommunityNotFoundException {
final Map<Integer, CommunityOrganization> organizations = getCommunityOrganizationMap(id);
if (!organizations.containsKey(organizationId)) {
@ -278,7 +313,10 @@ public class CommunityApiCore {// implements CommunityClient{
final CommunityDetails cd = new CommunityDetails();
final Set<String> current = Sets.newHashSet(cc.getCommunity(id).getFos());
final Set<String> current = Sets.newHashSet();
if(Optional.ofNullable(cc.getCommunity(id).getFos()).isPresent()){
current.addAll(cc.getCommunity(id).getFos());
}
current.addAll(foss);
@ -307,8 +345,10 @@ public class CommunityApiCore {// implements CommunityClient{
public CommunityDetails addCommunitySDG(final String id, final List<String> sdgs) throws CommunityException, CommunityNotFoundException {
final CommunityDetails cd = new CommunityDetails();
final Set<String> current = Sets.newHashSet(cc.getCommunity(id).getSdg());
final Set<String> current = Sets.newHashSet();
if(Optional.ofNullable(cc.getCommunity(id).getSdg()).isPresent()){
current.addAll(cc.getCommunity(id).getSdg());
}
current.addAll(sdgs);
@ -345,17 +385,15 @@ public class CommunityApiCore {// implements CommunityClient{
return cd;
}
public CommunityDetails removeCommunityAdvancedConstraint(final String id) throws CommunityException, CommunityNotFoundException {
public CommunityDetails removeCommunityAdvancedConstraint(final String id) throws CommunityNotFoundException, CommunityException {
final CommunityDetails cd = new CommunityDetails();
SelectionCriteria sl = new SelectionCriteria();
cd.setAdvancedConstraint(sl);
removeAdvancedConstraint(id);
setCommunity(id, CommunityWritableProperties.fromDetails(cd));
return cd;
return new CommunityDetails();
}
@CacheEvict(value = "community-cache", allEntries = true)
public void removeCommunityZenodoCommunity(final String id, final Integer zenodoCommId) throws CommunityException, CommunityNotFoundException {
@ -463,4 +501,5 @@ public class CommunityApiCore {// implements CommunityClient{
return organization;
}
}

View File

@ -168,6 +168,13 @@ public class CommunityCommon {
}
public void removeAdvancedConstraint(String id) throws CommunityException {
final Context context = getContextMap().get(id);
context.getParams()
.replace(CPROFILE_ADVANCED_CONSTRAINT, Arrays.asList(new Param()
.setName(CPROFILE_ADVANCED_CONSTRAINT).setValue(null)));
}
public void updateCommunity(String id, CommunityWritableProperties community) throws CommunityException {
final Context context = getContextMap().get(id);
@ -202,21 +209,35 @@ public class CommunityCommon {
.join(community.getSubjects()))));
}
if(community.getFos() != null){
if (context.getParams().containsKey(CPROFILE_FOS))
context.getParams()
.replace(CPROFILE_FOS, Arrays.asList(new Param().setName(CPROFILE_FOS)
.setValue(Joiner.on(CSV_DELIMITER)
.join(community.getFos()))));
else
context.getParams().put(CPROFILE_FOS, Arrays.asList(new Param().setName(CPROFILE_FOS)
.setValue(Joiner.on(CSV_DELIMITER)
.join(community.getFos()))));
}
if(community.getSdg() != null){
if(context.getParams().containsKey(CPROFILE_SDG))
context.getParams()
.replace(CPROFILE_SDG, Arrays.asList(new Param().setName(CPROFILE_SDG)
.setValue(Joiner.on(CSV_DELIMITER)
.join(community.getSdg()))));
else
context.getParams().put(CPROFILE_SDG, Arrays.asList(new Param().setName(CPROFILE_SDG)
.setValue(Joiner.on(CSV_DELIMITER)
.join(community.getSdg()))));
}
if (community.getAdvancedConstraint() != null) {
if(context.getParams().containsKey(CPROFILE_ADVANCED_CONSTRAINT))
context.getParams()
.replace(CPROFILE_ADVANCED_CONSTRAINT, Arrays.asList(new Param()
.setName(CPROFILE_ADVANCED_CONSTRAINT).setValue(new Gson().toJson(community.getAdvancedConstraint()))));
else
context.getParams().put(CPROFILE_ADVANCED_CONSTRAINT, Arrays.asList(new Param()
.setName(CPROFILE_ADVANCED_CONSTRAINT).setValue(new Gson().toJson(community.getAdvancedConstraint()))));
}
if(community.getMainZenodoCommunity() != null){
context.getParams()