From bcc5a4aef09c01db73b72b2888dbc1aca9060228 Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Mon, 5 Oct 2020 12:16:49 +0200 Subject: [PATCH] pending orgs management --- .../controller/OrganizationController.java | 19 +++++- .../controller/SuggestionInfo.java | 12 ++++ .../model/view/OrganizationView.java | 15 ++++- .../view/SuggestionInfoViewByCountry.java | 11 ++++ .../OrganizationSimpleViewRepository.java | 4 ++ .../organizations/utils/DatabaseUtils.java | 11 +++- .../src/main/resources/application.properties | 4 +- .../src/main/resources/sql/schema.sql | 14 ++-- .../resources/static/resources/html/edit.html | 5 +- .../resources/html/forms/all_conflicts.html | 4 +- .../resources/html/forms/all_duplicates.html | 4 +- .../resources/html/forms/org_metadata.html | 39 ++++++----- .../resources/html/forms/pending_orgs.html | 43 ++++++++++++ .../resources/html/modals/select_org.html | 7 +- ...org_results.html => org_results_page.html} | 23 +++++-- .../resources/html/resultsByCountry.html | 4 +- .../static/resources/html/resultsByType.html | 4 +- .../static/resources/html/searchResults.html | 4 +- .../static/resources/html/suggestions.html | 4 ++ .../static/resources/js/organizations.js | 65 ++++++++++++++----- .../src/main/resources/templates/home.html | 2 +- 21 files changed, 235 insertions(+), 63 deletions(-) create mode 100644 apps/dnet-orgs-database-application/src/main/resources/static/resources/html/forms/pending_orgs.html rename apps/dnet-orgs-database-application/src/main/resources/static/resources/html/parts/{org_results.html => org_results_page.html} (81%) diff --git a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/controller/OrganizationController.java b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/controller/OrganizationController.java index d3ed7a36..f8ff9c19 100644 --- a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/controller/OrganizationController.java +++ b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/controller/OrganizationController.java @@ -78,7 +78,7 @@ public class OrganizationController { } else if (StringUtils.isBlank(org.getType())) { throw new RuntimeException("Missing field: type"); } else if (UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), authentication.getName())) { - final String orgId = databaseUtils.insertOrUpdateOrganization(org, authentication.getName(), StringUtils.isNotBlank(org.getId())); + final String orgId = databaseUtils.insertOrUpdateOrganization(org, authentication.getName()); return Arrays.asList(orgId); } else { throw new RuntimeException("User not authorized"); @@ -225,6 +225,23 @@ public class OrganizationController { } } + @GetMapping("/byCountry/{status}/{code}") + public Iterable findPendingOrgsByCountry(@PathVariable final String status, + @PathVariable final String code, + final Authentication authentication) { + if (UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForCountry(code, authentication.getName())) { + if (status.equalsIgnoreCase("approved")) { + return organizationSimpleViewRepository.findByCountryAndApprovedOrderByName(code, true); + } else if (status.equalsIgnoreCase("pending")) { + return organizationSimpleViewRepository.findByCountryAndApprovedOrderByName(code, false); + } else { + return organizationSimpleViewRepository.findByCountryOrderByName(code); + } + } else { + throw new RuntimeException("User not authorized"); + } + } + @GetMapping("/byType/{type}/{page}/{size}") public Page findByType(@PathVariable final String type, @PathVariable final int page, diff --git a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/controller/SuggestionInfo.java b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/controller/SuggestionInfo.java index ca88659b..8e0c0fe0 100644 --- a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/controller/SuggestionInfo.java +++ b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/controller/SuggestionInfo.java @@ -13,6 +13,8 @@ public class SuggestionInfo { private long nConflicts = 0; + private long nPendingOrgs = 0; + public long getnDuplicates() { return nDuplicates; } @@ -29,10 +31,20 @@ public class SuggestionInfo { this.nConflicts = nConflicts; } + public long getnPendingOrgs() { + return nPendingOrgs; + } + + public void setnPenfingOrgs(final long nPendingOrgs) { + this.nPendingOrgs = nPendingOrgs; + } + public void add(final SuggestionInfoViewByCountry infoCountry) { nDuplicates += infoCountry.getnDuplicates(); nConflicts += infoCountry.getnConflicts(); + nPendingOrgs += infoCountry.getnPendingOrgs(); } + } public SuggestionCounter total = new SuggestionCounter();; diff --git a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/model/view/OrganizationView.java b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/model/view/OrganizationView.java index 503f5594..6ff96744 100644 --- a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/model/view/OrganizationView.java +++ b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/model/view/OrganizationView.java @@ -18,8 +18,8 @@ import com.vladmihalcea.hibernate.type.json.JsonStringType; @Entity @Table(name = "organizations_view") @TypeDefs({ - @TypeDef(name = "json", typeClass = JsonStringType.class), - @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class) + @TypeDef(name = "json", typeClass = JsonStringType.class), + @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class) }) public class OrganizationView implements Serializable { @@ -70,6 +70,9 @@ public class OrganizationView implements Serializable { @Column(name = "relations", columnDefinition = "jsonb") private Set relations; + @Column(name = "approved") + private boolean approved = false; + public String getId() { return id; } @@ -166,4 +169,12 @@ public class OrganizationView implements Serializable { this.relations = relations; } + public boolean isApproved() { + return approved; + } + + public void setApproved(final boolean approved) { + this.approved = approved; + } + } diff --git a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/model/view/SuggestionInfoViewByCountry.java b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/model/view/SuggestionInfoViewByCountry.java index 35ec70b4..ef8125b3 100644 --- a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/model/view/SuggestionInfoViewByCountry.java +++ b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/model/view/SuggestionInfoViewByCountry.java @@ -26,6 +26,9 @@ public class SuggestionInfoViewByCountry implements Serializable { @Column(name = "n_conflicts") private long nConflicts; + @Column(name = "n_pending_orgs") + private long nPendingOrgs; + public String getCountry() { return country; } @@ -50,4 +53,12 @@ public class SuggestionInfoViewByCountry implements Serializable { this.nConflicts = nConflicts; } + public long getnPendingOrgs() { + return nPendingOrgs; + } + + public void setnPendingOrgs(final long nPendingOrgs) { + this.nPendingOrgs = nPendingOrgs; + } + } diff --git a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/repository/readonly/OrganizationSimpleViewRepository.java b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/repository/readonly/OrganizationSimpleViewRepository.java index e549c51d..8bbd490c 100644 --- a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/repository/readonly/OrganizationSimpleViewRepository.java +++ b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/repository/readonly/OrganizationSimpleViewRepository.java @@ -21,6 +21,10 @@ public interface OrganizationSimpleViewRepository extends ReadOnlyRepository findByCountryOrderByName(String country, Pageable pageable); + Iterable findByCountryOrderByName(String code); + + Iterable findByCountryAndApprovedOrderByName(String code, boolean approved); + Page findByTypeOrderByName(String type, Pageable pageable); @Query(value = "select o.* from organizations_simple_view o left outer join user_countries uc on (uc.country = o.country) where uc.email = ?2 and o.type = ?1 order by o.name", nativeQuery = true) diff --git a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/utils/DatabaseUtils.java b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/utils/DatabaseUtils.java index 58d84c19..06c1b6cd 100644 --- a/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/utils/DatabaseUtils.java +++ b/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/utils/DatabaseUtils.java @@ -15,6 +15,7 @@ import java.util.stream.Collectors; import javax.transaction.Transactional; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -89,12 +90,20 @@ public class DatabaseUtils { } @Transactional - public String insertOrUpdateOrganization(final OrganizationView orgView, final String user, final boolean update) { + public String insertOrUpdateOrganization(final OrganizationView orgView, final String user) { + + final boolean update = StringUtils.isNotBlank(orgView.getId()); if (update) { cleanOldRelations(orgView.getId()); } + if (!orgView.isApproved()) { + cleanOldRelations(orgView.getId()); + organizationRepository.deleteById(orgView.getId()); + orgView.setId(null); + } + final Organization org = new Organization(update ? orgView.getId() : null, orgView.getName(), orgView.getType(), diff --git a/apps/dnet-orgs-database-application/src/main/resources/application.properties b/apps/dnet-orgs-database-application/src/main/resources/application.properties index 3e970a79..f1ff84e8 100644 --- a/apps/dnet-orgs-database-application/src/main/resources/application.properties +++ b/apps/dnet-orgs-database-application/src/main/resources/application.properties @@ -1,8 +1,8 @@ spring.main.banner-mode = off logging.level.root = INFO -spring.datasource.url=jdbc:postgresql://localhost:5432/dnet_orgs -spring.datasource.username= +spring.datasource.url=jdbc:postgresql://localhost:5432/oa_organizations +spring.datasource.username=oa_organizations spring.datasource.password= spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect diff --git a/apps/dnet-orgs-database-application/src/main/resources/sql/schema.sql b/apps/dnet-orgs-database-application/src/main/resources/sql/schema.sql index f1faea15..6191666f 100644 --- a/apps/dnet-orgs-database-application/src/main/resources/sql/schema.sql +++ b/apps/dnet-orgs-database-application/src/main/resources/sql/schema.sql @@ -165,6 +165,7 @@ CREATE VIEW organizations_view AS SELECT org.lng, org.city, org.country, + org.approved, COALESCE(jsonb_agg(DISTINCT jsonb_build_object('id', oid.otherid, 'type', oid.type)) FILTER (WHERE oid.otherid IS NOT NULL), '[]') AS other_ids, COALESCE(jsonb_agg(DISTINCT jsonb_build_object('name', n.name, 'lang', n.lang)) FILTER (WHERE n.name IS NOT NULL), '[]') AS other_names, COALESCE(jsonb_agg(DISTINCT a.acronym) FILTER (WHERE a.acronym IS NOT NULL), '[]') AS acronyms, @@ -185,7 +186,8 @@ GROUP BY org.lat, org.lng, org.city, - org.country; + org.country, + org.approved; CREATE VIEW organizations_info_view AS SELECT org.id, @@ -232,11 +234,13 @@ GROUP BY u.email, u.valid, u.role ORDER BY u.email; CREATE VIEW suggestions_info_by_country_view AS SELECT c.val AS country, - coalesce(t1.n_duplicates, 0) AS n_duplicates, - coalesce(t2.n_conflicts, 0) AS n_conflicts + coalesce(t1.n_duplicates, 0) AS n_duplicates, + coalesce(t2.n_conflicts, 0) AS n_conflicts, + coalesce(t3.n_pending_orgs, 0) AS n_pending_orgs FROM countries c - LEFT OUTER JOIN (SELECT o.country AS country, count(DISTINCT d.*) AS n_duplicates FROM oa_duplicates d LEFT OUTER JOIN organizations o ON (d.local_id = o.id) WHERE d.reltype = 'suggested' GROUP BY o.country) AS t1 ON (t1.country = c.val) - LEFT OUTER JOIN (SELECT o.country AS country, count(DISTINCT c.idgroup) AS n_conflicts FROM oa_conflicts c LEFT OUTER JOIN organizations o ON (c.id1 = o.id) WHERE c.reltype = 'suggested' GROUP BY o.country) AS t2 ON (t2.country = c.val); + LEFT OUTER JOIN (SELECT o.country AS country, count(DISTINCT d.*) AS n_duplicates FROM oa_duplicates d LEFT OUTER JOIN organizations o ON (d.local_id = o.id) WHERE d.reltype = 'suggested' GROUP BY o.country) AS t1 ON (t1.country = c.val) + LEFT OUTER JOIN (SELECT o.country AS country, count(DISTINCT c.idgroup) AS n_conflicts FROM oa_conflicts c LEFT OUTER JOIN organizations o ON (c.id1 = o.id) WHERE c.reltype = 'suggested' GROUP BY o.country) AS t2 ON (t2.country = c.val) + LEFT OUTER JOIN (SELECT o.country AS country, count(DISTINCT o.id) AS n_pending_orgs FROM organizations o WHERE o.approved = false GROUP BY o.country) AS t3 ON (t3.country = c.val); CREATE VIEW conflict_groups_view AS SELECT c.idgroup AS idgroup, diff --git a/apps/dnet-orgs-database-application/src/main/resources/static/resources/html/edit.html b/apps/dnet-orgs-database-application/src/main/resources/static/resources/html/edit.html index b8a8ae36..2725085b 100644 --- a/apps/dnet-orgs-database-application/src/main/resources/static/resources/html/edit.html +++ b/apps/dnet-orgs-database-application/src/main/resources/static/resources/html/edit.html @@ -9,6 +9,9 @@
- + + + +
diff --git a/apps/dnet-orgs-database-application/src/main/resources/static/resources/html/forms/all_conflicts.html b/apps/dnet-orgs-database-application/src/main/resources/static/resources/html/forms/all_conflicts.html index 2eaa36a2..1077c4a1 100644 --- a/apps/dnet-orgs-database-application/src/main/resources/static/resources/html/forms/all_conflicts.html +++ b/apps/dnet-orgs-database-application/src/main/resources/static/resources/html/forms/all_conflicts.html @@ -2,10 +2,10 @@
No suggestions
-
+
- Country: + Country: