@SuppressWarnings("deprecation")
public Tuple<SObject, SObjectItem> allocObjectItem(SBucket bucket, String nameKey, S3MetaDataEntry[] meta, S3AccessControlList acl, String cannedAccessPolicy)
{
SObjectDao objectDao = new SObjectDao();
SObjectItemDao objectItemDao = new SObjectItemDao();
SMetaDao metaDao = new SMetaDao();
SAclDao aclDao = new SAclDao();
SObjectItem 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 );
// [A] If versioning is off them we over write a null object item
SObject object = objectDao.getByNameKey(bucket, nameKey);
if ( object != null )
{
// -> if versioning is on create new object items
if ( SBucket.VERSIONING_ENABLED == versioningStatus )
{
session.lock(object, LockMode.UPGRADE);
versionSeq = object.getNextSequence();
object.setNextSequence(versionSeq + 1);
session.save(object);
item = new SObjectItem();
item.setTheObject(object);
object.getItems().add(item);
item.setVersion(String.valueOf(versionSeq));
Date ts = DateHelper.currentGMTTime();
item.setCreateTime(ts);
item.setLastAccessTime(ts);
item.setLastModifiedTime(ts);
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 = objectItemDao.getByObjectIdNullVersion( object.getId());
if (item == null)
{
item = new SObjectItem();
item.setTheObject(object);
object.getItems().add(item);
Date ts = DateHelper.currentGMTTime();
item.setCreateTime(ts);
item.setLastAccessTime(ts);
item.setLastModifiedTime(ts);
session.save(item);
}
}
}
else
{ // -> there is no object nor an object item
object = new SObject();
object.setBucket(bucket);
object.setNameKey(nameKey);
object.setNextSequence(2);
object.setCreateTime(DateHelper.currentGMTTime());
object.setOwnerCanonicalId(UserContext.current().getCanonicalUserId());
session.save(object);
item = new SObjectItem();
item.setTheObject(object);
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);
session.save(item);
}
// [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 )