2018-05-17 12:51:56 +02:00
|
|
|
package org.gcube.data.access.storagehub.accounting;
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
2018-06-29 16:59:24 +02:00
|
|
|
import java.util.Set;
|
2018-05-17 12:51:56 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import javax.inject.Singleton;
|
|
|
|
import javax.jcr.Node;
|
|
|
|
import javax.jcr.RepositoryException;
|
|
|
|
import javax.jcr.Session;
|
2018-06-20 16:59:41 +02:00
|
|
|
import javax.jcr.UnsupportedRepositoryOperationException;
|
|
|
|
import javax.jcr.version.Version;
|
|
|
|
import javax.jcr.version.VersionHistory;
|
|
|
|
import javax.jcr.version.VersionIterator;
|
|
|
|
import javax.jcr.version.VersionManager;
|
2018-05-17 12:51:56 +02:00
|
|
|
|
|
|
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
|
|
|
import org.gcube.common.storagehub.model.items.nodes.accounting.AccountingEntryType;
|
|
|
|
import org.gcube.common.storagehub.model.types.NodeProperty;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
public class AccountingHandler {
|
|
|
|
|
2019-02-27 17:49:55 +01:00
|
|
|
private static final String VERSION_ACCOUNTING= "hl:version";
|
2018-06-20 16:59:41 +02:00
|
|
|
private static final String USER = "hl:user";
|
|
|
|
private static final String DATE = "hl:date";
|
|
|
|
private static final String ITEM_NAME = "hl:itemName";
|
|
|
|
private static final String ITEM_TYPE = "hl:itemType";
|
|
|
|
private static final String MIME_TYPE = "hl:mimeType";
|
2018-06-29 16:59:24 +02:00
|
|
|
private static final String MEMBERS = "hl:members";
|
2018-10-25 16:33:23 +02:00
|
|
|
private static final String OLD_ITEM_NAME = "hl:oldItemName";
|
|
|
|
private static final String NEW_ITEM_NAME = "hl:newItemName";
|
|
|
|
|
|
|
|
|
2018-06-20 16:59:41 +02:00
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AccountingHandler.class);
|
|
|
|
|
|
|
|
public void createReadObj(String title, Session ses, Node node, boolean saveHistory ) {
|
|
|
|
try {
|
2019-02-27 17:49:55 +01:00
|
|
|
|
|
|
|
if (!node.hasNode(NodeProperty.ACCOUNTING.toString())){
|
|
|
|
node.addNode(NodeProperty.ACCOUNTING.toString(), NodeProperty.NT_ACCOUNTING.toString());
|
2018-05-17 12:51:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:49:55 +01:00
|
|
|
Node accountingNodeParent = node.getNode(NodeProperty.ACCOUNTING.toString());
|
2018-05-17 12:51:56 +02:00
|
|
|
Node accountingNode = accountingNodeParent.addNode(UUID.randomUUID().toString(),AccountingEntryType.READ.getNodeTypeDefinition());
|
2018-06-20 16:59:41 +02:00
|
|
|
accountingNode.setProperty(USER, AuthorizationProvider.instance.get().getClient().getId());
|
|
|
|
accountingNode.setProperty(DATE, Calendar.getInstance());
|
|
|
|
accountingNode.setProperty(ITEM_NAME, title);
|
|
|
|
|
|
|
|
try {
|
|
|
|
VersionManager vManager = ses.getWorkspace().getVersionManager();
|
|
|
|
VersionHistory history = vManager.getVersionHistory(node.getNode("jcr:content").getPath());
|
|
|
|
VersionIterator versions = history.getAllVersions();
|
|
|
|
Version version= null;
|
|
|
|
while (versions.hasNext()) {
|
|
|
|
version = versions.nextVersion();
|
|
|
|
}
|
|
|
|
if (version!=null)
|
2019-02-27 17:49:55 +01:00
|
|
|
accountingNode.setProperty(VERSION_ACCOUNTING, version.getName());
|
|
|
|
}catch(UnsupportedRepositoryOperationException uropex) {
|
|
|
|
logger.warn("version cannot be retrieved", uropex);
|
|
|
|
}
|
|
|
|
if (saveHistory) ses.save();
|
|
|
|
} catch (RepositoryException e) {
|
|
|
|
logger.warn("error trying to retrieve accountign node",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void createFileUpdated(String title, Session ses, Node node, boolean saveHistory ) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (!node.hasNode(NodeProperty.ACCOUNTING.toString())){
|
|
|
|
node.addNode(NodeProperty.ACCOUNTING.toString(), NodeProperty.NT_ACCOUNTING.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
Node accountingNodeParent = node.getNode(NodeProperty.ACCOUNTING.toString());
|
|
|
|
Node accountingNode = accountingNodeParent.addNode(UUID.randomUUID().toString(),AccountingEntryType.UPDATE.getNodeTypeDefinition());
|
|
|
|
|
|
|
|
accountingNode.setProperty(USER, AuthorizationProvider.instance.get().getClient().getId());
|
|
|
|
accountingNode.setProperty(DATE, Calendar.getInstance());
|
|
|
|
accountingNode.setProperty(ITEM_NAME, title);
|
|
|
|
|
|
|
|
try {
|
|
|
|
VersionManager vManager = ses.getWorkspace().getVersionManager();
|
|
|
|
VersionHistory history = vManager.getVersionHistory(node.getNode("jcr:content").getPath());
|
|
|
|
VersionIterator versions = history.getAllVersions();
|
|
|
|
Version version= null;
|
|
|
|
while (versions.hasNext()) {
|
|
|
|
version = versions.nextVersion();
|
|
|
|
}
|
|
|
|
if (version!=null)
|
|
|
|
accountingNode.setProperty(VERSION_ACCOUNTING, version.getName());
|
2019-03-13 18:33:24 +01:00
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
}catch(UnsupportedRepositoryOperationException uropex) {
|
|
|
|
logger.warn("version cannot be retrieved", uropex);
|
2018-06-20 16:59:41 +02:00
|
|
|
}
|
2019-02-27 17:49:55 +01:00
|
|
|
|
|
|
|
|
2018-05-28 12:01:01 +02:00
|
|
|
if (saveHistory) ses.save();
|
|
|
|
} catch (RepositoryException e) {
|
|
|
|
logger.warn("error trying to retrieve accountign node",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void createFolderAddObj(String title, String itemType, String mimeType, Session ses, Node node, boolean saveHistory ) {
|
|
|
|
try {
|
|
|
|
Node directoryNode = node.getParent();
|
|
|
|
|
|
|
|
if (!directoryNode.hasNode(NodeProperty.ACCOUNTING.toString())){
|
|
|
|
directoryNode.addNode(NodeProperty.ACCOUNTING.toString(), NodeProperty.NT_ACCOUNTING.toString());
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|
2018-05-28 12:01:01 +02:00
|
|
|
|
|
|
|
Node accountingNodeParent = directoryNode.getNode(NodeProperty.ACCOUNTING.toString());
|
|
|
|
Node accountingNode = accountingNodeParent.addNode(UUID.randomUUID().toString(),AccountingEntryType.ADD.getNodeTypeDefinition());
|
2018-06-20 16:59:41 +02:00
|
|
|
accountingNode.setProperty(USER, AuthorizationProvider.instance.get().getClient().getId());
|
|
|
|
accountingNode.setProperty(DATE, Calendar.getInstance());
|
|
|
|
accountingNode.setProperty(ITEM_NAME, title);
|
|
|
|
accountingNode.setProperty(ITEM_TYPE, itemType);
|
2018-05-28 12:01:01 +02:00
|
|
|
if (mimeType!=null)
|
2018-06-20 16:59:41 +02:00
|
|
|
accountingNode.setProperty(MIME_TYPE, mimeType);
|
2018-05-28 12:01:01 +02:00
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
if (saveHistory) ses.save();
|
|
|
|
} catch (RepositoryException e) {
|
|
|
|
logger.warn("error trying to retrieve accountign node",e);
|
|
|
|
}
|
|
|
|
}
|
2018-06-29 16:59:24 +02:00
|
|
|
|
|
|
|
public void createFolderRemoveObj(String title, String itemType, String mimeType, Session ses, Node parentNode, boolean saveHistory ) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (!parentNode.hasNode(NodeProperty.ACCOUNTING.toString())){
|
|
|
|
parentNode.addNode(NodeProperty.ACCOUNTING.toString(), NodeProperty.NT_ACCOUNTING.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
Node accountingNodeParent = parentNode.getNode(NodeProperty.ACCOUNTING.toString());
|
|
|
|
Node accountingNode = accountingNodeParent.addNode(UUID.randomUUID().toString(),AccountingEntryType.REMOVAL.getNodeTypeDefinition());
|
|
|
|
accountingNode.setProperty(USER, AuthorizationProvider.instance.get().getClient().getId());
|
|
|
|
accountingNode.setProperty(DATE, Calendar.getInstance());
|
|
|
|
accountingNode.setProperty(ITEM_NAME, title);
|
|
|
|
accountingNode.setProperty(ITEM_TYPE, itemType);
|
|
|
|
if (mimeType!=null)
|
|
|
|
accountingNode.setProperty(MIME_TYPE, mimeType);
|
|
|
|
|
|
|
|
if (saveHistory) ses.save();
|
|
|
|
} catch (RepositoryException e) {
|
|
|
|
logger.warn("error trying to retrieve accountign node",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
public void createShareFolder(String title, Set<String> users, Session ses, Node sharedNode, boolean saveHistory ) {
|
2018-06-29 16:59:24 +02:00
|
|
|
try {
|
|
|
|
|
|
|
|
if (!sharedNode.hasNode(NodeProperty.ACCOUNTING.toString())){
|
|
|
|
sharedNode.addNode(NodeProperty.ACCOUNTING.toString(), NodeProperty.NT_ACCOUNTING.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
Node accountingNodeParent = sharedNode.getNode(NodeProperty.ACCOUNTING.toString());
|
|
|
|
Node accountingNode = accountingNodeParent.addNode(UUID.randomUUID().toString(),AccountingEntryType.SHARE.getNodeTypeDefinition());
|
|
|
|
accountingNode.setProperty(USER, AuthorizationProvider.instance.get().getClient().getId());
|
|
|
|
accountingNode.setProperty(DATE, Calendar.getInstance());
|
|
|
|
accountingNode.setProperty(ITEM_NAME, title);
|
|
|
|
accountingNode.setProperty(MEMBERS, users.toArray(new String[users.size()]));
|
|
|
|
|
|
|
|
if (saveHistory) ses.save();
|
|
|
|
} catch (RepositoryException e) {
|
|
|
|
logger.warn("error trying to retrieve accountign node",e);
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 16:33:23 +02:00
|
|
|
|
|
|
|
public void createUnshareFolder(String title, Session ses, Node sharedNode, boolean saveHistory ) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (!sharedNode.hasNode(NodeProperty.ACCOUNTING.toString())){
|
|
|
|
sharedNode.addNode(NodeProperty.ACCOUNTING.toString(), NodeProperty.NT_ACCOUNTING.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
Node accountingNodeParent = sharedNode.getNode(NodeProperty.ACCOUNTING.toString());
|
|
|
|
Node accountingNode = accountingNodeParent.addNode(UUID.randomUUID().toString(),AccountingEntryType.SHARE.getNodeTypeDefinition());
|
|
|
|
accountingNode.setProperty(USER, AuthorizationProvider.instance.get().getClient().getId());
|
|
|
|
accountingNode.setProperty(DATE, Calendar.getInstance());
|
|
|
|
accountingNode.setProperty(ITEM_NAME, title);
|
|
|
|
|
|
|
|
if (saveHistory) ses.save();
|
|
|
|
} catch (RepositoryException e) {
|
|
|
|
logger.warn("error trying to retrieve accountign node",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void createRename(String oldTitle, String newTitle, Node node, Session ses, boolean saveHistory ) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (!node.hasNode(NodeProperty.ACCOUNTING.toString())){
|
|
|
|
node.addNode(NodeProperty.ACCOUNTING.toString(), NodeProperty.NT_ACCOUNTING.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
Node accountingNodeParent = node.getNode(NodeProperty.ACCOUNTING.toString());
|
|
|
|
Node accountingNode = accountingNodeParent.addNode(UUID.randomUUID().toString(),AccountingEntryType.RENAMING.getNodeTypeDefinition());
|
|
|
|
accountingNode.setProperty(USER, AuthorizationProvider.instance.get().getClient().getId());
|
|
|
|
accountingNode.setProperty(DATE, Calendar.getInstance());
|
|
|
|
accountingNode.setProperty(OLD_ITEM_NAME, oldTitle);
|
|
|
|
accountingNode.setProperty(NEW_ITEM_NAME, newTitle);
|
|
|
|
|
|
|
|
if (saveHistory) ses.save();
|
|
|
|
} catch (RepositoryException e) {
|
|
|
|
logger.warn("error trying to retrieve accountign node",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|