Search in sources :

Example 1 with ImportCollectionContent

use of io.lumeer.engine.api.event.ImportCollectionContent in project engine by Lumeer.

the class DocumentFacade method updateDocumentsMetaData.

public List<Document> updateDocumentsMetaData(final String collectionId, final List<Document> documents) {
    final List<Document> updatedDocuments = new ArrayList<>();
    Collection collection = null;
    for (Document document : documents) {
        var tuple = checkEditDocument(document.getId());
        collection = tuple.getFirst();
        document.setUpdatedBy(getCurrentUserId());
        document.setUpdateDate(ZonedDateTime.now());
        final Document updatedDocument = documentDao.updateDocument(document.getId(), document);
        updatedDocument.setData(constraintManager.decodeDataTypes(collection, document.getData()));
        updatedDocuments.add(updatedDocument);
    }
    if (importCollectionContentEvent != null && collection != null) {
        importCollectionContentEvent.fire(new ImportCollectionContent(collection));
    }
    return updatedDocuments;
}
Also used : ArrayList(java.util.ArrayList) ImportCollectionContent(io.lumeer.engine.api.event.ImportCollectionContent) Collection(io.lumeer.api.model.Collection) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) CreateDocument(io.lumeer.engine.api.event.CreateDocument) UpdateDocument(io.lumeer.engine.api.event.UpdateDocument)

Example 2 with ImportCollectionContent

use of io.lumeer.engine.api.event.ImportCollectionContent in project engine by Lumeer.

the class DocumentFacade method createDocuments.

