package gr.cite.annotation.model.builder; import gr.cite.annotation.authorization.AffiliatedResource; import gr.cite.annotation.convention.ConventionService; import gr.cite.commons.web.authz.service.AuthorizationService; import gr.cite.tools.data.builder.Builder; import gr.cite.tools.data.query.QueryBase; import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.fieldset.FieldSet; import gr.cite.tools.logging.LoggerService; import java.time.Instant; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; public abstract class BaseBuilder implements Builder { protected final LoggerService logger; protected final ConventionService conventionService; public BaseBuilder( ConventionService conventionService, LoggerService logger ) { this.conventionService = conventionService; this.logger = logger; } public M build(FieldSet directives, D data) throws MyApplicationException { if (data == null) { //this.logger.Debug(new MapLogEntry("requested build for null item requesting fields").And("fields", directives)); // return default(M); M model = null; return null; //TODO } List models = this.build(directives, Arrays.asList(data)); return models.stream().findFirst().orElse(null); //TODO } public abstract List build(FieldSet directives, List data) throws MyApplicationException; public Map asForeignKey(QueryBase query, FieldSet directives, Function keySelector) throws MyApplicationException { this.logger.trace("Building references from query"); List datas = query.collectAs(directives); this.logger.debug("collected {} items to build", Optional.ofNullable(datas).map(List::size).orElse(0)); return this.asForeignKey(datas, directives, keySelector); } public Map asForeignKey(List data, FieldSet directives, Function keySelector) throws MyApplicationException { this.logger.trace("building references"); List models = this.build(directives, data); this.logger.debug("mapping {} build items from {} requested", Optional.ofNullable(models).map(List::size).orElse(0), Optional.ofNullable(data).map(List::size).orElse(0)); Map map = models.stream().collect(Collectors.toMap(keySelector, o -> o)); return map; } public Map> asMasterKey(QueryBase query, FieldSet directives, Function keySelector) throws MyApplicationException { this.logger.trace("Building details from query"); List datas = query.collectAs(directives); this.logger.debug("collected {} items to build", Optional.ofNullable(datas).map(List::size).orElse(0)); return this.asMasterKey(datas, directives, keySelector); } public Map> asMasterKey(List data, FieldSet directives, Function keySelector) throws MyApplicationException { this.logger.trace("building details"); List models = this.build(directives, data); this.logger.debug("mapping {} build items from {} requested", Optional.ofNullable(models).map(List::size).orElse(0), Optional.ofNullable(data).map(List::size).orElse(0)); Map> map = new HashMap<>(); for (M model : models) { K key = keySelector.apply(model); if (!map.containsKey(key)) map.put(key, new ArrayList()); map.get(key).add(model); } return map; } public Map asEmpty(List keys, Function mapper, Function keySelector) { this.logger.trace("building static references"); List models = keys.stream().map(mapper).collect(Collectors.toList()); this.logger.debug("mapping {} build items from {} requested", Optional.of(models).map(List::size).orElse(0), Optional.of(keys).map(List::size)); Map map = models.stream().collect(Collectors.toMap(keySelector, o -> o)); return map; } protected String hashValue(Instant value) throws MyApplicationException { return this.conventionService.hashValue(value); } protected String asPrefix(String name) { return this.conventionService.asPrefix(name); } protected String asIndexer(String... names) { return this.conventionService.asIndexer(names); } protected Set extractAuthorizationFlags(FieldSet fields, String propertyName, List permissionNames){ if (fields == null) return new HashSet<>(); if (permissionNames == null) return new HashSet<>(); FieldSet authorizationFlags = fields.extractPrefixed(this.asPrefix(propertyName)); List permissions = new ArrayList<>(); for (String fieldValue : authorizationFlags.getFields()) permissions.addAll(permissionNames.stream().filter(x-> x.equalsIgnoreCase(fieldValue)).toList()); return new HashSet<>(permissions); } protected List evaluateAuthorizationFlags(AuthorizationService authorizationService, Set authorizationFlags, AffiliatedResource affiliatedResource) { List allowed = new ArrayList<>(); if (authorizationFlags == null) return allowed; if (authorizationService == null) return allowed; for (String permission : authorizationFlags) { Boolean isAllowed = affiliatedResource == null ? authorizationService.authorize(permission) : authorizationService.authorizeAtLeastOne(List.of(affiliatedResource), permission); if (isAllowed) allowed.add(permission); } return allowed; } }