subcommunities

This commit is contained in:
Michele Artini 2023-06-27 12:37:23 +02:00
parent 566e94d67b
commit de36ad03e6
6 changed files with 182 additions and 13 deletions

View File

@ -3,6 +3,7 @@ package eu.dnetlib.openaire.community;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Date;
@ -33,6 +34,7 @@ import eu.dnetlib.openaire.exporter.model.community.CommunityProject;
import eu.dnetlib.openaire.exporter.model.community.CommunityStatus;
import eu.dnetlib.openaire.exporter.model.community.CommunitySummary;
import eu.dnetlib.openaire.exporter.model.community.CommunityType;
import eu.dnetlib.openaire.exporter.model.community.SubCommunity;
import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionCriteria;
import eu.dnetlib.openaire.exporter.model.context.Category;
import eu.dnetlib.openaire.exporter.model.context.Concept;
@ -147,15 +149,21 @@ public class CommunityImporterController {
final List<CommunityContentprovider> datasources =
getCommunityInfo(context, CONTENTPROVIDERS_ID_SUFFIX, c -> asCommunityDataprovider(context.getId(), c));
final List<CommunityProject> projects =
getCommunityInfo(context, PROJECTS_ID_SUFFIX, c -> asCommunityProject(context.getId(), c));
final List<CommunityOrganization> orgs =
getCommunityInfo(context, ORGANIZATION_ID_SUFFIX, c -> asCommunityOrganization(context.getId(), c));
final List<SubCommunity> subs = new ArrayList<>();
// TODO populate the subs using all the other categories (ie: not projects, organizations and contentproviders)
service.saveCommunity(community);
service.addCommunityProjectList(context.getId(), projects);
service.addCommunityContentProvidersList(context.getId(), datasources);
service.addCommunityOrganizationList(context.getId(), orgs);
service.addSubCommunityList(subs);
// TODO MANAGE new fields and tables
} catch (final Exception e) {
throw new RuntimeException("Error importing community: " + context.getId(), e);
@ -184,11 +192,17 @@ public class CommunityImporterController {
}
if (params.containsKey(CSUMMARY_NAME)) {
summary.setName(asCsv(params.get(CSUMMARY_NAME)));
} else {
summary.setName(c.getLabel());
}
// TODO: gestire meglio le zenodo communities (main + other)
if (params.containsKey(CSUMMARY_ZENODOC)) {
summary.setZenodoCommunity(asCsv(params.get(CSUMMARY_ZENODOC)));
}
// TODO: Considerare anche i campi membership e claim
return summary;
}
@ -211,6 +225,9 @@ public class CommunityImporterController {
p.setAdvancedConstraints(SelectionCriteria.fromJson(asCsv(params.get(CPROFILE_ADVANCED_CONSTRAINT))));
}
// TODO Vanno considerati anche i REMOVE_CONSTRAINTS ?
if (params.containsKey(CPROFILE_CREATIONDATE)) {
try {
final Date d = org.apache.commons.lang3.time.DateUtils.parseDate(asCsv(params.get(CPROFILE_CREATIONDATE)), pattern);
@ -224,26 +241,21 @@ public class CommunityImporterController {
}
private static CommunityProject asCommunityProject(final String communityId, final Concept c) {
final Map<String, List<Param>> p = c.getParams();
final CommunityProject project = new CommunityProject();
project.setCommunityId(communityId);
// project.setId(StringUtils.substringAfterLast(c.getId(), ID_SEPARATOR));
project.setOpenaireId(firstValue(p, OPENAIRE_ID));
project.setFunder(firstValue(p, CPROJECT_FUNDER));
project.setGrantId(firstValue(p, CPROJECT_NUMBER));
project.setName(firstValue(p, CPROJECT_FULLNAME));
project.setAcronym(firstValue(p, CPROJECT_ACRONYM));
return project;
}
private static CommunityContentprovider asCommunityDataprovider(final String communityId, final Concept c) {
final Map<String, List<Param>> p = c.getParams();
final CommunityContentprovider d = new CommunityContentprovider();
d.setCommunityId(communityId);
// d.setId(StringUtils.substringAfterLast(c.getId(), ID_SEPARATOR));
d.setOpenaireId(firstValue(p, OPENAIRE_ID));
d.setName(firstValue(p, CCONTENTPROVIDER_NAME));
d.setOfficialname(firstValue(p, CCONTENTPROVIDER_OFFICIALNAME));
@ -259,10 +271,25 @@ public class CommunityImporterController {
o.setName(firstValue(p, CORGANIZATION_NAME));
o.setLogo_url(getDecodedUrl(firstValue(p, CORGANIZATION_LOGOURL)));
o.setWebsite_url(getDecodedUrl(firstValue(p, CORGANIZATION_WEBSITEURL)));
return o;
}
// TODO
private static List<SubCommunity> asSubCommunities(final String communityId, final String parent, final String category, final Concept c) {
final SubCommunity sc = new SubCommunity();
sc.setSubCommunityId(c.getId());
sc.setCommunityId(communityId);
sc.setParent(parent);
sc.setCategory(category);
sc.setLabel(c.getLabel());
sc.setParams(c.getParams());
final List<SubCommunity> list = new ArrayList<>();
list.add(sc);
c.getConcepts().forEach(child -> list.addAll(asSubCommunities(communityId, c.getId(), category, child)));
return list;
}
private static String getDecodedUrl(final String encoded_url) {
if (encoded_url == null) { return encoded_url; }
return new String(Base64.getDecoder().decode(encoded_url));

View File

@ -21,11 +21,13 @@ import eu.dnetlib.openaire.community.db.model.DbDatasource;
import eu.dnetlib.openaire.community.db.model.DbDatasourcePK;
import eu.dnetlib.openaire.community.db.model.DbProject;
import eu.dnetlib.openaire.community.db.model.DbProjectPK;
import eu.dnetlib.openaire.community.db.model.DbSubCommunity;
import eu.dnetlib.openaire.community.db.model.DbSupportOrg;
import eu.dnetlib.openaire.community.db.repository.DbCommunityRepository;
import eu.dnetlib.openaire.community.db.repository.DbDatasourceRepository;
import eu.dnetlib.openaire.community.db.repository.DbOrganizationRepository;
import eu.dnetlib.openaire.community.db.repository.DbProjectRepository;
import eu.dnetlib.openaire.community.db.repository.DbSubCommunityRepository;
import eu.dnetlib.openaire.community.db.repository.DbSupportOrgRepository;
import eu.dnetlib.openaire.community.db.utils.CommunityMappingUtils;
import eu.dnetlib.openaire.exporter.exceptions.CommunityException;
@ -36,6 +38,7 @@ import eu.dnetlib.openaire.exporter.model.community.CommunityOrganization;
import eu.dnetlib.openaire.exporter.model.community.CommunityProject;
import eu.dnetlib.openaire.exporter.model.community.CommunitySummary;
import eu.dnetlib.openaire.exporter.model.community.CommunityWritableProperties;
import eu.dnetlib.openaire.exporter.model.community.SubCommunity;
import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionCriteria;
@Service
@ -60,6 +63,8 @@ public class CommunityService {
private DbOrganizationRepository dbOrganizationRepository;
@Autowired
private DbSupportOrgRepository dbSupportOrgRepository;
@Autowired
private DbSubCommunityRepository dbSubCommunityRepository;
public List<CommunitySummary> listCommunities() throws CommunityException {
return dbCommunityRepository.findAll()
@ -163,7 +168,7 @@ public class CommunityService {
public List<CommunityOrganization> getCommunityOrganizations(final String id) throws CommunityException, ResourceNotFoundException {
return dbSupportOrgRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toCommunityOrganiztion)
.map(CommunityMappingUtils::toCommunityOrganization)
.collect(Collectors.toList());
}
@ -186,6 +191,34 @@ public class CommunityService {
return orgList;
}
public void removeSubCommunity(final String id, final String subCommunityId) throws CommunityException, ResourceNotFoundException {
dbSubCommunityRepository.deleteById(subCommunityId);
}
public List<SubCommunity> getSubCommunities(final String id) throws CommunityException, ResourceNotFoundException {
return dbSubCommunityRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toSubCommunity)
.collect(Collectors.toList());
}
public SubCommunity addSubCommunity(final SubCommunity sub) {
final DbSubCommunity sc = CommunityMappingUtils.toDbSubCommunity(sub);
dbSubCommunityRepository.save(sc);
return sub;
}
public List<SubCommunity> addSubCommunityList(final List<SubCommunity> subs) {
final List<DbSubCommunity> list = subs.stream()
.map(CommunityMappingUtils::toDbSubCommunity)
.collect(Collectors.toList());
dbSubCommunityRepository.saveAll(list);
return subs;
}
public CommunityDetails addCommunitySubjects(final String id, final String... subjects) throws CommunityException, ResourceNotFoundException {
return modifyElementToArrayField(id, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), false, subjects);
}

View File

@ -1,6 +1,7 @@
package eu.dnetlib.openaire.community.db.model;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.persistence.Column;
@ -16,6 +17,8 @@ import com.vladmihalcea.hibernate.type.array.StringArrayType;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import eu.dnetlib.openaire.exporter.model.context.Param;
@Entity
@Table(name = "community_subs")
@TypeDefs({
@ -42,7 +45,7 @@ public class DbSubCommunity implements Serializable {
@Type(type = "jsonb")
@Column(name = "params")
private Map<String, String> params;
private Map<String, List<Param>> params;
@Column(name = "parent")
private String parent;
@ -79,11 +82,11 @@ public class DbSubCommunity implements Serializable {
this.category = category;
}
public Map<String, String> getParams() {
public Map<String, List<Param>> getParams() {
return params;
}
public void setParams(final Map<String, String> params) {
public void setParams(final Map<String, List<Param>> params) {
this.params = params;
}

View File

@ -10,8 +10,8 @@ import eu.dnetlib.openaire.community.db.model.DbSubCommunity;
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
public interface DbSubCommunityRepository extends JpaRepository<DbSubCommunity, String> {
List<DbSubCommunityRepository> findByCommunity(String community);
List<DbSubCommunity> findByCommunity(String community);
List<DbSubCommunityRepository> findByCommunityAndParent(String community, String parent);
List<DbSubCommunity> findByCommunityAndParent(String community, String parent);
}

View File

@ -10,6 +10,7 @@ import org.apache.commons.lang3.StringUtils;
import eu.dnetlib.openaire.community.db.model.DbCommunity;
import eu.dnetlib.openaire.community.db.model.DbDatasource;
import eu.dnetlib.openaire.community.db.model.DbProject;
import eu.dnetlib.openaire.community.db.model.DbSubCommunity;
import eu.dnetlib.openaire.community.db.model.DbSupportOrg;
import eu.dnetlib.openaire.exporter.model.community.CommunityContentprovider;
import eu.dnetlib.openaire.exporter.model.community.CommunityDetails;
@ -17,6 +18,7 @@ import eu.dnetlib.openaire.exporter.model.community.CommunityOrganization;
import eu.dnetlib.openaire.exporter.model.community.CommunityProject;
import eu.dnetlib.openaire.exporter.model.community.CommunitySummary;
import eu.dnetlib.openaire.exporter.model.community.CommunityWritableProperties;
import eu.dnetlib.openaire.exporter.model.community.SubCommunity;
public class CommunityMappingUtils {
@ -171,7 +173,7 @@ public class CommunityMappingUtils {
return ds;
}
public static CommunityOrganization toCommunityOrganiztion(final DbSupportOrg dbEntry) {
public static CommunityOrganization toCommunityOrganization(final DbSupportOrg dbEntry) {
final CommunityOrganization co = new CommunityOrganization();
co.setCommunityId(dbEntry.getCommunity());
co.setName(dbEntry.getOrgName());
@ -189,7 +191,30 @@ public class CommunityMappingUtils {
return dbo;
}
public static DbSubCommunity toDbSubCommunity(final SubCommunity sub) {
final DbSubCommunity dbsc = new DbSubCommunity();
dbsc.setCommunity(sub.getCommunityId());
dbsc.setId(sub.getSubCommunityId());
dbsc.setCategory(sub.getCategory());
dbsc.setLabel(sub.getLabel());
dbsc.setParams(sub.getParams());
dbsc.setParent(sub.getParent());
return dbsc;
}
public static SubCommunity toSubCommunity(final DbSubCommunity sub) {
final SubCommunity sc = new SubCommunity();
sc.setSubCommunityId(sub.getId());
sc.setCategory(sub.getCategory());
sc.setCommunityId(sub.getCommunity());
sc.setLabel(sub.getLabel());
sc.setParams(sub.getParams());
sc.setParent(sub.getParent());
return sc;
}
private static String[] toStringArray(final List<String> list) {
return list.toArray(new String[list.size()]);
}
}

View File

@ -0,0 +1,81 @@
package eu.dnetlib.openaire.exporter.model.community;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import eu.dnetlib.openaire.exporter.model.context.Param;
import io.swagger.v3.oas.annotations.media.Schema;
@JsonAutoDetect
public class SubCommunity {
@Schema(description = "the id of the subCommunity", required = true)
private String subCommunityId;
@Schema(description = "the community identifier this sub community belongs to", required = true)
private String communityId;
@Schema(description = "the parent of the subCommunity, if available (it should the id of another subCommunity)", required = false)
private String parent;
@Schema(description = "the label of the subCommunity", required = true)
private String label;
@Schema(description = "the category of the subCommunity", required = true)
private String category;
@Schema(description = "the parameters of the subCommunity", required = true)
private Map<String, List<Param>> params = new LinkedHashMap<>();
public String getSubCommunityId() {
return subCommunityId;
}
public void setSubCommunityId(final String subCommunityId) {
this.subCommunityId = subCommunityId;
}
public String getCommunityId() {
return communityId;
}
public void setCommunityId(final String communityId) {
this.communityId = communityId;
}
public String getParent() {
return parent;
}
public void setParent(final String parent) {
this.parent = parent;
}
public String getLabel() {
return label;
}
public void setLabel(final String label) {
this.label = label;
}
public String getCategory() {
return category;
}
public void setCategory(final String category) {
this.category = category;
}
public Map<String, List<Param>> getParams() {
return params;
}
public void setParams(final Map<String, List<Param>> map) {
this.params = map;
}
}