gcube-cms-suite/geoportal-common/src/main/java/org/gcube/application/geoportal/common/model/useCaseDescriptor/UseCaseDescriptor.java

134 lines
3.9 KiB
Java
Raw Normal View History

2022-03-04 14:23:20 +01:00
package org.gcube.application.geoportal.common.model.useCaseDescriptor;
2021-09-20 16:47:35 +02:00
2022-03-07 17:59:06 +01:00
import com.fasterxml.jackson.annotation.JsonIgnore;
2022-02-23 17:13:22 +01:00
import com.fasterxml.jackson.annotation.JsonProperty;
2022-01-17 18:19:40 +01:00
import com.vdurmont.semver4j.Semver;
2021-12-07 12:16:29 +01:00
import lombok.Data;
2021-09-20 16:47:35 +02:00
import lombok.NoArgsConstructor;
2022-02-24 18:09:30 +01:00
import lombok.extern.slf4j.Slf4j;
2022-03-04 18:28:45 +01:00
import org.bson.types.ObjectId;
2022-02-14 12:23:13 +01:00
import org.gcube.application.geoportal.common.model.document.accounting.AccountingInfo;
2022-03-24 17:44:00 +01:00
import org.gcube.application.geoportal.common.model.document.accounting.User;
2021-09-20 16:47:35 +02:00
2022-09-26 16:17:37 +02:00
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
2021-12-07 12:16:29 +01:00
@Data
2021-09-20 16:47:35 +02:00
@NoArgsConstructor
@XmlRootElement
2022-02-24 18:09:30 +01:00
@Slf4j
2022-09-12 18:14:43 +02:00
public class UseCaseDescriptor implements Serializable {
2021-09-20 16:47:35 +02:00
2022-03-04 18:28:45 +01:00
public static final String MONGO_ID="_mongoId";
2022-02-23 17:13:22 +01:00
public static final String ID="_id";
public static final String VERSION="_version";
public static final String NAME="_name";
public static final String DESCRIPTION="_description";
public static final String CREATION_INFO="_creationInfo";
public static final String SCHEMA="_schema";
public static final String HANDLERS="_handlers";
public static final String DATA_ACCESS_POLICIES="_dataAccessPolicies";
2022-03-04 18:28:45 +01:00
@JsonProperty(MONGO_ID)
private ObjectId mongoId;
2022-02-23 17:13:22 +01:00
@JsonProperty(ID)
2021-09-20 16:47:35 +02:00
private String id;
2022-02-23 17:13:22 +01:00
@JsonProperty(VERSION)
2022-01-17 18:19:40 +01:00
private Semver version;
2021-12-07 13:05:08 +01:00
2022-02-23 17:13:22 +01:00
@JsonProperty(NAME)
2021-12-07 13:05:08 +01:00
private String name;
2022-02-23 17:13:22 +01:00
@JsonProperty(DESCRIPTION)
2021-12-07 13:05:08 +01:00
private String description;
2022-02-23 17:13:22 +01:00
@JsonProperty(CREATION_INFO)
2021-12-07 13:05:08 +01:00
private AccountingInfo creationInfo;
2022-02-23 17:13:22 +01:00
@JsonProperty(SCHEMA)
2022-01-17 18:19:40 +01:00
private Field schema;
2021-12-07 13:05:08 +01:00
2022-02-23 17:13:22 +01:00
@JsonProperty(HANDLERS)
2021-12-07 13:05:08 +01:00
private List<HandlerDeclaration> handlers;
2022-01-12 18:42:22 +01:00
2022-01-27 15:02:53 +01:00
2022-02-23 17:13:22 +01:00
@JsonProperty(DATA_ACCESS_POLICIES)
2022-01-27 15:02:53 +01:00
private List<DataAccessPolicy> dataAccessPolicies;
2022-01-12 18:42:22 +01:00
/**
* Returns map Type -> Handler Declaration
* @return
*/
2022-03-07 17:59:06 +01:00
@JsonIgnore
2022-02-24 18:09:30 +01:00
public Map<String,List<HandlerDeclaration>> getHandlersMapByType(){
2022-01-12 18:42:22 +01:00
HashMap<String,List<HandlerDeclaration>> toReturn=new HashMap<>();
handlers.forEach(h->{
2022-02-24 18:09:30 +01:00
try {
2022-04-29 17:00:12 +02:00
if(!toReturn.containsKey(h.getType()))
toReturn.put(h.getType(),new ArrayList<>());
toReturn.get(h.getType()).add(h);
}catch (Throwable t){
log.error("Invalid useCaseDescriptor : unable to get Handler Map by Type with {} in useCaseDescriptor [ID {}]",h,this.getId(),t);}
2022-01-12 18:42:22 +01:00
});
2022-05-02 17:55:48 +02:00
if(log.isTraceEnabled()) {
toReturn.forEach((s, handlerDeclarations) -> log.trace("UCD {} : Found N {} handlers of type {}",this.getId(),handlerDeclarations.size(),s));
}
2022-01-12 18:42:22 +01:00
return toReturn;
}
2022-01-17 18:19:40 +01:00
2022-02-24 18:09:30 +01:00
/**
2022-05-02 17:55:48 +02:00
* Returns List of Handler Declaration by type
*
* @param type
* @return
*/
@JsonIgnore
public List<HandlerDeclaration> getHandlersByType(String type){
List<HandlerDeclaration> toReturn=
handlers.stream().sequential().
filter(handlerDeclaration -> handlerDeclaration.getType().equals(type)).collect(Collectors.toList());
log.debug("UCD {} : Found {} Handlers for {}",getId(),toReturn.size(),type);
return toReturn;
}
/**
* Returns map ID -> Handler Declaration
2022-02-24 18:09:30 +01:00
* @return
*/
2022-03-07 17:59:06 +01:00
@JsonIgnore
2022-02-24 18:09:30 +01:00
public Map<String,List<HandlerDeclaration>> getHandlersMapByID(){
HashMap<String,List<HandlerDeclaration>> toReturn=new HashMap<>();
handlers.forEach(h->{
try {
if (!toReturn.containsKey(h.getId()))
toReturn.put(h.getId(), new ArrayList<>());
toReturn.get(h.getId()).add(h);
}catch (Throwable t){
2022-03-04 14:23:20 +01:00
log.error("Invalid useCaseDescriptor : unable to get Handler Map by ID with {} in useCaseDescriptor [ID {}]",h,this.getId(),t);}
2022-02-24 18:09:30 +01:00
});
return toReturn;
}
2022-01-17 18:19:40 +01:00
2022-03-24 17:44:00 +01:00
@JsonIgnore
public DataAccessPolicy getMatching(User u){
DataAccessPolicy defaultPolicy = null;
2022-03-30 18:39:10 +02:00
if(dataAccessPolicies!=null)
for (DataAccessPolicy dataAccessPolicy : dataAccessPolicies) {
if(dataAccessPolicy.getRoles()==null||dataAccessPolicy.getRoles().isEmpty())
defaultPolicy= dataAccessPolicy;
for (String r : dataAccessPolicy.getRoles())
if (u.getRoles().contains(r))
return dataAccessPolicy;
}
2022-03-24 17:44:00 +01:00
return defaultPolicy;
}
2022-03-04 18:28:45 +01:00
2021-09-20 16:47:35 +02:00
}