org.mmbase.applications.xmlimporter
Class Transaction

java.lang.Object
  extended by org.mmbase.applications.xmlimporter.Transaction
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
InteractiveTransaction

public class Transaction
extends Object
implements Runnable

This class models a Temporary Cloud Transaction. The class:


See also the xml description in the tcp 2.0 project.

Since:
MMBase-1.5
Version:
$Id: Transaction.java 35335 2009-05-21 08:14:41Z michiel $
Author:
Rob van Maris: Finalist IT Group

Field Summary
protected  Consultant consultant
          Transaction information for current user.
 
Constructor Summary
protected Transaction(UserTransactionInfo uti, String key, String id, boolean commitOnClose, long timeOut)
          Creates new Transaction.
protected Transaction(UserTransactionInfo uti, String key, String id, boolean commitOnClose, long timeOut, File reportFile, Consultant consultant)
          Creates new Transaction.
 
Method Summary
 TmpObject accessObject(String objectId, int mmbaseId)
          Create access object for an object in the persistent cloud, in the context of this transaction.
 void appendReportBuffer(String str)
          Add text to reportBufferFile of this transaction.
protected  TmpObject caculateMerge(TmpObject tempObj1, TmpObject tempObj2, ObjectMerger merger)
          Calculates the fields that would result from merging two temporary objects in this transaction.
 void commit()
          Commit this transaction.
 TmpObject createObject(String objectId, String type, boolean disposeWhenNotReferenced)
          Create object in the context of this transaction.
 TmpObject createRelation(String objectId, String type, String source, String destination)
          Create relation in the context of this transaction.
static Transaction createTransaction(UserTransactionInfo uti, String id, boolean commitOnClose, long timeOut)
          Create a Transaction.
static Transaction createTransaction(UserTransactionInfo uti, String id, boolean commitOnClose, long timeOut, File reportFile, Consultant consultant)
          Create a Transaction.
 void delete()
          Delete this transaction.
 void deleteObject(TmpObject tmpObject)
          Deletes object from the context of this transaction, as well as its relations.
(package private)  void dropIfRequested(TmpObject tmpObject)
          Drops an object from the temporary cloud, based on its disposeWhenNotReferenced flag - i.e.
protected  boolean equalRelations(TmpObject tmpObj1, TmpObject tmpObj2)
          Test if two objects in the temporary cloud represent the same relation (are of same relation type and have the same source and destination objects).
 TmpObject getAccessObject(int mmbaseId)
          Gets an access object for a specified node in the persistent cloud.
 String getKey()
          Key accessor.
 TmpObject getMergedObject(TmpObject tempObj1)
          Gets merged object, resulting from previous merge operations.
(package private)  HashMap<String,TmpObject> getObjectContexts()
          Gets HashMap of all non-anonymous object contexts, mapped by their id.
 List<TmpObject> getRelations(TmpObject tmpObject)
          For an object in this transaction, looks up all its relations, and returns access objects for these.
 List<TmpObject> getTmpObjects()
          Gets (unmodifiable) list of all temporary objects in the transaction.
protected  boolean handleDuplicates(TmpObject tempObj, List<TmpObject> similarObjects, ObjectMerger merger)
          Handles sitiuations where more then one similar objects are found to merge with.
 void leave()
          This method should be called when leaving this transaction's context.
 void markDeleteObject(TmpObject tmpObject, boolean deleteRelations)
          Mark object in the context of this transaction for deletion.
 void merge(TmpObject tempObj1, TmpObject tempObj2, ObjectMerger merger)
          Merges two temporary objects in this transaction.
 void mergeObjects(String objectType, SimilarObjectFinder finder, ObjectMerger merger)
          Merges all objects in this transaction of a given type.
 TmpObject openObject(String objectId)
          Open object in the context of this transaction.
static Transaction openTransaction(UserTransactionInfo uti, String id, boolean commitOnClose)
          Open previously created transaction.
 void run()
          Wait assynchronously for the transaction to time out.
protected  void start()
          Start the Transaction.
protected  boolean stillExists(TmpObject tmpObject)
          Tests if this object still exists in the temporary cloud.
