/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: DefaultResource.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.4 2007/02/27 14:34:08 drmlipp
* Some refactoring to reduce cyclic dependencies.
*
* Revision 1.3 2006/10/15 19:29:51 mlipp
* Merged changes from 1.4.x up to 1.4ea3pre1.
*
* Revision 1.2.2.1 2006/10/14 21:34:06 mlipp
* Simplified resource assignment service implementation.
*
* Revision 1.2 2006/10/03 17:04:02 mlipp
* Fixed visibility.
*
* Revision 1.1 2006/10/02 20:53:56 mlipp
* New resource base classes.
*
* Revision 1.2 2006/09/29 12:32:12 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1.1.2 2004/08/18 15:17:38 drmlipp
* Update to 1.2
*
* Revision 1.11 2002/12/19 21:37:42 lipp
* Reorganized interfaces.
*
* Revision 1.10 2002/11/26 11:23:29 lipp
* Modified RemoteException comment.
*
* Revision 1.9 2002/04/03 12:53:05 lipp
* JavaDoc fixes.
*
* Revision 1.8 2002/01/15 13:42:21 lipp
* Added resourceByKey to ResourceAssignmentService and some missing
* NoSuchResourceExceptions in various methods.
*
* Revision 1.7 2001/12/20 22:27:34 lipp
* WfResource release semantics fixed.
*
* Revision 1.6 2001/12/19 09:05:15 lipp
* Made it serializable.
*
* Revision 1.5 2001/12/17 12:14:04 lipp
* Adapted to configurable ResourceManagement/AssignmentServices.
*
* Revision 1.4 2001/12/16 21:48:57 lipp
* addAssignment implemented.
*
* Revision 1.3 2001/12/11 12:55:36 robert
* fixed parameter error
*
* Revision 1.2 2001/12/11 12:47:37 lipp
* JavaDoc fixes.
*
* Revision 1.1 2001/12/09 14:03:08 lipp
* Improved design of workflow/resource management interface.
*
*/
package de.danet.an.workflow.spis.rms;
import java.io.Serializable;
import java.util.Collection;
import java.rmi.RemoteException;
import de.danet.an.workflow.omgcore.InvalidResourceException;
import de.danet.an.workflow.omgcore.NotAssignedException;
import de.danet.an.workflow.omgcore.WfAssignment;
import de.danet.an.workflow.omgcore.WfResource;
import de.danet.an.workflow.api.Activity;
import de.danet.an.workflow.api.NoSuchResourceException;
import de.danet.an.workflow.spis.ras.ResourceAssignmentService;
/**
* This class provides a default implementation of the {@link
* de.danet.an.workflow.omgcore.WfResource <code>WfResource</code>}'s methods
* {@link de.danet.an.workflow.omgcore.WfResource#workItems
* <code>workItems</code>},
* {@link de.danet.an.workflow.omgcore.WfResource#isMemberOfWorkItems
* <code>isMemberOfWorkItems</code>} and
* {@link de.danet.an.workflow.omgcore.WfResource#release <code>release</code>}.
* The implementation is based on the methods of a
* {@link ResourceAssignmentContext <code>ResourceAssignmentContext</code>}
* passed to the constructor.
*/
public class DefaultResource implements WfResource, Serializable {
/**
* The resource service factory used.
*/
private ResourceAssignmentContext callbackHandler = null;
private String resourceKey = null;
private String resourceName = null;
/**
* The constructor. It ensures that a valid factory exists.
*
* @param assignSvc
* the callback handler
* @param key
* the resource's key
* @param name
* the resource's name
*/
public DefaultResource(ResourceAssignmentContext cbh, String key,
String name) {
callbackHandler = cbh;
resourceKey = key;
resourceName = name;
}
/**
* Retrieve the key of a resource.
*
* @return key of resource
* @throws RemoteException
* problems accessing resource
*/
public String resourceKey() throws RemoteException {
return resourceKey;
}
/**
* Retrieve the name of a resource.
*
* @return name of resource
* @throws RemoteException
* problems accessing resource
*/
public String resourceName() throws RemoteException {
return resourceName;
}
/**
* This method returns the {@link WfAssignment <code>WfAssignments</code>}s
* associated with a resource.
*
* @return the associated {@link WfAssignment <code>WfAssignments</code>}s.
* @throws RemoteException
* if a system-level error occurs.
* @throws IllegalStateException
* if the resource has become invalid. This is actually a
* remapping of the <code>NoSuchResourceException</code>
* thrown by {@link ResourceAssignmentService#workItems
* <code>ResourceAssignmentService.workItems()</code>}. It
* must be remapped because this method's signature is specified
* by
* {@link WfResource#workItems <code>WfResource.workItems()</code>}.
*/
public Collection workItems() throws RemoteException, IllegalStateException {
try {
return callbackHandler.workItems(this);
} catch (NoSuchResourceException e) {
throw new IllegalStateException(e.getMessage());
}
}
/**
* Checks if a given {@link WfAssignment <code>WfAssignment</code>} is
* associated with this resource.
*
* @param assignment
* the assignment in question.
* @return <code>true</code> if the association exists.
* @throws RemoteException
* if a system-level error occurs. This is actually a remapping
* of the <code>NoSuchResourceException</code> thrown by
* {@link ResourceAssignmentService#isMemberOfWorkItems
* <code>ResourceAssignmentService.isMemberOfWorkItems(...)</code>}.
* It must be remapped because this method's signature is
* specified by {@link WfResource#isMemberOfWorkItems
* <code>WfResource.isMemberOfWorkItems(...)</code>}.
* @throws IllegalStateException
* if the resource has become invalid.
*/
public boolean isMemberOfWorkItems(WfAssignment assignment)
throws RemoteException, IllegalStateException {
try {
return callbackHandler.isMemberOfWorkItems(this, assignment);
} catch (NoSuchResourceException e) {
throw new IllegalStateException(e.getMessage());
}
}
/**
* Signals to the resource that it is no longer needed for a specific
* assignment. The default implementation calls
* <code>removeAssignment</code> on the activity.
*
* @param fromAssignment
* the specific assignment.
* @param releaseInfo
* specifies additional information on the reason for realizing
* the resource as input.
* @throws NotAssignedException
* if the resource is not associated with the given assignment.
* @throws RemoteException
* if a system-level error occurs.
*/
public void release(WfAssignment fromAssignment, String releaseInfo)
throws RemoteException, NotAssignedException {
try {
((Activity) fromAssignment.activity())
.removeAssignment(fromAssignment.assignee());
} catch (InvalidResourceException e) {
throw (NotAssignedException) (new NotAssignedException())
.initCause(e);
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result
+ ((resourceKey == null) ? 0 : resourceKey.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final DefaultResource other = (DefaultResource) obj;
if (resourceKey == null) {
if (other.resourceKey != null) {
return false;
}
} else if (!resourceKey.equals(other.resourceKey)) {
return false;
}
return true;
}
}