public List<Document> createDocuments(final String collectionId, final List<Document> documents, final boolean sendNotification) {
    final Collection collection = checkCreateDocuments(collectionId);
    final Map<String, Integer> usages = new HashMap<>();
    final Map<String, DataDocument> documentsData = new HashMap<>();
    permissionsChecker.checkDocumentLimits(documents);
    // encode the original data and remember them by their original template id
    documents.forEach(document -> {
        DataDocument data = constraintManager.encodeDataTypes(collection, document.getData());
        documentsData.put((String) document.createIfAbsentMetaData().computeIfAbsent(Document.META_TEMPLATE_ID, key -> UUID.randomUUID().toString()), data);
    });
    final List<Document> storedDocuments = createDocuments(collection, documents);
    // map the original data to the newly created documents
    storedDocuments.forEach(storedDocument -> {
        final DataDocument data = documentsData.get(storedDocument.getMetaData().getString(Document.META_TEMPLATE_ID));
        data.setId(storedDocument.getId());
        storedDocument.setData(data);
    });
    // store the documents data
    final List<DataDocument> storedData = dataDao.createData(collection.getId(), storedDocuments.stream().map(Document::getData).collect(Collectors.toList()));
    // map the stored data to the document ids
    final Map<String, DataDocument> storedDocumentsData = new HashMap<>();
    storedData.forEach(dd -> storedDocumentsData.put(dd.getId(), dd));
    // put the stored data to the stored documents, decode data types and count attributes usage
    storedDocuments.forEach(storedDocument -> {
        final DataDocument singleStoredData = storedDocumentsData.get(storedDocument.getId());
        storedDocument.setData(singleStoredData);
        singleStoredData.keySet().forEach(key -> usages.put(key, usages.computeIfAbsent(key, k -> 0) + 1));
        storedDocument.setData(constraintManager.decodeDataTypes(collection, storedDocument.getData()));
    });
    collectionAdapter.updateCollectionMetadata(collection, usages);
    if (sendNotification && importCollectionContentEvent != null) {
        importCollectionContentEvent.fire(new ImportCollectionContent(collection));
    }
    return storedDocuments;
}
Also used : Arrays(java.util.Arrays) ZonedDateTime(java.time.ZonedDateTime) DocumentsChain(io.lumeer.api.model.DocumentsChain) LinkTypeDao(io.lumeer.storage.api.dao.LinkTypeDao) HashMap(java.util.HashMap) DocumentAdapter(io.lumeer.core.adapter.DocumentAdapter) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CollectionDao(io.lumeer.storage.api.dao.CollectionDao) DataDao(io.lumeer.storage.api.dao.DataDao) ResourceType(io.lumeer.api.model.ResourceType) Resource(io.lumeer.api.model.common.Resource) Map(java.util.Map) ResourceCommentDao(io.lumeer.storage.api.dao.ResourceCommentDao) CollectionAdapter(io.lumeer.core.adapter.CollectionAdapter) ImportCollectionContent(io.lumeer.engine.api.event.ImportCollectionContent) ConstraintManager(io.lumeer.core.constraint.ConstraintManager) Tuple(io.lumeer.core.util.Tuple) Event(javax.enterprise.event.Event) DocumentDao(io.lumeer.storage.api.dao.DocumentDao) DataDocument(io.lumeer.engine.api.data.DataDocument) ResourceNotFoundException(io.lumeer.storage.api.exception.ResourceNotFoundException) Document(io.lumeer.api.model.Document) CreateDocument(io.lumeer.engine.api.event.CreateDocument) Set(java.util.Set) UUID(java.util.UUID) Constraint(io.lumeer.api.model.Constraint) Collectors(java.util.stream.Collectors) LinkInstance(io.lumeer.api.model.LinkInstance) LinkType(io.lumeer.api.model.LinkType) DocumentUtils(io.lumeer.core.util.DocumentUtils) Objects(java.util.Objects) FileAttachment(io.lumeer.api.model.FileAttachment) Project(io.lumeer.api.model.Project) LinkInstanceDao(io.lumeer.storage.api.dao.LinkInstanceDao) DefaultConfigurationProducer(io.lumeer.core.facade.configuration.DefaultConfigurationProducer) List(java.util.List) ResourceUtils(io.lumeer.api.util.ResourceUtils) RequestScoped(javax.enterprise.context.RequestScoped) PostConstruct(javax.annotation.PostConstruct) FavoriteItemDao(io.lumeer.storage.api.dao.FavoriteItemDao) UpdateDocument(io.lumeer.engine.api.event.UpdateDocument) Utils(io.lumeer.core.util.Utils) Collections(java.util.Collections) Collection(io.lumeer.api.model.Collection) CreateDocumentsAndLinks(io.lumeer.engine.api.event.CreateDocumentsAndLinks) DataDocument(io.lumeer.engine.api.data.DataDocument) HashMap(java.util.HashMap) ImportCollectionContent(io.lumeer.engine.api.event.ImportCollectionContent) Collection(io.lumeer.api.model.Collection) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) CreateDocument(io.lumeer.engine.api.event.CreateDocument) UpdateDocument(io.lumeer.engine.api.event.UpdateDocument)

Aggregations

Collection (io.lumeer.api.model.Collection)2 Document (io.lumeer.api.model.Document)2 DataDocument (io.lumeer.engine.api.data.DataDocument)2 CreateDocument (io.lumeer.engine.api.event.CreateDocument)2 ImportCollectionContent (io.lumeer.engine.api.event.ImportCollectionContent)2 UpdateDocument (io.lumeer.engine.api.event.UpdateDocument)2 ArrayList (java.util.ArrayList)2 Constraint (io.lumeer.api.model.Constraint)1 DocumentsChain (io.lumeer.api.model.DocumentsChain)1 FileAttachment (io.lumeer.api.model.FileAttachment)1 LinkInstance (io.lumeer.api.model.LinkInstance)1 LinkType (io.lumeer.api.model.LinkType)1 Project (io.lumeer.api.model.Project)1 ResourceType (io.lumeer.api.model.ResourceType)1 Resource (io.lumeer.api.model.common.Resource)1 ResourceUtils (io.lumeer.api.util.ResourceUtils)1 CollectionAdapter (io.lumeer.core.adapter.CollectionAdapter)1 DocumentAdapter (io.lumeer.core.adapter.DocumentAdapter)1 ConstraintManager (io.lumeer.core.constraint.ConstraintManager)1 DefaultConfigurationProducer (io.lumeer.core.facade.configuration.DefaultConfigurationProducer)1