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.SuggestionInfoViewByCountryRepository;
import eu.dnetlib.organizations.utils.DatabaseUtils;
import eu.dnetlib.organizations.utils.RelationType;
@RestController
@RequestMapping("/api/organizations")
@ -270,10 +269,9 @@ public class OrganizationController {
return otherIds.stream()
.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)
.collect(Collectors.toList());
} else {
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")
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
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) {
cleanOldRelations(orgView.getId());
}
if (!StringUtils.equals(orgView.getStatus(), OrganizationStatus.approved.toString())) {
cleanOldRelations(orgView.getId());
organizationRepository.deleteById(orgView.getId());
if (StringUtils.isBlank(orgView.getId())) {
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.getType(),
orgView.getLat(), orgView.getLng(),
orgView.getCity(), orgView.getCountry(),
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) {
final OffsetDateTime now = OffsetDateTime.now();
if (update) {
organizationRepository.updateModificationDate(id, user, now);
@ -139,7 +141,7 @@ public class DatabaseUtils {
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.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())));
@ -268,12 +270,6 @@ public class DatabaseUtils {
final Relationship r2 = new Relationship(id2, id1, type.getInverse().toString());
relationshipRepository.save(r1);
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);
}
@ -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,
approved,
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 $$
BEGIN
DELETE FROM org_index_search WHERE id = old.id;
RETURN NULL;
RETURN OLD;
END;
$$;
@ -304,7 +304,7 @@ BEGIN
WHERE o.id = new.id
GROUP BY o.id, o.name)
ON CONFLICT (id) DO UPDATE SET txt = EXCLUDED.txt;
RETURN NULL;
RETURN NEW;
END;
$$;

View File

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

View File

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