Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
Clean up and added additional test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
srinicodebytes committed May 29, 2015
1 parent 05907dc commit 5351722
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 118 deletions.
167 changes: 81 additions & 86 deletions src/com/google/enterprise/adaptor/documentum/DocumentumAcls.java
Expand Up @@ -16,6 +16,7 @@

import static java.util.Collections.singletonList;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.enterprise.adaptor.Acl;
Expand Down Expand Up @@ -55,15 +56,16 @@ class DocumentumAcls {
private static Logger logger =
Logger.getLogger(DocumentumAcls.class.getName());

@VisibleForTesting
static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

private static String aclModifiedDate;
private static String aclModifyId;

private final IDfClientX dmClientX;
private final IDfSession dmSession;
private final Principals principals;

private static String aclModifiedDate;
private static String aclModifyId;

DocumentumAcls(IDfClientX dmClientX, IDfSession dmSession,
Principals principals) {
Preconditions.checkNotNull(dmClientX, "dmClientX may not be null");
Expand All @@ -81,6 +83,39 @@ private IDfQuery makeAclQuery() {
return query;
}

private IDfQuery makeUpdateAclQuery() {
StringBuilder queryStr = new StringBuilder(
"select r_object_id, chronicle_id, audited_obj_id, event_name, "
+ "time_stamp_utc, "
+ "DATETOSTRING(time_stamp_utc, 'yyyy-mm-dd hh:mi:ss') "
+ "as time_stamp_utc_str "
+ "from dm_audittrail_acl "
+ "where (event_name='dm_save' or event_name='dm_saveasnew' "
+ "or event_name='dm_destroy')");

String whereBoundedClause = " and ((time_stamp_utc = "
+ "date(''{0}'',''yyyy-mm-dd hh:mi:ss'') and (r_object_id > ''{1}'')) "
+ "OR (time_stamp_utc > date(''{0}'',''yyyy-mm-dd hh:mi:ss'')))";

if (Strings.isNullOrEmpty(aclModifiedDate)) {
aclModifiedDate = new SimpleDateFormat(DATE_FORMAT).format(new Date());
logger.log(Level.FINE, "Setting ModifiedDate to now: {0}",
aclModifiedDate);
aclModifyId = "0";
}

// TODO (Srinivas): Adjust modified date to server TZ offset
Object[] arguments = {aclModifiedDate, aclModifyId};
queryStr.append(MessageFormat.format(whereBoundedClause, arguments));
queryStr.append(" order by time_stamp_utc, r_object_id, event_name");
logger.log(Level.FINE, "Modify date: {0} ; Modify ID: {1}", arguments);
logger.log(Level.FINER, "Update ACL query: {0}", queryStr);

IDfQuery query = dmClientX.getQuery();
query.setDQL(queryStr.toString());
return query;
}

/**
* Returns all Documentum ACLs in map with doc id and Acl.
*
Expand Down Expand Up @@ -113,6 +148,49 @@ public Map<DocId, Acl> getAcls() throws DfException {
}
}

/**
* Returns all updated Documentum ACLs in map with doc id and Acl.
*
* In Documentum, ACLs are high level objects separate from content objects.
* An ACL can be applied to one or many content objects. Or, each object
* can have it's own individual ACL applied to it. So this method needs to
* send all the ACLs in Documentum to GSA.
*
* @return Documentum ACLs in map
* @throws DfException if error in getting ACL information.
*/
public Map<DocId, Acl> getUpdateAcls() throws DfException {
HashSet<String> aclModifiedIds = new HashSet<String>();
IDfQuery query = makeUpdateAclQuery();
IDfCollection dmAclCollection =
query.execute(dmSession, IDfQuery.DF_EXECREAD_QUERY);
try {
Map<DocId, Acl> aclMap = new HashMap<DocId, Acl>();
while (dmAclCollection.next()) {
aclModifiedDate = dmAclCollection.getString("time_stamp_utc_str");
aclModifyId = dmAclCollection.getString("r_object_id");
String chronicleId = dmAclCollection.getString("chronicle_id");
String modifyObjectId = dmAclCollection.getString("audited_obj_id");

if (aclModifiedIds.contains(chronicleId)) {
logger.log(Level.FINE,
"Skipping redundant modify of: {0}", chronicleId);
continue;
}
IDfACL dmAcl = (IDfACL) dmSession.getObject(new DfId(modifyObjectId));
addAclChainToMap(dmAcl, modifyObjectId, aclMap);
aclModifiedIds.add(chronicleId);
}
return aclMap;
} finally {
try {
dmAclCollection.close();
} catch (DfException e) {
logger.log(Level.WARNING, "Error closing collection", e);
}
}
}

/**
* Adds all of the Adaptor Acls for the Documentum ACL to the map.
*
Expand Down Expand Up @@ -247,87 +325,4 @@ private Acl getBasicAcl(String objectId, String parentAclId,
}
return builder.build();
}

/**
* Returns all updated Documentum ACLs in map with doc id and Acl.
*
* In Documentum, ACLs are high level objects separate from content objects.
* An ACL can be applied to one or many content objects. Or, each object
* can have it's own individual ACL applied to it. So this method needs to
* send all the ACLs in Documentum to GSA.
*
* @return Documentum ACLs in map
* @throws DfException if error in getting ACL information.
*/
public Map<DocId, Acl> getUpdateAcls() throws DfException {
HashSet<String> aclModifiedIds = new HashSet<String>();
IDfQuery query = makeUpdateAclQuery();
IDfCollection dmAclCollection =
query.execute(dmSession, IDfQuery.DF_EXECREAD_QUERY);
try {
Map<DocId, Acl> aclMap = new HashMap<DocId, Acl>();
while (dmAclCollection.next()) {
aclModifyId = dmAclCollection.getString("r_object_id");
aclModifiedDate = dmAclCollection.getString("time_stamp_utc_str");
String chronicleId = dmAclCollection.getString("chronicle_id");
String modifyObjectId = dmAclCollection.getString("audited_obj_id");

if (aclModifiedIds.contains(chronicleId)) {
logger.log(Level.FINE,
"Skipping redundant modify of: {0}", chronicleId);
continue;
}
IDfACL dmAcl = (IDfACL) dmSession.getObject(new DfId(modifyObjectId));
addAclChainToMap(dmAcl, modifyObjectId, aclMap);
aclModifiedIds.add(chronicleId);
}
return aclMap;
} finally {
try {
dmAclCollection.close();
} catch (DfException e) {
logger.log(Level.WARNING, "Error closing collection", e);
}
}
}

private IDfQuery makeUpdateAclQuery() {
StringBuilder queryStr = new StringBuilder(
"select r_object_id, chronicle_id, audited_obj_id, event_name, "
+ "time_stamp_utc, "
+ "DATETOSTRING(time_stamp_utc, 'yyyy-mm-dd hh:mi:ss') "
+ "as time_stamp_utc_str "
+ "from dm_audittrail_acl "
+ "where (event_name='dm_save' or event_name='dm_saveasnew' "
+ "or event_name='dm_destroy')");

String whereBoundedClause = " and ((time_stamp_utc = "
+ "date(''{0}'',''yyyy-mm-dd hh:mi:ss'') and (r_object_id > ''{1}'')) "
+ "OR (time_stamp_utc > date(''{0}'',''yyyy-mm-dd hh:mi:ss'')))";
String whereBoundedClauseDateOnly = " and (time_stamp_utc > "
+ "date(''{0}'',''yyyy-mm-dd hh:mi:ss''))";

if (Strings.isNullOrEmpty(aclModifiedDate)) {
aclModifiedDate = getNOW();
logger.log(Level.FINE, "Setting ModifiedDate to now: {0}",
aclModifiedDate);
}

Object[] arguments = {aclModifiedDate, aclModifyId};
queryStr.append(MessageFormat.format(
(arguments[1] == null) ? whereBoundedClauseDateOnly
: whereBoundedClause, arguments));
queryStr.append(" order by time_stamp_utc, r_object_id, event_name");
logger.log(Level.FINE, "ModifiedDate: {0} ; Modify Id: {1}", arguments);
logger.log(Level.FINER, "UpdateAcl query: {0}", queryStr.toString());

IDfQuery query = dmClientX.getQuery();
query.setDQL(queryStr.toString());
return query;
}

private String getNOW() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(new Date());
}
}
Expand Up @@ -2735,19 +2735,13 @@ private class CollectionMock {

public CollectionMock(String query) throws SQLException {
stmt = jdbcFixture.getConnection().createStatement();
query = query.replace("DATETOSTRING(time_stamp_utc, "
+ "'yyyy-mm-dd hh:mi:ss') as time_stamp_utc_str",
"time_stamp_utc as time_stamp_utc_str");
query = query.replace("date", "convert(parseDateTime");
query = query.replace("'yyyy-mm-dd hh:mi:ss')", "'"
+ DocumentumAcls.DATE_FORMAT + "'), timestamp)");
query = query.replace("DATETOSTRING", "FORMATDATETIME")
.replace("date(", "parseDateTime(")
.replace("yyyy-mm-dd hh:mi:ss", DocumentumAcls.DATE_FORMAT);
rs = stmt.executeQuery(query);
}

public String getString(String colName) throws SQLException {
if (colName.equals("time_stamp_utc_str")) {
colName = "time_stamp_utc";
}
return rs.getString(colName);
}

Expand Down Expand Up @@ -2987,19 +2981,17 @@ private DocumentumAcls getDocumentumAcls() throws DfException {
}

@Test
public void testUpdateAcls() throws DfException, InterruptedException,
SQLException {
public void testUpdateAcls() throws Exception {
createAcl("4501081f80000100");
createAcl("4501081f80000101");
createAcl("4501081f80000102");
String dateStr = getDateString(5);
String dateStr = getNowPlusMinutes(5);
insertAclAudit("123", "234", "4501081f80000100", "dm_save", dateStr);
insertAclAudit("124", "235", "4501081f80000101", "dm_saveasnew", dateStr);
insertAclAudit("125", "236", "4501081f80000102", "dm_destroy", dateStr);

Map<DocId, Acl> aclMap = getDocumentumAcls().getUpdateAcls();
assertEquals(3, aclMap.size());
assertEquals( ImmutableSet.of(
assertEquals(ImmutableSet.of(
new DocId("4501081f80000100"),
new DocId("4501081f80000101"),
new DocId("4501081f80000102")), aclMap.keySet());
Expand All @@ -3009,78 +3001,91 @@ public void testUpdateAcls() throws DfException, InterruptedException,
}

@Test
public void testUpdateAclsWithSameChronicleId() throws DfException,
InterruptedException, SQLException {
public void testUpdateAclsWithSameChronicleId() throws Exception {
createAcl("4501081f80000100");
createAcl("4501081f80000101");
createAcl("4501081f80000102");
String dateStr = getDateString(6);
String dateStr = getNowPlusMinutes(6);
insertAclAudit("123", "234", "4501081f80000100", "dm_save", dateStr);
insertAclAudit("124", "234", "4501081f80000101", "dm_saveasnew", dateStr);
insertAclAudit("125", "234", "4501081f80000102", "dm_destroy", dateStr);

Map<DocId, Acl> aclMap = getDocumentumAcls().getUpdateAcls();
assertEquals(1, aclMap.size());
assertEquals(ImmutableSet.of(new DocId("4501081f80000100")),
aclMap.keySet());
}

@Test
public void testPreviouslyUpdatedAcls() throws DfException,
InterruptedException, SQLException {
public void testPreviouslyUpdatedAcls() throws Exception {
createAcl("4501081f80000100");
createAcl("4501081f80000101");
createAcl("4501081f80000102");
String dateStr = getDateString(-10);
String dateStr = getNowPlusMinutes(-10);
insertAclAudit("123", "234", "4501081f80000100", "dm_save", dateStr);
insertAclAudit("124", "235", "4501081f80000101", "dm_saveasnew", dateStr);
insertAclAudit("125", "236", "4501081f80000102", "dm_destroy", dateStr);

Map<DocId, Acl> aclMap = getDocumentumAcls().getUpdateAcls();
assertEquals(0, aclMap.size());
assertEquals(ImmutableMap.of(), aclMap);
}

@Test
public void testMultiUpdateAcls() throws DfException, InterruptedException,
SQLException {
public void testMultiUpdateAcls() throws Exception {
DocumentumAcls dctmAcls = getDocumentumAcls();

createAcl("4501081f80000100");
createAcl("4501081f80000101");
createAcl("4501081f80000102");
String dateStr = getDateString(10);
String dateStr = getNowPlusMinutes(10);
insertAclAudit("123", "234", "4501081f80000100", "dm_save", dateStr);
insertAclAudit("124", "235", "4501081f80000101", "dm_saveasnew", dateStr);
insertAclAudit("125", "236", "4501081f80000102", "dm_saveasnew", dateStr);

Map<DocId, Acl> aclMap = dctmAcls.getUpdateAcls();
assertEquals(3, aclMap.size());
assertEquals( ImmutableSet.of(
assertEquals(ImmutableSet.of(
new DocId("4501081f80000100"),
new DocId("4501081f80000101"),
new DocId("4501081f80000102")), aclMap.keySet());

dateStr = getDateString(15);
dateStr = getNowPlusMinutes(15);
insertAclAudit("126", "237", "4501081f80000103", "dm_saveasnew", dateStr);
insertAclAudit("127", "238", "4501081f80000104", "dm_destroy", dateStr);

aclMap = dctmAcls.getUpdateAcls();
assertEquals(2, aclMap.size());
assertEquals(ImmutableSet.of(
new DocId("4501081f80000103"),
new DocId("4501081f80000104")), aclMap.keySet());
}

@Test
public void testMultiUpdateAclsWithNoResults() throws Exception {
DocumentumAcls dctmAcls = getDocumentumAcls();

createAcl("4501081f80000106");
createAcl("4501081f80000107");
String dateStr = getNowPlusMinutes(20);
insertAclAudit("128", "234", "4501081f80000106", "dm_saveasnew", dateStr);
insertAclAudit("129", "235", "4501081f80000107", "dm_saveasnew", dateStr);

Map<DocId, Acl> aclMap = dctmAcls.getUpdateAcls();
assertEquals(ImmutableSet.of(
new DocId("4501081f80000106"),
new DocId("4501081f80000107")), aclMap.keySet());

aclMap = dctmAcls.getUpdateAcls();
assertEquals(ImmutableSet.of(), aclMap.keySet());
}

/**
* Returns date string with minutes added to current time.
*
* @param min minutes to add.
* @param minutes minutes to add.
* @return date in string format.
*/
private String getDateString(int min) {
private String getNowPlusMinutes(int minutes) {
SimpleDateFormat format = new SimpleDateFormat(DocumentumAcls.DATE_FORMAT);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, min);
calendar.add(Calendar.MINUTE, minutes);
return format.format(calendar.getTime());
}
}

0 comments on commit 5351722

Please sign in to comment.