All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.bonita.facade.runtime.impl.ActivityBodyImpl Maven / Gradle / Ivy

/**
 * Copyright (C) 2006  Bull S. A. S.
 * Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA  02110-1301, USA.
 **/
package org.ow2.bonita.facade.runtime.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.ow2.bonita.facade.runtime.ActivityBody;
import org.ow2.bonita.facade.runtime.ActivityState;
import org.ow2.bonita.facade.runtime.StateUpdate;
import org.ow2.bonita.facade.uuid.ActivityBodyUUID;
import org.ow2.bonita.util.Misc;


public abstract class ActivityBodyImpl implements ActivityBody {

  /**
   * 
   */
  private static final long serialVersionUID = -7157649209824042925L;

  protected long dbid;

  protected ActivityBodyUUID uuid;
  protected Date startedDate;
  protected Date endedDate;
  protected Date readyDate;
  protected List stateUpdates = new ArrayList();

  protected ActivityBodyImpl() { }

  protected ActivityBodyImpl(final ActivityBodyUUID uuid) {
    super();
    this.uuid = uuid;
  }

  protected ActivityBodyImpl(final ActivityBody activityBody) {
    super();
    this.uuid = activityBody.getUUID().copy();
    this.startedDate = activityBody.getStartedDate();
    this.endedDate = activityBody.getEndedDate();
    this.readyDate = activityBody.getReadyDate();
    final List stateList = activityBody.getStateUpdates();
    if (stateList != null && !stateList.isEmpty()) {
      this.stateUpdates = new ArrayList();
      for (final StateUpdate update : stateList) {
        this.stateUpdates.add(new StateUpdateImpl(update));
      }
    }
  }

  @Override
  public String toString() {
    return this.getClass().getName()
      + "[uuid: " + getUUID()
      + ", startedDate: " + getStartedDate()
      + ", endedDate: " + getEndedDate()
      + ", readyDate: " + getReadyDate()
      + "]";
  }

  public ActivityBodyUUID getUUID() {
    return this.uuid;
  }

  public Date getStartedDate() {
    return this.startedDate;
  }

  public Date getEndedDate() {
    return this.endedDate;
  }

  public Date getReadyDate() {
    return this.readyDate;
  }

  public List getStateUpdates() {
    return this.stateUpdates;
  }

  public ActivityState getState() {
    if (getStateUpdates() != null && !getStateUpdates().isEmpty()) {
      final StateUpdate stateUpdate = getStateUpdates().get(getStateUpdates().size() - 1);
      if (stateUpdate.getActivityState() != null) {
        return stateUpdate.getActivityState();
      }
    }
    return ActivityState.INITIAL;
  }

  public void setStartedDate(final Date date) {
    Misc.badStateIfNotNull(getStartedDate(), "startedDate can not be set twice!");
    Misc.badStateIfNull(getReadyDate(), "startedDate can not be set before readyDate!");
    this.startedDate = date;
  }

  public void setEndedDate(final Date date) {
    Misc.badStateIfNotNull(getEndedDate(), "endedDate can not be set twice!");
//    Misc.badStateIfNull(getStartedDate(), "endedDate can not be set before startedDate!");
    this.endedDate = date;
  }

  public void setReadyDate(final Date date) {
    Misc.badStateIfNotNull(getReadyDate(), "readyDate can not be set twice!");
    this.readyDate = date;
  }

  public void setActivityState(final ActivityState newState, final String userId) {
    // TODO: why not simply calling getState() to have old state ?
    ActivityState oldState;
    final Date actual = new Date();
    if (getStateUpdates().isEmpty()) {
      oldState = ActivityState.INITIAL;
    } else {
      // get last state update
      final StateUpdate oldStateUpdate = getStateUpdates().get(getStateUpdates().size() - 1);
      final Date lastUpdate = oldStateUpdate.getUpdatedDate();
      //get the old task state from the previous update
      oldState = oldStateUpdate.getActivityState();
      // check last state update append before this state update
      if (actual.before(lastUpdate)) {
        UpdateImpl.logClockInconsistency();
      }
    }
    //add a state update
    this.getStateUpdates().add(new StateUpdateImpl(actual, newState, oldState, userId));
  }
}






© 2015 - 2025 Weber Informatics LLC | Privacy Policy