checkIfAuditable(currentItem);
AuditSession auditSession = new AuditSession(currentItem);
SessionImpl session = (SessionImpl)currentItem.getSession();
SessionDataManager dataManager = auditSession.getDataManager();
NodeData auditHistory = auditSession.getAuditHistoryNodeData();
if (auditHistory == null)
{
throw new PathNotFoundException("Audit history not found for " + currentItem.getPath());
}
// make path to the AUDITHISTORY_LASTRECORD property
QPath path = QPath.makeChildPath(auditHistory.getQPath(), AuditService.EXO_AUDITHISTORY_LASTRECORD);
// searching last name of node
PropertyData pData = (PropertyData)dataManager.getItemData(path);
String auditRecordName = String.valueOf(ValueDataUtil.getLong(pData.getValues().get(0)) + 1);
// exo:auditRecord
List<AccessControlEntry> access = new ArrayList<AccessControlEntry>();
access.add(new AccessControlEntry(IdentityConstants.ANY, PermissionType.SET_PROPERTY));
access.add(new AccessControlEntry(IdentityConstants.ANY, PermissionType.READ));
for (String identity : adminIdentitys)
{
access.add(new AccessControlEntry(identity, PermissionType.REMOVE));
}
AccessControlList exoAuditRecordAccessControlList = new AccessControlList(session.getUserID(), access);
TransientNodeData arNode =
new TransientNodeData(QPath.makeChildPath(auditHistory.getQPath(), new InternalQName(null, auditRecordName)),
IdGenerator.generate(), -1, AuditService.EXO_AUDITRECORD, new InternalQName[0],
Integer.parseInt(auditRecordName), auditHistory.getIdentifier(), exoAuditRecordAccessControlList);
// exo:auditRecord
dataManager.update(new ItemState(arNode, ItemState.ADDED, true, ((ItemImpl)currentItem).getInternalPath()), true);
// jcr:primaryType
TransientPropertyData arPrType =
TransientPropertyData.createPropertyData(arNode, Constants.JCR_PRIMARYTYPE, PropertyType.NAME, false,
new TransientValueData(arNode.getPrimaryTypeName()));
// exo:user
TransientPropertyData arUser =
TransientPropertyData.createPropertyData(arNode, AuditService.EXO_AUDITRECORD_USER, PropertyType.STRING,
false, new TransientValueData(session.getUserID()));
// exo:created
TransientPropertyData arCreated =
TransientPropertyData.createPropertyData(arNode, AuditService.EXO_AUDITRECORD_CREATED, PropertyType.DATE,
false, new TransientValueData(dataManager.getTransactManager().getStorageDataManager().getCurrentTime()));
// exo:eventType
TransientPropertyData arEventType =
TransientPropertyData.createPropertyData(arNode, AuditService.EXO_AUDITRECORD_EVENTTYPE, PropertyType.LONG,
false, new TransientValueData(eventType));
// jcr:primaryType
dataManager.update(new ItemState(arPrType, ItemState.ADDED, true, ((ItemImpl)currentItem).getInternalPath()),
true);
// exo:user
dataManager.update(new ItemState(arUser, ItemState.ADDED, true, ((ItemImpl)currentItem).getInternalPath()), true);
// exo:created
dataManager.update(new ItemState(arCreated, ItemState.ADDED, true, ((ItemImpl)currentItem).getInternalPath()),
true);
// exo:eventType
dataManager.update(new ItemState(arEventType, ItemState.ADDED, true, ((ItemImpl)currentItem).getInternalPath()),
true);
if (!currentItem.isNode())
{
int propertyType = ((Property)currentItem).getType();
if (propertyType != PropertyType.BINARY)
{
// exo:newValue
TransientPropertyData arNewValue =
TransientPropertyData.createPropertyData(arNode, AuditService.EXO_AUDITRECORD_NEWVALUE, propertyType,
((PropertyImpl)currentItem).isMultiValued(),
((PropertyData)((PropertyImpl)currentItem).getData()).getValues());
dataManager.update(
new ItemState(arNewValue, ItemState.ADDED, true, ((ItemImpl)currentItem).getInternalPath()), true);
if (eventType == Event.PROPERTY_CHANGED)
{
// exo:oldValue
TransientPropertyData arOldValue =
TransientPropertyData.createPropertyData(arNode, AuditService.EXO_AUDITRECORD_OLDVALUE, propertyType,
((PropertyImpl)previousItem).isMultiValued(),
((PropertyData)((PropertyImpl)previousItem).getData()).getValues());
dataManager.update(
new ItemState(arOldValue, ItemState.ADDED, true, ((ItemImpl)previousItem).getInternalPath()), true);
}
}
}
NodeData vancestor; // nearest versionable ancestor
if (currentItem.isNode())
{
vancestor = ((NodeImpl)currentItem).getVersionableAncestor();
}
else
{
vancestor = ((NodeImpl)((Property)currentItem).getParent()).getVersionableAncestor();
// exo:propertyName
TransientPropertyData propertyNameData =
TransientPropertyData.createPropertyData(arNode, EXO_AUDITRECORD_PROPERTYNAME, PropertyType.STRING, false,
new TransientValueData(((ItemImpl)currentItem).getInternalName()));
dataManager.update(
new ItemState(propertyNameData, ItemState.ADDED, true, ((ItemImpl)currentItem).getInternalPath()), true);
}
if (vancestor != null)
{
// auditable node under a version control, set related properties to the
// audit record
String versionUUID; // current base version UUID
StringBuilder versionName = new StringBuilder(); // current base version name + labels
PropertyData bvProp =
(PropertyData)dataManager.getItemData(vancestor, new QPathEntry(Constants.JCR_BASEVERSION, 1),
ItemType.PROPERTY);
versionUUID = ValueDataUtil.getString(bvProp.getValues().get(0));
// using JCR API objects
Version version = (Version)dataManager.getItemByIdentifier(versionUUID, false);
versionName = new StringBuilder(version.getName());
if (!dataManager.isNew(version.getParent().getUUID()))
{
VersionHistory versionHistory =
(VersionHistory)dataManager.getItemByIdentifier(version.getParent().getUUID(), false);
String[] labels = versionHistory.getVersionLabels(version);
for (int i = 0; i < labels.length; i++)
{
String vl = labels[i];
if (i == 0)
{
versionName.append(" ");
}
versionName.append("'").append(vl).append("' ");
}
}
TransientPropertyData auditVersion =
TransientPropertyData.createPropertyData(arNode, EXO_AUDITRECORD_AUDITVERSION, PropertyType.STRING, false,
new TransientValueData(versionUUID));
TransientPropertyData auditVersionName =
TransientPropertyData.createPropertyData(arNode, EXO_AUDITRECORD_AUDITVERSIONNAME, PropertyType.STRING,
false, new TransientValueData(versionName.toString()));
dataManager.update(
new ItemState(auditVersion, ItemState.ADDED, true, ((ItemImpl)currentItem).getInternalPath()), true);
dataManager.update(
new ItemState(auditVersionName, ItemState.ADDED, true, ((ItemImpl)currentItem).getInternalPath()), true);
}
// Update lastRecord
PropertyData pLastRecord =
(PropertyData)auditSession.getDataManager().getItemData(
QPath.makeChildPath(auditHistory.getQPath(), EXO_AUDITHISTORY_LASTRECORD));
pLastRecord =
new TransientPropertyData(pLastRecord.getQPath(), pLastRecord.getIdentifier(),
pLastRecord.getPersistedVersion(), pLastRecord.getType(), pLastRecord.getParentIdentifier(),
pLastRecord.isMultiValued(), new TransientValueData(String.valueOf(auditRecordName)));
dataManager.update(
new ItemState(pLastRecord, ItemState.UPDATED, true, ((ItemImpl)currentItem).getInternalPath()), true);
if (LOG.isDebugEnabled())
LOG.debug("Add audit record: " + " Item path="
+ ((ItemImpl)currentItem).getLocation().getInternalPath().getAsString() + " User=" + session.getUserID()