cancel
Showing results for 
Search instead for 
Did you mean: 

Upload document to alfresco with activiti task (form comp.)

ivan_hajdinjak
Champ in-the-making
Champ in-the-making
Greetings,

Is this feature integrated in latest activiti release, as I wrote in title ? What's the fastest way to do that ?

True regards,
Ivan Hajdinjak
6 REPLIES 6

jbarrez
Star Contributor
Star Contributor
No, nothing there yet.

Fastest way would be using CMIS and a custom upload component in the form.

ivan_hajdinjak
Champ in-the-making
Champ in-the-making
No, nothing there yet.

Fastest way would be using CMIS and a custom upload component in the form.

Thank you for the reply. Can you please give me some code example or guidelines to start using CMIS and form component ?

jbarrez
Star Contributor
Star Contributor
I'm afraid there is not much online, besides the userguide. For CMIS, just check out Apache Chemistry. The openCMIS API is really simple.

rantanohneplan
Champ in-the-making
Champ in-the-making
Hello everybody,

first of all I want to apologize for posting in a 3 year old thread. But I wonder if anybody has developed such an upload component in the meantime.

I want to use it for uploading documents which represent the results of user tasks. To be more specific, a user task orders the user to create a specific document. Afterwards, the user has to upload the document via the form of the user task to make it available to the server. On completion of the user task, the server is able to archive the document in a repository using openCMIS.

Can anybody help?

Best regards,
Sven

jbarrez
Star Contributor
Star Contributor
No, so far nothing open source out there I'm afraid. Would be a great contribution though 😉

rantanohneplan
Champ in-the-making
Champ in-the-making
Alright, here we go.

I implemented a component which is based on the user component of the form and uses the upload component of the deployment tab. It consists of three classes for the implementation of the form field and the form type (SelectFileField, FileFormType and FileFormPropertyRenderer) and a receiver to process the upload data (FileUploadReceiver). The receiver writes the data to a file in the filesystem and the filename (without path) is saved in a form variable.

Please feel free to adapt this component to your needs and use it in your own projects.

Greetings,
Sven

<java>
package org.activiti.explorer.form;

import org.activiti.engine.form.AbstractFormType;

// ————————————————————————————————-
/**
* Minimal implementation of a new custom form type.
*
* @author Sven Friedrichs, 2014
*/
// ————————————————————————————————-
public class FileFormType extends AbstractFormType
{

  public static final String TYPE_NAME = "file";

  // ———————————————————————————————–
  /*
   * (non-Javadoc)
   * @see org.activiti.engine.form.FormType#getName()
   */
  // ———————————————————————————————–
  public String getName()
  {
    return TYPE_NAME;
  }

  // ———————————————————————————————–
  /*
   * (non-Javadoc)
   * @see org.activiti.engine.form.AbstractFormType#convertFormValueToModelValue(java.lang.String)
   */
  // ———————————————————————————————–
  @Override
  public Object convertFormValueToModelValue(String propertyValue)
  {
    return propertyValue;
  }

  // ———————————————————————————————–
  /*
   * (non-Javadoc)
   * @see org.activiti.engine.form.AbstractFormType#convertModelValueToFormValue(java.lang.Object)
   */
  // ———————————————————————————————–
  @Override
  public String convertModelValueToFormValue(Object modelValue)
  {
    return (String) modelValue;
  }
}
</java>

<java>
package org.activiti.explorer.ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.explorer.ui.form.SelectFileField;

import com.vaadin.ui.Upload.FinishedEvent;
import com.vaadin.ui.Upload.FinishedListener;
import com.vaadin.ui.Upload.Receiver;

// ————————————————————————————————-
/**
* The receiver is used to process the upload data. In this case, the data is saved as a file on
* hard disk but there are a lot more possibilities to use the outputstream of the upload. Second
* functionality is to refresh the label of the form field with the filename of the upload.
*
* @author Sven Friedrichs, 2014
*/
// ————————————————————————————————-
public class FileUploadReceiver implements Receiver, FinishedListener
{

  private static final long   serialVersionUID = 1L;

  // Upload directory.
  private static final String UPLOAD_DIR       = "/tmp/uploads";

  // Filename of the upload.
  protected String            fileName;

  // Form field.
  protected SelectFileField   field;

  // ———————————————————————————————–
  /**
   * Constructor.
   */
  // ———————————————————————————————–
  public FileUploadReceiver(SelectFileField field)
  {
    this.field = field;
  }

  // ———————————————————————————————–
  /*
   * (non-Javadoc)
   * @see com.vaadin.ui.Upload.Receiver#receiveUpload(java.lang.String, java.lang.String)
   */
  // ———————————————————————————————–
  public OutputStream receiveUpload(String filename, String mimeType)
  {
    fileName = filename;

    File file = null;
    FileOutputStream fos = null;
    try
    {
      // Open the file for writing.
      file = new File(UPLOAD_DIR + File.separator + fileName);
      fos = new FileOutputStream(file);
    }
    catch (FileNotFoundException e)
    {
      throw new ActivitiIllegalArgumentException("Could not write to file " + UPLOAD_DIR
          + File.separator + fileName);
    }

    return fos;
  }

  // ———————————————————————————————–
  /*
   * (non-Javadoc)
   * @see com.vaadin.ui.Upload.FinishedListener#uploadFinished(com.vaadin.ui.Upload.FinishedEvent)
   */
  // ———————————————————————————————–
  public void uploadFinished(FinishedEvent event)
  {
    System.out.println("Upload of " + UPLOAD_DIR + File.separator + fileName);
    field.setValue(fileName);
  }

}
</java>

<java>
package org.activiti.explorer.ui.form;

import org.activiti.engine.form.FormProperty;
import org.activiti.explorer.Messages;
import org.activiti.explorer.form.FileFormType;

import com.vaadin.ui.Field;

// ————————————————————————————————-
/**
* Minimal implementation of a custom form type renderer.
*
* @author Sven Friedrichs, 2014
*/
// ————————————————————————————————-
public class FileFormPropertyRenderer extends AbstractFormPropertyRenderer
{

  private static final long serialVersionUID = 1L;

  // ———————————————————————————————–
  /**
   * Constructor.
   */
  // ———————————————————————————————–
  public FileFormPropertyRenderer()
  {
    super(FileFormType.class);
  }

  // ———————————————————————————————–
  /*
   * (non-Javadoc)
   * @see
   * org.activiti.explorer.ui.form.AbstractFormPropertyRenderer#getPropertyField(org.activiti.engine
   * .form.FormProperty)
   */
  // ———————————————————————————————–
  @Override
  public Field getPropertyField(FormProperty formProperty)
  {
    SelectFileField selectFileField = new SelectFileField(getPropertyLabel(formProperty));
    selectFileField.setRequired(formProperty.isRequired());
    selectFileField.setRequiredError(getMessage(Messages.FORM_FIELD_REQUIRED,
        getPropertyLabel(formProperty)));
    selectFileField.setEnabled(formProperty.isWritable());

    if (formProperty.getValue() != null)
    {
      selectFileField.setValue(formProperty.getValue());
    }

    return selectFileField;
  }

}
</java>

<java>
package org.activiti.explorer.ui.form;

import java.util.Collection;

import org.activiti.explorer.ExplorerApp;
import org.activiti.explorer.I18nManager;
import org.activiti.explorer.Messages;
import org.activiti.explorer.ui.FileUploadReceiver;
import org.activiti.explorer.ui.custom.UploadPopupWindow;
import org.activiti.explorer.ui.mainlayout.ExplorerLayout;

import com.vaadin.data.Property;
import com.vaadin.data.Validator;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Field;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.themes.Reindeer;

// ————————————————————————————————-
/**
* Custom form field which is used for document upload in activiti user tasks. It is based on the
* implementation of the user component and it uses the upload component which is known from the
* deployment tab in activiti-explorer. This component mainly consists of a label, a button and an
* invisible textfield. The label presents the filename of the upload to the user after it is
* finished. The invisible textfield also contains the filename in order to save it as a process
* variable. The button opens up a dialog which is used to select the file to upload. Please refer
* to the upload component for further information on the functionality.
*
* @author Sven Friedrichs, 2014
*/
// ————————————————————————————————-
public class SelectFileField extends HorizontalLayout implements Field
{

  private static final long    serialVersionUID = 1L;

  // Text of the label before upload.
  private static final String  NO_FILE_SELECTED = "Keine Datei ausgewählt";

  // Button text.
  private static final String  FORM_FILE_SELECT = "Auswählen";

  protected I18nManager        i18nManager;

  // Receiver to process upload data.
  protected FileUploadReceiver receiver;

  // Invisible textfield.
  protected TextField          wrappedField;

  // Label to present the filename.
  protected Label              selectedFileLabel;

  // Button to open up the dialog.
  protected Button             selectFileButton;

  // ———————————————————————————————–
  /**
   * Constructor.
   */
  // ———————————————————————————————–
  public SelectFileField(String caption)
  {
    i18nManager = ExplorerApp.get().getI18nManager();
    receiver = new FileUploadReceiver(this);

    setSpacing(true);
    setCaption(caption);

    selectedFileLabel = new Label();
    selectedFileLabel.setValue(NO_FILE_SELECTED);
    selectedFileLabel.addStyleName(ExplorerLayout.STYLE_FORM_NO_USER_SELECTED);
    addComponent(selectedFileLabel);

    selectFileButton = new Button();
    selectFileButton.addStyleName(Reindeer.BUTTON_SMALL);
    selectFileButton.setCaption(FORM_FILE_SELECT);
    addComponent(selectFileButton);

    selectFileButton.addListener(new ClickListener()
    {
      private static final long serialVersionUID = 1L;

      public void buttonClick(ClickEvent event)
      {
        final UploadPopupWindow window = new UploadPopupWindow(i18nManager
            .getMessage(Messages.UPLOAD_SELECT), "Bitte Datei auswählen", receiver);
        window.addFinishedListener(receiver);
        ExplorerApp.get().getViewManager().showPopupWindow(window);
      }
    });

    wrappedField = new TextField();
    wrappedField.setVisible(false);
    addComponent(wrappedField);
  }

  public boolean isInvalidCommitted()
  {
    return wrappedField.isInvalidCommitted();
  }

  public void setInvalidCommitted(boolean isCommitted)
  {
    wrappedField.setInvalidCommitted(isCommitted);
  }

  public void commit() throws SourceException, InvalidValueException
  {
    wrappedField.commit();
  }

  public void discard() throws SourceException
  {
    wrappedField.discard();
  }

  public boolean isWriteThrough()
  {
    return wrappedField.isWriteThrough();
  }

  public void setWriteThrough(boolean writeThrough) throws SourceException, InvalidValueException
  {
    wrappedField.setWriteThrough(true);
  }

  public boolean isReadThrough()
  {
    return wrappedField.isReadThrough();
  }

  public void setReadThrough(boolean readThrough) throws SourceException
  {
    wrappedField.setReadThrough(readThrough);
  }

  public boolean isModified()
  {
    return wrappedField.isModified();
  }

  public void addValidator(Validator validator)
  {
    wrappedField.addValidator(validator);
  }

  public void removeValidator(Validator validator)
  {
    wrappedField.removeValidator(validator);
  }

  public Collection<Validator> getValidators()
  {
    return wrappedField.getValidators();
  }

  public boolean isValid()
  {
    return wrappedField.isValid();
  }

  public void validate() throws InvalidValueException
  {
    wrappedField.validate();
  }

  public boolean isInvalidAllowed()
  {
    return wrappedField.isInvalidAllowed();
  }

  public void setInvalidAllowed(boolean invalidValueAllowed) throws UnsupportedOperationException
  {
    wrappedField.setInvalidAllowed(invalidValueAllowed);
  }

  public Object getValue()
  {
    return wrappedField.getValue();
  }

  public void setValue(Object newValue) throws ReadOnlyException, ConversionException
  {
    wrappedField.setValue(newValue);

    // Update label
    if (newValue != null)
    {
      selectedFileLabel.setValue(newValue);
    }
    else
    {
      selectedFileLabel.setValue(NO_FILE_SELECTED);
    }
  }

  public Class<?> getType()
  {
    return wrappedField.getType();
  }

  public void addListener(ValueChangeListener listener)
  {
    wrappedField.addListener(listener);
  }

  public void removeListener(ValueChangeListener listener)
  {
    wrappedField.removeListener(listener);
  }

  public void valueChange(com.vaadin.data.Property.ValueChangeEvent event)
  {
    wrappedField.valueChange(event);
  }

  public void setPropertyDataSource(Property newDataSource)
  {
    wrappedField.setPropertyDataSource(newDataSource);
  }

  public Property getPropertyDataSource()
  {
    return wrappedField.getPropertyDataSource();
  }

  public int getTabIndex()
  {
    return wrappedField.getTabIndex();
  }

  public void setTabIndex(int tabIndex)
  {
    wrappedField.setTabIndex(tabIndex);
  }

  public boolean isRequired()
  {
    return wrappedField.isRequired();
  }

  public void setRequired(boolean required)
  {
    wrappedField.setRequired(required);
  }

  public void setRequiredError(String requiredMessage)
  {
    wrappedField.setRequiredError(requiredMessage);
  }

  public String getRequiredError()
  {
    return wrappedField.getRequiredError();
  }

  @Override
  public void focus()
  {
    wrappedField.focus();
  }
}
</java>