use of com.icthh.xm.ms.entity.domain.Link in project xm-ms-entity by xm-online.
the class EntityServiceImplIntTest method testGetLinkTargetsByEntityId.
@Test
@Transactional
public void testGetLinkTargetsByEntityId() throws Exception {
XmEntity sourceEntity = xmEntityRepository.save(createEntity(null, "ACCOUNT"));
XmEntity targetEntity = xmEntityRepository.save(createEntity(2l, TARGET_TYPE_KEY));
Link link = createLink(sourceEntity, targetEntity);
linkRepository.save(link);
List<Link> targetsLinks = xmEntityService.getLinkTargets(IdOrKey.of(sourceEntity.getId()), TARGET_TYPE_KEY);
assertThat(targetsLinks).isNotEmpty();
assertThat(targetsLinks.size()).isEqualTo(BigInteger.ONE.intValue());
}
use of com.icthh.xm.ms.entity.domain.Link in project xm-ms-entity by xm-online.
the class XmEntityUtils method getRequiredLinkedTarget.
public static XmEntity getRequiredLinkedTarget(XmEntity xmEntity, String targetTypeKey) {
Optional<Link> linkOpt = getLink(xmEntity, targetTypeKey);
XmEntity target = linkOpt.orElseThrow(() -> new BusinessException("Can't find linkOpt with target type key: " + targetTypeKey)).getTarget();
return Objects.requireNonNull(target, "Target in link with typeKey: " + targetTypeKey + " is null");
}
use of com.icthh.xm.ms.entity.domain.Link in project xm-ms-entity by xm-online.
the class XmEntityUtils method getRequiredTargetByLink.
public static XmEntity getRequiredTargetByLink(XmEntity xmEntity, String linkTypeKey) {
Optional<Link> firstLink = findFirstLink(xmEntity, linkTypeKey);
if (!firstLink.isPresent()) {
throw new IllegalArgumentException("Link with type key '" + linkTypeKey + "' are required");
}
Link link = firstLink.get();
return Objects.requireNonNull(link.getTarget(), "Link with type key '" + linkTypeKey + "' has null target");
}
use of com.icthh.xm.ms.entity.domain.Link in project xm-ms-entity by xm-online.
the class XmEntityServiceImpl method saveLinkTarget.
@LogicExtensionPoint("SaveLinkTarget")
@Override
public Link saveLinkTarget(IdOrKey idOrKey, Link link, MultipartFile file) {
// resolve source entity by idOrKey
XmEntity source = toSourceXmEntity(idOrKey);
// resolve target entity by key
XmEntity target = findOne(toIdOrKey(link.getTarget()));
// save link
link.setSource(source);
link.setTarget(target);
link.setStartDate(Instant.now());
Link savedLink = linkService.save(link);
log.debug("Link saved with id {}", link.getId());
// save file to storage and attachment
if (file != null) {
addFileAttachment(savedLink.getTarget(), file);
}
return savedLink;
}
use of com.icthh.xm.ms.entity.domain.Link in project xm-ms-entity by xm-online.
the class XmEntityServiceImpl method saveXmEntity.
/**
* Save a xmEntity.
* When you call this method will be run general save LEP
*
* @param xmEntity the entity to save
* @return the persisted entity
*/
@LogicExtensionPoint("Save")
public XmEntity saveXmEntity(XmEntity xmEntity) {
log.debug("Request to save XmEntity : {}", xmEntity);
Optional<XmEntity> oldEntity = startUpdateDateGenerationStrategy.preProcessStartUpdateDates(xmEntity, xmEntity.getId(), xmEntityRepository, XmEntity::setStartDate, XmEntity::getStartDate, XmEntity::setUpdateDate);
if (oldEntity.isPresent()) {
preventRenameTenant(xmEntity, oldEntity.get());
} else if (xmEntity.getCreatedBy() == null) {
xmEntity.setCreatedBy(authContextHolder.getContext().getUserKey().orElse(null));
}
// FIXME It is hack to link each tag with entity before persisting. may be there is more elegant solution.
xmEntity.updateXmEntityReference(xmEntity.getAttachments(), Attachment::setXmEntity);
xmEntity.updateXmEntityReference(xmEntity.getCalendars(), Calendar::setXmEntity);
xmEntity.updateXmEntityReference(xmEntity.getLocations(), Location::setXmEntity);
xmEntity.updateXmEntityReference(xmEntity.getRatings(), Rating::setXmEntity);
xmEntity.updateXmEntityReference(xmEntity.getTags(), Tag::setXmEntity);
xmEntity.updateXmEntityReference(xmEntity.getComments(), Comment::setXmEntity);
xmEntity.updateXmEntityReference(xmEntity.getTargets(), Link::setSource);
xmEntity.updateXmEntityReference(xmEntity.getSources(), Link::setTarget);
xmEntity.updateXmEntityReference(xmEntity.getVotes(), Vote::setXmEntity);
XmEntity result = xmEntityRepository.save(xmEntity);
xmEntitySearchRepository.save(result);
return result;
}
Aggregations