use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.
the class AtomService method createAtom.
public Atom createAtom(final WonMessage wonMessage) {
acquireOwnerApplicationLock();
wonMessage.getMessageType().requireType(WonMessageType.CREATE_ATOM);
Dataset atomContent = wonMessage.getMessageContent();
List<WonMessage.AttachmentHolder> attachmentHolders = wonMessage.getAttachments();
// remove attachment and its signature from the atomContent
removeAttachmentsFromAtomContent(atomContent, attachmentHolders);
URI messageURI = wonMessage.getMessageURI();
final AtomModelWrapper atomModelWrapper = new AtomModelWrapper(atomContent);
URI atomURI = getAtomURIAndCheck(atomModelWrapper);
// rename the content graphs and signature graphs so they start with the atom
// uri
Collection<String> sockets = getSocketsAndCheck(atomModelWrapper, atomURI);
Set<Socket> socketEntities = sockets.stream().map(socketUri -> {
Optional<String> socketType = getSocketTypeAndCheck(atomModelWrapper, socketUri);
Socket f = new Socket();
f.setAtomURI(atomURI);
f.setSocketURI(URI.create(socketUri));
f.setTypeURI(URI.create(socketType.get()));
return f;
}).collect(Collectors.toSet());
checkResourcesInAtomContent(atomModelWrapper);
Dataset atomDataset = atomModelWrapper.getDataset();
socketAclService.addLocalSocketAcls(atomURI, socketEntities, atomDataset);
// add everything to the atom model class and save it
checkCanThisMessageCreateOrModifyThisAtom(wonMessage, atomURI);
Atom atom = new Atom();
atom.setState(AtomState.ACTIVE);
atom.setAtomURI(atomURI);
// make a new wrapper because we just changed the underlying dataset
atom.setWonNodeURI(URI.create(uriService.getResourceURIPrefix()));
ConnectionContainer connectionContainer = new ConnectionContainer(atom);
atom.setConnectionContainer(connectionContainer);
AtomMessageContainer atomMessageContainer = atomMessageContainerRepository.findOneByParentUri(atomURI);
if (atomMessageContainer == null) {
atomMessageContainer = new AtomMessageContainer(atom, atom.getAtomURI());
} else {
throw new UriAlreadyInUseException("Found an AtomMessageContainer for the atom we're about to create (" + atomURI + ") - aborting");
}
atom.setMessageContainer(atomMessageContainer);
// store the atom content
atomModelWrapper.renameResourceWithPrefix(messageURI.toString(), atomURI.toString());
DatasetHolder datasetHolder = new DatasetHolder(atomURI, atomModelWrapper.getDataset());
// store attachments
List<DatasetHolder> attachments = new ArrayList<>(attachmentHolders.size());
for (WonMessage.AttachmentHolder attachmentHolder : attachmentHolders) {
datasetHolder = new DatasetHolder(attachmentHolder.getDestinationUri(), attachmentHolder.getAttachmentDataset());
attachments.add(datasetHolder);
}
atom.setDatatsetHolder(datasetHolder);
atom.setAttachmentDatasetHolders(attachments);
atom = atomRepository.save(atom);
connectionContainerRepository.save(connectionContainer);
socketEntities.forEach(socket -> socketRepository.save(socket));
dataDerivationService.deriveDataIfNecessary(atom);
return atom;
}
use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.
the class WonMessageRoutesTest method loadDatasetAndReplaceAtomURI.
protected Dataset loadDatasetAndReplaceAtomURI(String file, URI atomURI) throws IOException {
Dataset atomDataset = loadDataset(file);
AtomModelWrapper amw = new AtomModelWrapper(atomDataset);
amw.setAtomURI(atomURI);
return atomDataset;
}
Aggregations