public static ChildEdgeResponseDTO toResponseDTO(BusinessServiceChildEdgeEntity input) { ChildEdgeResponseDTO edge = new ChildEdgeResponseDTO(); edge.setLocation(ResourceLocationFactory.createBusinessServiceEdgeLocation(input.getBusinessService().getId(), input.getId())); edge.setReductionKeys(edge.getReductionKeys()); edge.setMapFunction(transform(input.getMapFunction())); edge.setId(input.getId()); edge.setChildId(input.getChild().getId()); edge.setWeight(input.getWeight()); edge.setOperationalStatus(Status.INDETERMINATE); // we assume INDETERMINATE return edge; }
public BusinessServiceEntity addChildServiceEdge(BusinessServiceEntity child, AbstractMapFunctionEntity mapFunction, int weight) { BusinessServiceChildEdgeEntity edge = new BusinessServiceChildEdgeEntity(); edge.setBusinessService(this); edge.setChild(Objects.requireNonNull(child)); edge.setWeight(weight); edge.setMapFunction(Objects.requireNonNull(mapFunction)); addEdge(edge); return this; }
@Override public Set<String> getReductionKeys() { return Collections.unmodifiableSet(getEntity().getReductionKeys()); }
@Override public boolean equalsDefinition(BusinessServiceEdgeEntity other) { boolean equalsSuper = super.equalsDefinition(other); if (equalsSuper) { return Objects.equals(child.getId(), ((BusinessServiceChildEdgeEntity) other).getChild().getId()); } return false; }
@Override public void setChild(BusinessService child) { getEntity().setChild(((BusinessServiceImpl) child).getEntity()); }
@SuppressWarnings("unchecked") private <T extends Edge> T createEdge(Class<T> type, BusinessService source, MapFunction mapFunction, int weight) { T edge = null; if (type == IpServiceEdge.class) { edge = (T) new IpServiceEdgeImpl(this, new IPServiceEdgeEntity()); } if (type == ChildEdge.class) { edge = (T) new ChildEdgeImpl(this, new BusinessServiceChildEdgeEntity()); } if (type == ReductionKeyEdge.class) { edge = (T) new ReductionKeyEdgeImpl(this, new SingleReductionKeyEdgeEntity()); } if (type == ApplicationEdge.class) { edge = (T) new ApplicationEdgeImpl(this, new ApplicationEdgeEntity()); } if (edge != null) { edge.setSource(source); edge.setMapFunction(mapFunction); edge.setWeight(weight); return edge; } throw new IllegalArgumentException("Could not create edge for type " + type); }
@Override public boolean addChildEdge(BusinessService parentService, BusinessService childService, MapFunction mapFunction, int weight) { // verify that exists final BusinessServiceEntity parentEntity = getBusinessServiceEntity(parentService); final BusinessServiceEntity childEntity = getBusinessServiceEntity(childService); // Create the edge ChildEdge childEdge = createEdge(ChildEdge.class, parentService, mapFunction, weight); childEdge.setChild(childService); // Verify no loop if (this.checkDescendantForLoop(parentEntity, childEntity)) { throw new IllegalArgumentException("Service will form a loop"); } // if already exists, no update final BusinessServiceChildEdgeEntity edgeEntity = getBusinessServiceEdgeEntity(childEdge); long count = parentEntity.getChildEdges().stream().filter(e -> e.equalsDefinition(edgeEntity)).count(); if (count > 0) { return false; } parentEntity.addEdge(((ChildEdgeImpl)childEdge).getEntity()); return true; }
private boolean checkDescendantForLoop(final BusinessServiceEntity parent, final BusinessServiceEntity descendant) { if (parent.equals(descendant)) { return true; } for (BusinessServiceChildEdgeEntity eachChildEdge : descendant.getChildEdges()) { return this.checkDescendantForLoop(parent, eachChildEdge.getChild()); } return false; }
@Override public BusinessService getChild() { return new BusinessServiceImpl(getManager(), getEntity().getChild()); }
@Override public void deleteBusinessService(BusinessService businessService) { BusinessServiceEntity entity = getBusinessServiceEntity(businessService); // remove all parent -> child associations for(BusinessServiceEntity parent : getDao().findParents(entity)) { List<BusinessServiceChildEdgeEntity> collect = parent.getChildEdges().stream().filter(e -> entity.equals(e.getChild())).collect(Collectors.toList()); collect.forEach(x -> { parent.removeEdge(x); edgeDao.delete(x); // we need to delete this edge manually as they cannot be deleted automatically }); } // edges of the entity are deleted automatically by hibernate getDao().delete(entity); }