protected  void stop()
          Stop the Transaction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

consultant

protected Consultant consultant
Transaction information for current user.

Constructor Detail

Transaction

protected Transaction(UserTransactionInfo uti,
                      String key,
                      String id,
                      boolean commitOnClose,
                      long timeOut,
                      File reportFile,
                      Consultant consultant)
Creates new Transaction.

Parameters:
timeOut - if the transactions is not finished after the timeout (in seconds) the transaction is cancelled.
uti - transaction info for current user.
key - TransactionManager key for this transaction.
id - TransactionHandler id for this transactions.
commitOnClose - - The user-specified commit-on-close setting. True if this transaction is to be committed when the user leaves it's context, false otherwise.
reportFile - The file to use as reportfile.
consultant - The intermediate import object. Used to set and get status from and set and get objects to and from.

Transaction

protected Transaction(UserTransactionInfo uti,
                      String key,
                      String id,
                      boolean commitOnClose,
                      long timeOut)
Creates new Transaction.

Parameters:
timeOut - if the transactions is not finished after the timeout (in seconds) the transaction is cancelled.
uti - transaction info for current user.
key - TransactionManager key for this transaction.
id - TransactionHandler id for this transactions.
commitOnClose - - The user-specified commit-on-close setting. True if this transaction is to be committed when the user leaves it's context, false otherwise.
Method Detail

createTransaction

public static Transaction createTransaction(UserTransactionInfo uti,
                                            String id,
                                            boolean commitOnClose,
                                            long timeOut)
                                     throws TransactionHandlerException
Create a Transaction.

Parameters:
uti - - The UserTransactionInfo it belongs to.
id - - The user-specified id, null for anonymous transaction.
commitOnClose - - The user-specified commit-on-close setting. True if this transaction is to be committed when the user leaves it's context, false otherwise.
timeOut - - The user-specified time-out setting.
Returns:
- New transaction.
Throws:
TransactionHandlerException - - When failing to create the new transaction.

createTransaction

public static Transaction createTransaction(UserTransactionInfo uti,
                                            String id,
                                            boolean commitOnClose,
                                            long timeOut,
                                            File reportFile,
                                            Consultant consultant)
                                     throws TransactionHandlerException
Create a Transaction.

Parameters:
uti - the UserTransactionInfo it belongs to.
id - the user-specified id, null for anonymous transaction.
commitOnClose - - The user-specified commit-on-close setting. True if this transaction is to be committed when the user leaves it's context, false otherwise.
timeOut - the user-specified time-out setting.
reportFile - The reportfile.
consultant - The intermediate import object. Used to set and get status from and set and get objects to and from.
Returns:
new transaction.
Throws:
TransactionHandlerException - When failing to create the new transaction.

openTransaction

public static Transaction openTransaction(UserTransactionInfo uti,
                                          String id,
                                          boolean commitOnClose)
                                   throws TransactionHandlerException
Open previously created transaction. This assumes that the transaction is not anonymous.

Parameters:
commitOnClose - - The user-specified commit-on-close setting. True if this transaction is to be committed when the user leaves it's context, false otherwise.
uti - the UserTransactionInfo it belongs to.
id - the user-specified id (not null).
Returns:
the transaction.
Throws:
TransactionHandlerException - if the transaction does not exist.

leave

public void leave()
           throws TransactionHandlerException
This method should be called when leaving this transaction's context. This will commit the transaction, when commitOnClose is set.

Throws:
TransactionHandlerException - when a failure occured while committing the transaction.

commit

public void commit()
            throws TransactionHandlerException
Commit this transaction.

Throws:
TransactionHandlerException - when a failure occured while committing the transaction:
  • an exception thrown by MMBase TransactionManager.
  • an error while accessing the reportfile.
  • a transaction has timed out

delete

public void delete()
            throws TransactionHandlerException
Delete this transaction.

Throws:
TransactionHandlerException - when:
  • - a failure occured while deleting the transaction
  • - transaction has timed out

createObject

public TmpObject createObject(String objectId,
                              String type,
                              boolean disposeWhenNotReferenced)
                       throws TransactionHandlerException
