* @throws IOException
*/
@SuppressWarnings("deprecation")
public OrderedPair<SObjectVO, SObjectItemVO> allocObjectItem(SBucketVO bucket, String nameKey, S3MetaDataEntry[] meta, S3AccessControlList acl, String cannedAccessPolicy)
{
SObjectItemVO item = null;
int versionSeq = 1;
int versioningStatus = bucket.getVersioningStatus();
//Session session = PersistContext.getSession();
// [A] To write into a bucket the user must have write permission to that bucket
S3PolicyContext context = new S3PolicyContext( PolicyActions.PutObject, bucket.getName());
context.setKeyName( nameKey );
context.setEvalParam( ConditionKeys.Acl, cannedAccessPolicy);
verifyAccess( context, "SBucket", bucket.getId(), SAcl.PERMISSION_WRITE ); // TODO - check this validates plain POSTs
Transaction txn = Transaction.open(Transaction.AWSAPI_DB);
txn.start();
// [B] If versioning is off them we over write a null object item
SObjectVO object = objectDao.getByNameKey(bucket, nameKey);
if ( object != null )
{
// -> if versioning is on create new object items
if ( SBucket.VERSIONING_ENABLED == versioningStatus )
{
versionSeq = object.getNextSequence();
object.setNextSequence(versionSeq + 1);
objectDao.update(object.getId(), object);
item = new SObjectItemVO();
item.setTheObject(object);
object.getItems().add(item);
item.setsObjectID(object.getId());
item.setVersion(String.valueOf(versionSeq));
Date ts = DateHelper.currentGMTTime();
item.setCreateTime(ts);
item.setLastAccessTime(ts);
item.setLastModifiedTime(ts);
item = itemDao.persist(item);
txn.commit();
//session.save(item);
}
else
{ // -> find an object item with a null version, can be null
// if bucket started out with versioning enabled and was then suspended
item = itemDao.getByObjectIdNullVersion( object.getId());
if (item == null)
{
item = new SObjectItemVO();
item.setTheObject(object);
item.setsObjectID(object.getId());
object.getItems().add(item);
Date ts = DateHelper.currentGMTTime();
item.setCreateTime(ts);
item.setLastAccessTime(ts);
item.setLastModifiedTime(ts);
item = itemDao.persist(item);
txn.commit();
}
}
}
else
{
Transaction txn1 = Transaction.open(Transaction.AWSAPI_DB);
txn1.start();
// -> there is no object nor an object item
object = new SObjectVO();
object.setBucket(bucket);
object.setNameKey(nameKey);
object.setNextSequence(2);
object.setBucketID(bucket.getId());
object.setCreateTime(DateHelper.currentGMTTime());
object.setOwnerCanonicalId(UserContext.current().getCanonicalUserId());
object = objectDao.persist(object);
item = new SObjectItemVO();
item.setTheObject(object);
item.setsObjectID(object.getId());
object.getItems().add(item);
if (SBucket.VERSIONING_ENABLED == versioningStatus) item.setVersion(String.valueOf(versionSeq));
Date ts = DateHelper.currentGMTTime();
item.setCreateTime(ts);
item.setLastAccessTime(ts);
item.setLastModifiedTime(ts);
item = itemDao.persist(item);
txn.commit();
txn.close();
}
// [C] We will use the item DB id as the file name, MD5/contentLength will be stored later
String suffix = null;
int dotPos = nameKey.lastIndexOf('.');
if (dotPos >= 0) suffix = nameKey.substring(dotPos);
if ( suffix != null )
item.setStoredPath(String.valueOf(item.getId()) + suffix);
else item.setStoredPath(String.valueOf(item.getId()));
metaDao.save("SObjectItem", item.getId(), meta);
// [D] Are we setting an ACL along with the object
// -> the ACL is ALWAYS set on a particular instance of the object (i.e., a version)
if ( null != cannedAccessPolicy )
{
setCannedAccessControls( cannedAccessPolicy, "SObjectItem", item.getId(), bucket );
}
else if (null == acl || 0 == acl.size())
{
// -> this is termed the "private" or default ACL, "Owner gets FULL_CONTROL"
setSingleAcl( "SObjectItem", item.getId(), SAcl.PERMISSION_FULL );
}
else if (null != acl) {
aclDao.save( "SObjectItem", item.getId(), acl );
}
itemDao.update(item.getId(), item);
txn.close();
return new OrderedPair<SObjectVO, SObjectItemVO>(object, item);
}