Skip to content

Commit

Permalink
fix(faidare): various fixes to faidare services
Browse files Browse the repository at this point in the history
OpenSILEX/opensilex-dev!1234
  • Loading branch information
BESOMBES Gabriel committed Jun 25, 2024
1 parent 46dd15e commit 14a7b7b
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//******************************************************************************
// OpenSILEX - Licence AGPL V3.0 - https://www.gnu.org/licenses/agpl-3.0.en.html
// Copyright © INRA 2019
// Contact: vincent.migot@inra.fr, anne.tireau@inra.fr, pascal.neveu@inra.fr
//******************************************************************************
/*
* *****************************************************************************
* OrganizationDAO.java
* OpenSILEX - Licence AGPL V3.0 - https://www.gnu.org/licenses/agpl-3.0.en.html
* Copyright © INRAE 2024.
* Last Modification: 25/06/2024 10:10
* Contact: vincent.migot@inra.fr, anne.tireau@inra.fr, pascal.neveu@inra.fr, gabriel.besombes@inrae.fr
* *****************************************************************************
*/
package org.opensilex.core.organisation.dal;

import com.github.benmanes.caffeine.cache.Cache;
Expand Down Expand Up @@ -54,6 +58,10 @@ public class OrganizationDAO {
private static final Cache<URI, Map<URI, OrganizationModel>> userOrganizationCache = Caffeine.newBuilder()
.build();

public void invalidateCache() {
userOrganizationCache.invalidateAll();
}

public OrganizationDAO(SPARQLService sparql, MongoDBService nosql) throws Exception {
this.sparql = sparql;
this.nosql = nosql;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*******************************************************************************
/*
* *****************************************************************************
* FacilityDAO.java
* OpenSILEX - Licence AGPL V3.0 - https://www.gnu.org/licenses/agpl-3.0.en.html
* Copyright © INRAE 2022.
* Last Modification: 28/10/2022
* Contact: valentin.rigolle@inrae.fr, anne.tireau@inrae.fr, pascal.neveu@inrae.fr
*
******************************************************************************/
* Copyright © INRAE 2024.
* Last Modification: 25/06/2024 10:10
* Contact: valentin.rigolle@inrae.fr, anne.tireau@inrae.fr, pascal.neveu@inrae.fr, gabriel.besombes@inrae.fr
* *****************************************************************************
*/
package org.opensilex.core.organisation.dal.facility;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.mongodb.client.model.geojson.Geometry;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -22,7 +22,6 @@
import org.opensilex.core.geospatial.dal.GeospatialModel;
import org.opensilex.core.ontology.Oeso;
import org.opensilex.core.organisation.api.facility.FacilityAddressDTO;
import org.opensilex.core.organisation.api.site.SiteAddressDTO;
import org.opensilex.core.organisation.dal.OrganizationDAO;
import org.opensilex.core.organisation.dal.OrganizationModel;
import org.opensilex.core.organisation.dal.OrganizationSPARQLHelper;
Expand Down Expand Up @@ -102,6 +101,7 @@ public FacilityModel create(FacilityModel instance, Geometry geometry, AccountMo

createFacilityGeospatialModel(instance, geometry);

organizationDAO.invalidateCache();
return instance;
}

Expand Down Expand Up @@ -265,6 +265,8 @@ public void delete(URI uri, AccountModel user) throws Exception {
}

sparql.delete(FacilityModel.class, uri);

organizationDAO.invalidateCache();
}

/**
Expand All @@ -284,8 +286,6 @@ public FacilityModel update(FacilityModel instance, Geometry geometry, AccountMo
List<OrganizationModel> organizationModels = sparql.getListByURIs(OrganizationModel.class, instance.getOrganizationUris(), user.getLanguage());
instance.setOrganizations(organizationModels);

URI graphUri = sparql.getDefaultGraphURI(OrganizationModel.class);

FacilityModel existingModel = sparql.getByURI(FacilityModel.class, instance.getUri(), user.getLanguage());

if (Objects.nonNull(existingModel.getAddress()) || Objects.nonNull(geometry)) {
Expand All @@ -299,6 +299,8 @@ public FacilityModel update(FacilityModel instance, Geometry geometry, AccountMo
createFacilityGeospatialModel(instance, geometry);

sparql.update(instance);

organizationDAO.invalidateCache();
return instance;
}

Expand All @@ -319,7 +321,7 @@ private void deleteFacilityGeospatialModel(URI facilityUri) {
}
}

private void createFacilityGeospatialModel(FacilityModel facility, Geometry geometry) throws JsonProcessingException {
private void createFacilityGeospatialModel(FacilityModel facility, Geometry geometry) {
if (Objects.isNull(geometry) && Objects.isNull(facility.getAddress())) {
deleteFacilityGeospatialModel(facility.getUri());
return;
Expand Down Expand Up @@ -393,31 +395,29 @@ private void validateFacilityAccess(URI facilityURI, AccountModel accountModel)
* Checks if a facility address is valid (it must be the same as its sites).
*
* @param facilityModel The facility
* @param user The user
* @throws SiteFacilityInvalidAddressException If the address is invalid
*/
protected void validateFacilityAddress(FacilityModel facilityModel, AccountModel user) throws Exception {
if (facilityModel.getUri() == null) {
protected void validateFacilityAddress(FacilityModel facilityModel, AccountModel user) {

if (facilityModel.getAddress() == null) {
return;
}

if (facilityModel.getAddress() == null) {
if (facilityModel.getSites() == null) {
return;
}

List<SiteModel> siteModelList = this.siteDAO.getByFacility(facilityModel.getUri(), user);
List<SiteModel> siteModelList = facilityModel.getSites().stream().map(site -> {
try {
return this.siteDAO.get(site.getUri(), user);
} catch (Exception e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList());

for (SiteModel siteModel : siteModelList) {
if (siteModel.getAddress() != null) {
FacilityAddressDTO facilityAddress = new FacilityAddressDTO();
facilityAddress.fromModel(facilityModel.getAddress());

SiteAddressDTO siteAddress = new SiteAddressDTO();
siteAddress.fromModel(siteModel.getAddress());

if (!Objects.equals(facilityAddress, siteAddress)) {
throw new SiteFacilityInvalidAddressException(siteModel.getName(), facilityModel.getName());
}
SiteDAO.assertEqualsFacilityAndSiteAddresses(siteModel, facilityModel);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
/*
* *****************************************************************************
* SiteDAO.java
* OpenSILEX - Licence AGPL V3.0 - https://www.gnu.org/licenses/agpl-3.0.en.html
* Copyright © INRAE 2024.
* Last Modification: 25/06/2024 10:10
* Contact: gabriel.besombes@inrae.fr
* *****************************************************************************
*/

package org.opensilex.core.organisation.dal.site;

import com.mongodb.client.model.geojson.Geometry;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jena.arq.querybuilder.AskBuilder;
import org.apache.jena.arq.querybuilder.SelectBuilder;
import org.apache.jena.arq.querybuilder.clauses.WhereClause;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.vocabulary.ORG;
Expand All @@ -24,9 +33,9 @@
import org.opensilex.nosql.mongodb.MongoDBService;
import org.opensilex.security.account.dal.AccountModel;
import org.opensilex.security.authentication.ForbiddenURIAccessException;
import org.opensilex.server.exceptions.NotFoundURIException;
import org.opensilex.server.exceptions.BadRequestException;
import org.opensilex.server.exceptions.NotFoundException;
import org.opensilex.server.exceptions.NotFoundURIException;
import org.opensilex.sparql.deserializer.SPARQLDeserializers;
import org.opensilex.sparql.mapping.SPARQLListFetcher;
import org.opensilex.sparql.model.SPARQLResourceModel;
Expand All @@ -40,7 +49,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import static org.opensilex.sparql.service.SPARQLQueryHelper.makeVar;
Expand Down Expand Up @@ -96,7 +104,6 @@ public ListWithPagination<SiteModel> search(SiteSearchFilter filter)
.stream().map(SPARQLResourceModel::getUri)
.collect(Collectors.toList());

AtomicReference<SelectBuilder> initialSelect = new AtomicReference<>();
ListWithPagination<SiteModel> models = sparql.searchWithPagination(SiteModel.class, filter.getUser().getLanguage(), select -> {
Var uriVar = makeVar(SiteModel.URI_FIELD);
Var organizationsVar = makeVar("__" + SiteModel.ORGANIZATION_FIELD);
Expand All @@ -119,8 +126,6 @@ public ListWithPagination<SiteModel> search(SiteSearchFilter filter)
if (Objects.nonNull(filter.getFacility())) {
select.addWhere(SPARQLDeserializers.nodeURI(filter.getFacility()), Oeso.withinSite, uriVar);
}

initialSelect.set(select);
}, filter.getOrderByList(), filter.getPage(), filter.getPageSize());

SPARQLListFetcher<SiteModel> listFetcher = new SPARQLListFetcher<>(
Expand Down Expand Up @@ -229,6 +234,8 @@ public SiteModel create(SiteModel siteModel, AccountModel user) throws Exception

createSiteGeospatialModel(siteModel);


organizationDAO.invalidateCache();
return siteModel;
}

Expand Down Expand Up @@ -263,6 +270,8 @@ public SiteModel update(SiteModel siteModel, AccountModel accountModel) throws E

sparql.deleteByURI(sparql.getDefaultGraph(SiteModel.class), existingModel.getUri());
sparql.create(siteModel);

organizationDAO.invalidateCache();
return siteModel;
}

Expand All @@ -282,6 +291,8 @@ public void delete(URI uri, AccountModel account) throws Exception {
deleteSiteGeospatialModel(siteModel);

sparql.delete(SiteModel.class, uri);

organizationDAO.invalidateCache();
}

protected void deleteSiteGeospatialModel(SiteModel siteModel) throws Exception {
Expand Down Expand Up @@ -334,15 +345,21 @@ protected void validateSiteFacilityAddress(SiteModel siteModel, AccountModel cur

for (FacilityModel facilityModel : facilityModelList) {
if (facilityModel.getAddress() != null) {
FacilityAddressDTO facilityAddress = new FacilityAddressDTO();
facilityAddress.fromModel(facilityModel.getAddress());
assertEqualsFacilityAndSiteAddresses(siteModel, facilityModel);
}
}
}

SiteAddressDTO siteAddress = new SiteAddressDTO();
siteAddress.fromModel(siteModel.getAddress());
public static void assertEqualsFacilityAndSiteAddresses(SiteModel siteModel, FacilityModel facilityModel) {
if (facilityModel.getAddress() != null) {
FacilityAddressDTO facilityAddress = new FacilityAddressDTO();
facilityAddress.fromModel(facilityModel.getAddress());

if (!Objects.equals(facilityAddress, siteAddress)) {
throw new SiteFacilityInvalidAddressException(siteModel.getName(), facilityModel.getName());
}
SiteAddressDTO siteAddress = new SiteAddressDTO();
siteAddress.fromModel(siteModel.getAddress());

if (!Objects.equals(facilityAddress, siteAddress)) {
throw new SiteFacilityInvalidAddressException(siteModel.getName(), facilityModel.getName());
}
}
}
Expand Down
Loading

0 comments on commit 14a7b7b

Please sign in to comment.