/** * This method collects all templates of the given <code>templates</code> which are abstract and must be * substituted. Additionally, all children of the template's type are included as a list of trees. * * @param templates the list of TOSCA templates which have to examied whether they must be substituted * @param types the map of all types of the same kind (e.g. Node Templates and Node Types) identified by their * corresponding <code>DefinitionsChildId</code> * @param <R> the class of the templates * @param <T> the class of the types * @return a map containing a mapping between substitutable templates and their available sub types which can be * used during the substitution */ public static <R extends HasType, T extends HasInheritance> Map<R, List<Subtypes<T>>> collectSubstitutableTemplates(List<R> templates, Map<QName, T> types) { Map<R, List<Subtypes<T>>> substitutableTypes = new HashMap<>(); templates.forEach(tNodeTemplate -> { QName nodeTemplateType = tNodeTemplate.getTypeAsQName(); collectTypeHierarchy(types, nodeTemplateType) .ifPresent(tNodeTypeSubtypes -> substitutableTypes.put(tNodeTemplate, tNodeTypeSubtypes) ); }); return substitutableTypes; }
/** * Calls setType(QName) with QName.valueOf(typeStr) * <p> * Directly persists the resource * * @param typeStr a textual representation of a QName */ default void setType(String typeStr) { this.setType(QName.valueOf(typeStr)); } }
/** * TODO: This method should be moved to the probably can be moved up the type hierarchy and somehow be meregd with * the functionality to get referenced templates. Currently, it is only required here by the topology modeler. * * @return the type of the artifact template OR the number of references pointing to this resource */ @GET @Produces(MediaType.TEXT_PLAIN) public Response getReferenceCount( @QueryParam("referenceCount") @ApiParam("Determines the number of elements known by the repository which point to this resource.") String referenceCount, @QueryParam("type") @ApiParam("Type of the artifact template") String type) { if (referenceCount != null) { String res = Integer.toString(RepositoryFactory.getRepository().getReferenceCount((ArtifactTemplateId) this.id)); return Response.ok().entity(res).build(); } else if (type != null) { String res = ((HasType) this.getElement()).getTypeAsQName().toString(); return Response.ok().entity(res).build(); } else { // we enforce the query parameter to be extensible to other queries return Response.status(Status.BAD_REQUEST).entity("You have to pass the query parameter referenceCount or type").build(); } }
/** * @return Response */ protected Response putInheritance(InheritanceResourceApiData json) { HasType derivedFrom = null; // If (none) is selected, derivedFrom needs to be null in order to have valid XML in ALL cases! if (!json.derivedFrom.endsWith("(none)")) { QName qname = QName.valueOf(json.derivedFrom); if (this instanceof EntityTypeResource) { derivedFrom = new TEntityType.DerivedFrom(); } else if (this instanceof RelationshipTypeImplementationResource) { derivedFrom = new TRelationshipTypeImplementation.DerivedFrom(); } else if (this instanceof NodeTypeImplementationResource) { derivedFrom = new TNodeTypeImplementation.DerivedFrom(); } else { return Response.status(Response.Status.BAD_REQUEST).entity("Type does not support inheritance!").build(); } derivedFrom.setType(qname); } HasInheritance element = (HasInheritance) this.getElement(); element.setDerivedFrom(derivedFrom); element.setAbstract(TBoolean.fromValue(json.isAbstract)); element.setFinal(TBoolean.fromValue(json.isFinal)); return RestUtils.persist(this); } }
QName derivedFromType = null; if (derivedFrom != null) { derivedFromType = derivedFrom.getTypeAsQName();
final Definitions definitions = repository.getDefinitions(id); final TExtensibleElements element = definitions.getElement(); ((HasType) element).setType(jsonData.type);
/** * This method collects all elements of the given class <code>T</code> which are derived from the <b>abstract</b> * <code>parent</code>. * * @param <T> a TOSCA definitions type which has inheritance * @param types all available types of the specified class <code>T</code> * @param parent abstract the parent * @return an <code>Optional</code> containing a tree of subtypes */ public static <T extends HasInheritance> Optional<List<Subtypes<T>>> collectTypeHierarchy(Map<QName, T> types, QName parent) { T type = types.get(parent); if (Objects.nonNull(type) && type.getAbstract().equals(TBoolean.YES)) { List<Subtypes<T>> subtypes = new ArrayList<>(); types.forEach((key, current) -> { if (Objects.nonNull(current.getDerivedFrom()) && current.getDerivedFrom().getTypeAsQName().equals(parent)) { Subtypes<T> child = new Subtypes<>(current); subtypes.add(child); collectTypeHierarchy(types, key) .ifPresent(child::addChildren); } }); return Optional.of(subtypes); } return Optional.empty(); }
public String getDerivedFrom() { if (((HasInheritance) this.getElement()).getDerivedFrom() != null) { return ((HasInheritance) this.getElement()).getDerivedFrom().getTypeAsQName().toString(); } else { return "(none)"; } }
/** * This is a quick helper method. The code should be refactored to use HasType on the element directly instead of * going through the resource. The method was implemented, because it is not that easy to get the id of the element * belonging to a resource. * * @param res a resource, where the associated element has a type. Example: EntityTypeImplementationResource * @return the QName of the associated type */ public static QName getType(IPersistable res) { DefinitionsChildId id = (DefinitionsChildId) res.getRepositoryFileReference().getParent(); final HasType element = (HasType) RepositoryFactory.getRepository().getDefinitions(id).getElement(); return element.getTypeAsQName(); }
referencesGivenQName = qNameOfTheType.equals(((HasType) element).getTypeAsQName());