Search in sources :

Example 26 with Document

use of org.jbpm.document.Document in project jbpm by kiegroup.

the class DocumentMarshallingStrategy method unmarshal.

@SuppressWarnings({ "resource", "unused" })
@Override
public Object unmarshal(Context context, ObjectInputStream objectInputStream, byte[] object, ClassLoader classLoader) throws IOException, ClassNotFoundException {
    DroolsObjectInputStream is = new DroolsObjectInputStream(new ByteArrayInputStream(object), classLoader);
    // first we read out the object id and class name we stored during marshaling
    String objectId = is.readUTF();
    String canonicalName = is.readUTF();
    String link = is.readUTF();
    Document storedDoc = null;
    try {
        storedDoc = documentStorageService.getDocument(objectId);
        storedDoc.setLink(link);
        // when loaded, mark it as not updated to avoid not needed marshalling
        storedDoc.addAttribute(Document.UPDATED_ATTRIBUTE, "false");
    } catch (Exception e) {
        throw new RuntimeException("Cannot read document from storage service", e);
    }
    return storedDoc;
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Document(org.jbpm.document.Document) IOException(java.io.IOException)

Example 27 with Document

use of org.jbpm.document.Document in project jbpm by kiegroup.

the class DocumentMarshallingStrategy method marshal.

@Override
public byte[] marshal(Context context, ObjectOutputStream objectOutputStream, Object o) throws IOException {
    Document document = (Document) o;
    String updatedAttribute = document.getAttribute(Document.UPDATED_ATTRIBUTE);
    if (Boolean.parseBoolean(updatedAttribute)) {
        // store via service only when it was actually updated
        documentStorageService.saveDocument(document, document.getContent());
        document.addAttribute(Document.UPDATED_ATTRIBUTE, "false");
    }
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(buff);
    oos.writeUTF(document.getIdentifier());
    oos.writeUTF(document.getClass().getCanonicalName());
    oos.writeUTF(document.getLink());
    oos.close();
    return buff.toByteArray();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.jbpm.document.Document) ObjectOutputStream(java.io.ObjectOutputStream)

Example 28 with Document

use of org.jbpm.document.Document in project jbpm by kiegroup.

the class DocumentsMarshallingStrategy method marshal.

public byte[] marshal(Context ctx, ObjectOutputStream objectOutputStream, Object o) throws IOException {
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    try (ObjectOutputStream oos = new ObjectOutputStream(buff)) {
        Documents documents = (Documents) o;
        // Write the number of documents in the list.
        oos.writeInt(documents.getDocuments().size());
        for (Document nextDocument : documents.getDocuments()) {
            // Use the DocumentMarshallingStrategy to marshal individual documents.
            byte[] nextMarshalledDocument = docMarshallingStrategy.marshal(ctx, objectOutputStream, nextDocument);
            oos.writeInt(nextMarshalledDocument.length);
            oos.write(nextMarshalledDocument);
            // Need to call reset on the stream in order for the Document bytes to be written correctly.
            oos.reset();
        }
    }
    return buff.toByteArray();
}
Also used : Documents(org.jbpm.document.Documents) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Document(org.jbpm.document.Document)

Example 29 with Document

use of org.jbpm.document.Document in project jbpm by kiegroup.

the class DocumentsMarshallingStrategy method unmarshal.

public Object unmarshal(Context ctx, ObjectInputStream objectInputStream, byte[] object, ClassLoader classLoader) throws IOException, ClassNotFoundException {
    Documents documents = new Documents();
    try (DroolsObjectInputStream is = new DroolsObjectInputStream(new ByteArrayInputStream(object), classLoader)) {
        // first we read the size of the list we've stored.
        int size = is.readInt();
        for (int i = 0; i < size; i++) {
            // Use the DocumentMarshallingStrategy to unmarshal the individual documents.
            int length = is.readInt();
            byte[] marshalledDocument = new byte[length];
            is.readFully(marshalledDocument);
            Document nextDocument = (Document) docMarshallingStrategy.unmarshal(ctx, objectInputStream, marshalledDocument, classLoader);
            documents.addDocument(nextDocument);
        }
    }
    return documents;
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Documents(org.jbpm.document.Documents) Document(org.jbpm.document.Document)

Example 30 with Document

use of org.jbpm.document.Document in project jbpm by kiegroup.

the class DocumentStorageServiceImpl method listDocuments.

@Override
public List<Document> listDocuments(Integer page, Integer pageSize) {
    List<Document> listOfDocs = new ArrayList<Document>();
    int startIndex = page * pageSize;
    int endIndex = startIndex + pageSize;
    File[] documents = storageFile.listFiles();
    if (documents == null) {
        // happens when the docs folder doesn't exist
        return Collections.emptyList();
    }
    // make sure the endIndex is not bigger then amount of files
    if (documents.length < endIndex) {
        endIndex = documents.length;
    }
    Arrays.sort(documents, Comparator.comparingLong(File::lastModified));
    for (int i = startIndex; i < endIndex; i++) {
        Document doc = getDocument(documents[i].getName());
        listOfDocs.add(doc);
    }
    return listOfDocs;
}
Also used : ArrayList(java.util.ArrayList) Document(org.jbpm.document.Document) File(java.io.File)

Aggregations

Document (org.jbpm.document.Document)38 Test (org.junit.Test)21 Date (java.util.Date)14 DocumentImpl (org.jbpm.document.service.impl.DocumentImpl)9 HashMap (java.util.HashMap)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DocumentData (org.kie.workbench.common.forms.dynamic.model.document.DocumentData)4 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Documents (org.jbpm.document.Documents)3 Drive (com.google.api.services.drive.Drive)2 VisualRecognition (com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 DroolsObjectInputStream (org.drools.core.common.DroolsObjectInputStream)2 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)2 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)2 TestWorkItemManager (org.jbpm.process.workitem.core.TestWorkItemManager)2