Create object in the context of this transaction.

Parameters:
objectId - user-specified id for the new object (must be unique in this transaction context), or null for anonymous object.
type - type of the new object.
disposeWhenNotReferenced - flag: true if this object is to be dropped when it has no relations on commit, false otherwise.
Returns:
the new object in the temporary cloud.
Throws:
TransactionHandlerException - When failing to create the new object.

createRelation

public TmpObject createRelation(String objectId,
                                String type,
                                String source,
                                String destination)
                         throws TransactionHandlerException
Create relation in the context of this transaction.

Parameters:
objectId - user-specified id for the new object (must be unique in this transaction context), or null for anonymous object.
type - type of the new relation.
source - the user-specified id of the source object.
destination - the user-specified id of the destination object.
Returns:
the new relation object in the temporary cloud.
Throws:
TransactionHandlerException - when
  • an object with this id already exists in this transaction context
  • a relation object can't be created
  • an object can't be added to a transaction

openObject

public TmpObject openObject(String objectId)
                     throws TransactionHandlerException
Open object in the context of this transaction.

Parameters:
objectId - the user-specified id of the object.
Returns:
the object in the temporary cloud.
Throws:
TransactionHandlerException - when the id does not exist in the context of this transactions.

accessObject

public TmpObject accessObject(String objectId,
                              int mmbaseId)
                       throws TransactionHandlerException
Create access object for an object in the persistent cloud, in the context of this transaction. If an accessObjects with the same objectId and mmbaseId then the already existing accessObject is returned. This because in a stylesheet it's hard to establish if an object already exists.

Parameters:
objectId - user-specified id of the new object (must be unique in this transaction context).
mmbaseId - the mmbase id for the persistent object.
Returns:
the access object in the temporary cloud.
Throws:
TransactionHandlerException - When failing to create the access object.

deleteObject

public void deleteObject(TmpObject tmpObject)
                  throws TransactionHandlerException
Deletes object from the context of this transaction, as well as its relations. Note that, when the object is an relation, this may affect its source and/or destination object as well: if it (the source/destination) is an input object that has its disposedWhenNotReferenced flag set and this is the last relation that references it, it is deleted as well.

Parameters:
tmpObject - the object.
Throws:
TransactionHandlerException - When failing to delete the object.

markDeleteObject

public void markDeleteObject(TmpObject tmpObject,
                             boolean deleteRelations)
                      throws TransactionHandlerException
Mark object in the context of this transaction for deletion.

Parameters:
deleteRelations - - Set to true if all relations are to be deleted too, set to false otherwise.
tmpObject - - The object.
Throws:
TransactionHandlerException - - When a failure occurred.

mergeObjects

public void mergeObjects(String objectType,
                         SimilarObjectFinder finder,
                         ObjectMerger merger)
                  throws TransactionHandlerException
Merges all objects in this transaction of a given type.

Parameters:
objectType - The type of the objects to merge.
finder - SimilarObjectFinder instance that prescribes the actions necessary to find similar objects.
merger - ObjectMerger instance that prescribes the actions necessary to merge similar objects.
Throws:
TransactionHandlerException - When a failure occurred.

handleDuplicates

protected boolean handleDuplicates(TmpObject tempObj,
                                   List<TmpObject> similarObjects,
                                   ObjectMerger merger)
                            throws TransactionHandlerException
Handles sitiuations where more then one similar objects are found to merge with. This implementation writes the transaction to a file includng comments on the duplicates found. The transaction is cancelled. The file can be used to examine the duplicates and process the transaction later on.

Parameters:
tempObj - the original object.
similarObjects - the similar objects.
merger - the merger.
Returns:
True if duplicates are resolved. This implementation always returns false because duplicates are not resolved. Throws TransactionHandlerException When failing to handle the duplicates as desired.
Throws:
TransactionHandlerException - When a failure occurred.

caculateMerge

protected TmpObject caculateMerge(TmpObject tempObj1,
                                  TmpObject tempObj2,
                                  ObjectMerger merger)
