fixed triggers

This commit is contained in:
Michele Artini 2020-10-07 17:04:29 +02:00
parent 9458a1dc40
commit 4e7c5c78c1
7 changed files with 40 additions and 30 deletions

View File

@ -43,7 +43,6 @@ import eu.dnetlib.organizations.repository.readonly.OrganizationSimpleViewReposi
import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository; import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository;
import eu.dnetlib.organizations.repository.readonly.SuggestionInfoViewByCountryRepository; import eu.dnetlib.organizations.repository.readonly.SuggestionInfoViewByCountryRepository;
import eu.dnetlib.organizations.utils.DatabaseUtils; import eu.dnetlib.organizations.utils.DatabaseUtils;
import eu.dnetlib.organizations.utils.RelationType;
@RestController @RestController
@RequestMapping("/api/organizations") @RequestMapping("/api/organizations")
@ -270,10 +269,9 @@ public class OrganizationController {
return otherIds.stream() return otherIds.stream()
.filter(id -> UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForId(id, authentication.getName())) .filter(id -> UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForId(id, authentication.getName()))
.map(id -> databaseUtils.makeRelation(masterId, id, RelationType.Merges)) .map(id -> databaseUtils.fixDuplicate(masterId, id))
.flatMap(List::stream) .flatMap(List::stream)
.collect(Collectors.toList()); .collect(Collectors.toList());
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -18,4 +18,8 @@ public interface OrganizationRepository extends JpaRepository<Organization, Stri
@Query("update Organization set modified_by = ?2, modification_date = ?3 where id = ?1") @Query("update Organization set modified_by = ?2, modification_date = ?3 where id = ?1")
void updateModificationDate(String id, String user, OffsetDateTime now); void updateModificationDate(String id, String user, OffsetDateTime now);
@Modifying
@Query("update Organization set status = ?2 where id = ?1")
void updateStatus(String id, String status);
} }

View File

@ -91,36 +91,38 @@ public class DatabaseUtils {
@Transactional @Transactional
public String insertOrUpdateOrganization(final OrganizationView orgView, final String user) { public String insertOrUpdateOrganization(final OrganizationView orgView, final String user) {
final boolean alreadyApproved = StringUtils.equals(orgView.getStatus(), OrganizationStatus.approved.toString());
final boolean update = StringUtils.isNotBlank(orgView.getId()); final String oldId = orgView.getId();
if (update) { if (StringUtils.isBlank(orgView.getId())) {
cleanOldRelations(orgView.getId());
}
if (!StringUtils.equals(orgView.getStatus(), OrganizationStatus.approved.toString())) {
cleanOldRelations(orgView.getId());
organizationRepository.deleteById(orgView.getId());
orgView.setId(null); orgView.setId(null);
} else if (!alreadyApproved) {
cleanOldRelations(oldId);
organizationRepository.deleteById(oldId);
orgView.setId(null);
} else {
cleanOldRelations(orgView.getId());
} }
final Organization org = new Organization(update ? orgView.getId() : null, final Organization org = new Organization(orgView.getId(),
orgView.getName(), orgView.getName(),
orgView.getType(), orgView.getType(),
orgView.getLat(), orgView.getLng(), orgView.getLat(), orgView.getLng(),
orgView.getCity(), orgView.getCountry(), orgView.getCity(), orgView.getCountry(),
OrganizationStatus.approved.toString()); OrganizationStatus.approved.toString());
final String orgId = organizationRepository.save(org).getId(); final String newId = organizationRepository.save(org).getId();
makeNewRelations(orgView, orgId); makeNewRelations(newId, orgView);
updateHistoryFields(orgId, user, update); updateHistoryFields(newId, user, alreadyApproved);
return orgId; return newId;
} }
private void updateHistoryFields(final String id, final String user, final boolean update) { private void updateHistoryFields(final String id, final String user, final boolean update) {
final OffsetDateTime now = OffsetDateTime.now(); final OffsetDateTime now = OffsetDateTime.now();
if (update) { if (update) {
organizationRepository.updateModificationDate(id, user, now); organizationRepository.updateModificationDate(id, user, now);
@ -139,7 +141,7 @@ public class DatabaseUtils {
return list; return list;
} }
private void makeNewRelations(final OrganizationView orgView, final String orgId) { private void makeNewRelations(final String orgId, final OrganizationView orgView) {
orgView.getAcronyms().forEach(s -> acronymRepository.save(new Acronym(orgId, s))); orgView.getAcronyms().forEach(s -> acronymRepository.save(new Acronym(orgId, s)));
orgView.getOtherNames().forEach(n -> otherNameRepository.save(new OtherName(orgId, n.getName(), n.getLang()))); orgView.getOtherNames().forEach(n -> otherNameRepository.save(new OtherName(orgId, n.getName(), n.getLang())));
orgView.getOtherIdentifiers().forEach(id -> otherIdentifierRepository.save(new OtherIdentifier(orgId, id.getId(), id.getType()))); orgView.getOtherIdentifiers().forEach(id -> otherIdentifierRepository.save(new OtherIdentifier(orgId, id.getId(), id.getType())));
@ -268,12 +270,6 @@ public class DatabaseUtils {
final Relationship r2 = new Relationship(id2, id1, type.getInverse().toString()); final Relationship r2 = new Relationship(id2, id1, type.getInverse().toString());
relationshipRepository.save(r1); relationshipRepository.save(r1);
relationshipRepository.save(r2); relationshipRepository.save(r2);
if (type == RelationType.Merged_In || type == RelationType.Merges) {
openaireConflictRepository.findById(new OpenaireConflictPK(id1, id2)).ifPresent(openaireConflictRepository::delete);
openaireConflictRepository.findById(new OpenaireConflictPK(id2, id1)).ifPresent(openaireConflictRepository::delete);
}
return Arrays.asList(r1, r2); return Arrays.asList(r1, r2);
} }
@ -330,4 +326,15 @@ public class DatabaseUtils {
} }
} }
@Transactional
public List<Relationship> fixDuplicate(final String masterId, final String otherId) {
organizationRepository.updateStatus(otherId, OrganizationStatus.hidden.toString());
openaireConflictRepository.findById(new OpenaireConflictPK(masterId, otherId)).ifPresent(openaireConflictRepository::delete);
openaireConflictRepository.findById(new OpenaireConflictPK(otherId, masterId)).ifPresent(openaireConflictRepository::delete);
// TODO Merge the organizations ???
return makeRelation(masterId, otherId, RelationType.Merges);
}
} }