Calculates the fields that would result from merging two temporary objects in this transaction. This does not affect the actual objects in the transaction. A new TmpObject is created outside this transaction, to hold the result.

Parameters:
merger - ObjectMerger instance that prescribes the actions necessary to merge similar objects.
tempObj1 - First object.
tempObj2 - Second object.
Returns:
A TmpObject instance that holds the calculated fields. This object exists outside this transaction.

merge

public void merge(TmpObject tempObj1,
                  TmpObject tempObj2,
                  ObjectMerger merger)
           throws TransactionHandlerException
Merges two temporary objects in this transaction. Afterwards one of these will contain the merged object and the other one will have been deleted. If neither of the objects is an access object, the first one will become the merged object, and the second will be deleted.

Parameters:
merger - ObjectMerger instance that prescribes the actions necessary to merge similar objects.
tempObj1 - First object.
tempObj2 - Second object.
Throws:
TransactionHandlerException - When a failure occurred.

getAccessObject

public TmpObject getAccessObject(int mmbaseId)
                          throws TransactionHandlerException
Gets an access object for a specified node in the persistent cloud. If such an access object already exists in this transaction, this object is returned, otherwise a new access object is created.

Parameters:
mmbaseId - MMBase number of the specified node.
Returns:
the access object in this transaction context, or null if such an access object does not exist.
Throws:
TransactionHandlerException - When unable to create the access object.

getRelations

public List<TmpObject> getRelations(TmpObject tmpObject)
                             throws TransactionHandlerException
For an object in this transaction, looks up all its relations, and returns access objects for these.

Parameters:
tmpObject - An object in the temporary cloud (can be an access objects).
Returns:
List of access objects for the found relations.
Throws:
TransactionHandlerException - When a failure occurred.

dropIfRequested

void dropIfRequested(TmpObject tmpObject)
               throws TransactionHandlerException
Drops an object from the temporary cloud, based on its disposeWhenNotReferenced flag - i.e. drop it only when it is not an access object, is has no relations and the flag is set.

Parameters:
tmpObject - An object in the temporary cloud.
Throws:
TransactionHandlerException - When a failure occurred.

equalRelations

protected boolean equalRelations(TmpObject tmpObj1,
                                 TmpObject tmpObj2)
Test if two objects in the temporary cloud represent the same relation (are of same relation type and have the same source and destination objects). This takes into account that an (access) object in the temporary cloud may represent an object in the persistent cloud.

Parameters:
tmpObj1 - The first object.
tmpObj2 - The second object.
Returns:
True if both objects represent the same relation, false otherwise.

stillExists

protected boolean stillExists(TmpObject tmpObject)
Tests if this object still exists in the temporary cloud.

Parameters:
tmpObject - A temporary object.
Returns:
True if the object still exists in the temporary cloud, false otherwise.

getKey

public String getKey()
Key accessor.

Returns:
TransactionManager key.

getObjectContexts

HashMap<String,TmpObject> getObjectContexts()
Gets HashMap of all non-anonymous object contexts, mapped by their id.

Returns:
the object context map.

getTmpObjects

public List<TmpObject> getTmpObjects()
Gets (unmodifiable) list of all temporary objects in the transaction.

Returns:
List of all temporary objects in the transaction.

getMergedObject

public TmpObject getMergedObject(TmpObject tempObj1)
Gets merged object, resulting from previous merge operations. After a merge the result is put in a map with the deleted object as key so it can be looked up by this method.

Parameters:
tempObj1 - The original object.
Returns:
The object that contains the result of the previous merge operation. This can be tempObj1 itself or the object tempObj1 is merged with.

appendReportBuffer

public void appendReportBuffer(String str)
Add text to reportBufferFile of this transaction.

Parameters:
str - Text to add to reportBuffer.

start

protected void start()
Start the Transaction. If it is not stopped explicitly (commit or delete), it will timeout eventually.


stop

protected void stop()
Stop the Transaction.


run

public void run()
Wait assynchronously for the transaction to time out. This can be ended by invoking stop().

Specified by:
run in interface Runnable


MMBase 2.0-SNAPSHOT - null