View File

@ -4,5 +4,6 @@ public enum OrganizationStatus {
pending, pending,
approved, approved,
discarded, discarded,
hidden hidden,
deleted
} }

View File

@ -289,7 +289,7 @@ GROUP BY o.id, o.name;
CREATE OR REPLACE FUNCTION delete_index_search() RETURNS trigger LANGUAGE plpgsql AS $$ CREATE OR REPLACE FUNCTION delete_index_search() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN BEGIN
DELETE FROM org_index_search WHERE id = old.id; DELETE FROM org_index_search WHERE id = old.id;
RETURN NULL; RETURN OLD;
END; END;
$$; $$;
@ -304,7 +304,7 @@ BEGIN
WHERE o.id = new.id WHERE o.id = new.id
GROUP BY o.id, o.name) GROUP BY o.id, o.name)
ON CONFLICT (id) DO UPDATE SET txt = EXCLUDED.txt; ON CONFLICT (id) DO UPDATE SET txt = EXCLUDED.txt;
RETURN NULL; RETURN NEW;
END; END;
$$; $$;

View File

@ -34,8 +34,8 @@
<div class="card-body"> <div class="card-body">
<h5 class="card-title">Authorization request</h5> <h5 class="card-title">Authorization request</h5>
<p class="card-text" th:inline="text"> <p class="card-text" th:inline="text">
Hello '[[${#httpServletRequest.remoteUser}]]', you do not have permission to ... <br /> Hello '[[${#httpServletRequest.remoteUser}]]', you don't have a role yet <br />
If you want to contribute to ... compile the form, an administrator will authorize you as soon as possible. To apply as data curator compile the form below, an administrator will authorize you as soon as possible.
</p> </p>
<form class="small"> <form class="small">

View File

@ -69,8 +69,8 @@
<p class="card-text mb-4"> <p class="card-text mb-4">
To use this service you have to perform the following steps: To use this service you have to perform the following steps:
<ol> <ol>
<li>Register yourself on the OpenAIRE portal.</li> <li>Register on the OpenAIRE portal.</li>
<li>Perform login using the OpenAIRE credentials</li> <li>Login using the OpenAIRE credentials</li>
<li>Compile the Authorization Request Form</li> <li>Compile the Authorization Request Form</li>
<li>An administrator will authorize you as soon as possible</li> <li>An administrator will authorize you as soon as possible</li>
</ol> </ol>