use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.
the class JcrRepositoryFileDao method undeleteFile.
/**
* {@inheritDoc}
*/
@Override
public void undeleteFile(final Serializable fileId, final String versionMessage) {
if (isKioskEnabled()) {
// $NON-NLS-1$
throw new RuntimeException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED"));
}
Assert.notNull(fileId);
jcrTemplate.execute(new JcrCallback() {
@Override
public Object doInJcr(final Session session) throws RepositoryException, IOException {
PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
String absOrigParentFolderPath = deleteHelper.getOriginalParentFolderPath(session, pentahoJcrConstants, fileId);
Serializable origParentFolderId = null;
RepositoryFile file = getFileById(fileId);
RepositoryFileAcl acl = aclDao.getAcl(fileId);
if (!accessVoterManager.hasAccess(file, RepositoryFilePermission.WRITE, acl, PentahoSessionHolder.getSession())) {
return null;
}
// original parent folder path may no longer exist!
if (session.itemExists(JcrStringHelper.pathEncode(absOrigParentFolderPath))) {
origParentFolderId = ((Node) session.getItem(JcrStringHelper.pathEncode(absOrigParentFolderPath))).getIdentifier();
} else {
// go through each of the segments of the original parent folder path, creating as necessary
String[] segments = pathConversionHelper.absToRel(absOrigParentFolderPath).split(RepositoryFile.SEPARATOR);
RepositoryFile lastParentFolder = internalGetFile(session, ServerRepositoryPaths.getTenantRootFolderPath(), false, null);
for (String segment : segments) {
if (StringUtils.hasLength(segment)) {
RepositoryFile tmp = internalGetFile(session, pathConversionHelper.relToAbs((lastParentFolder.getPath().equals(RepositoryFile.SEPARATOR) ? "" : lastParentFolder.getPath()) + RepositoryFile.SEPARATOR + segment), false, // $NON-NLS-1$
null);
if (tmp == null) {
lastParentFolder = internalCreateFolder(session, lastParentFolder.getId(), new RepositoryFile.Builder(segment).folder(true).build(), defaultAclHandler.createDefaultAcl(lastParentFolder), null);
} else {
lastParentFolder = tmp;
}
}
}
origParentFolderId = lastParentFolder.getId();
}
JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, origParentFolderId);
deleteHelper.undeleteFile(session, pentahoJcrConstants, fileId);
session.save();
JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, origParentFolderId, versionMessage);
return null;
}
});
}
use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.
the class JcrRepositoryFileDao method deleteFile.
/**
* {@inheritDoc}
*/
@Override
public void deleteFile(final Serializable fileId, final String versionMessage) {
if (isKioskEnabled()) {
// $NON-NLS-1$
throw new RuntimeException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED"));
}
Assert.notNull(fileId);
jcrTemplate.execute(new JcrCallback() {
@Override
public Object doInJcr(final Session session) throws RepositoryException, IOException {
RepositoryFile fileToBeDeleted = getFileById(fileId);
// Get repository file info and acl info of parent
if (fileToBeDeleted != null) {
RepositoryFileAcl toBeDeletedFileAcl = aclDao.getAcl(fileToBeDeleted.getId());
// Invoke accessVoterManager to see if we have access to perform this operation
if (!accessVoterManager.hasAccess(fileToBeDeleted, RepositoryFilePermission.DELETE, toBeDeletedFileAcl, PentahoSessionHolder.getSession())) {
return null;
}
}
List<RepositoryFilePermission> perms = new ArrayList<RepositoryFilePermission>();
perms.add(RepositoryFilePermission.DELETE);
if (!aclDao.hasAccess(fileToBeDeleted.getPath(), EnumSet.copyOf(perms))) {
throw new AccessDeniedException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED_DELETE", fileId));
}
PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
Serializable parentFolderId = JcrRepositoryFileUtils.getParentId(session, fileId);
JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, parentFolderId);
deleteHelper.deleteFile(session, pentahoJcrConstants, fileId);
session.save();
JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, parentFolderId, versionMessage);
return null;
}
});
}
use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.
the class DumpToFilePentahoSystemListener method startup.
// ~ Instance fields
// =================================================================================================
// ~ Constructors
// ====================================================================================================
// ~ Methods
// =========================================================================================================
@Override
public boolean startup(IPentahoSession pentahoSession) {
Mode tmpMode = null;
if (!StringUtils.hasText(fileName)) {
fileName = System.getProperty(PROP_DUMP_TO_FILE);
tmpMode = Mode.CUSTOM;
if (fileName == null) {
fileName = System.getProperty(PROP_DUMP_TO_FILE_SYSTEM_VIEW);
tmpMode = Mode.SYS;
}
if (fileName == null) {
fileName = System.getProperty(PROP_DUMP_TO_FILE_DOCUMENT_VIEW);
tmpMode = Mode.DOC;
}
} else {
tmpMode = Mode.CUSTOM;
}
final Mode mode = tmpMode;
if (fileName != null) {
// $NON-NLS-1$
final JcrTemplate jcrTemplate = PentahoSystem.get(JcrTemplate.class, "jcrTemplate", pentahoSession);
TransactionTemplate txnTemplate = // $NON-NLS-1$
PentahoSystem.get(TransactionTemplate.class, "jcrTransactionTemplate", pentahoSession);
// $NON-NLS-1$
String repositoryAdminUsername = PentahoSystem.get(String.class, "repositoryAdminUsername", pentahoSession);
// $NON-NLS-1$
final String ZIP_EXTENSION = ".zip";
// let the user know this is a zip
if (!fileName.endsWith(ZIP_EXTENSION)) {
fileName = fileName + ZIP_EXTENSION;
}
// $NON-NLS-1$
logger.debug(String.format("dumping repository to file \"%s\"", fileName));
ZipOutputStream tmpOut = null;
try {
tmpOut = new ZipOutputStream(new BufferedOutputStream(FileUtils.openOutputStream(new File(fileName))));
} catch (IOException e) {
IOUtils.closeQuietly(tmpOut);
throw new RuntimeException(e);
}
final ZipOutputStream out = tmpOut;
// stash existing session
IPentahoSession origPentahoSession = PentahoSessionHolder.getSession();
// run as repo super user
PentahoSessionHolder.setSession(createRepositoryAdminPentahoSession(repositoryAdminUsername));
try {
txnTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(final TransactionStatus status) {
jcrTemplate.execute(new JcrCallback() {
public Object doInJcr(final Session session) throws RepositoryException, IOException {
switch(mode) {
case SYS:
{
final boolean SKIP_BINARY = false;
final boolean NO_RECURSE = false;
// $NON-NLS-1$
out.putNextEntry(new ZipEntry("repository.xml"));
// $NON-NLS-1$
session.exportSystemView("/", out, SKIP_BINARY, NO_RECURSE);
return null;
}
case DOC:
{
final boolean SKIP_BINARY = false;
final boolean NO_RECURSE = false;
// $NON-NLS-1$
out.putNextEntry(new ZipEntry("repository.xml"));
// $NON-NLS-1$
session.exportDocumentView("/", out, SKIP_BINARY, NO_RECURSE);
return null;
}
default:
{
// $NON-NLS-1$
out.putNextEntry(new ZipEntry("repository.txt"));
session.getRootNode().accept(new DumpToFileTraversingItemVisitor(out));
return null;
}
}
}
});
}
});
} finally {
// restore original session
PentahoSessionHolder.setSession(origPentahoSession);
IOUtils.closeQuietly(out);
}
// $NON-NLS-1$
logger.debug(String.format("dumped repository to file \"%s\"", fileName));
}
return true;
}
use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.
the class RepositoryTenantManager method createTenantFolder.
private RepositoryFile createTenantFolder(final ITenant parentTenant, final String tenantName, final String tenantCreatorId) {
return (RepositoryFile) jcrTemplate.execute(new JcrCallback() {
@Override
public Object doInJcr(final Session session) throws RepositoryException {
Tenant tenant = null;
RepositoryFile parentFolder = null;
if (parentTenant == null) {
tenant = new Tenant("/" + tenantName, true);
} else {
tenant = new Tenant(parentTenant.getRootFolderAbsolutePath() + "/" + tenantName, true);
String folderPath = parentTenant.getRootFolderAbsolutePath();
parentFolder = repositoryFileDao.getFileByAbsolutePath(folderPath);
}
RepositoryFileAcl acl = new RepositoryFileAcl.Builder(tenantCreatorId).entriesInheriting(false).build();
RepositoryFile systemTenantFolder = repositoryFileDao.createFolder(parentFolder != null ? parentFolder.getId() : null, new RepositoryFile.Builder(tenant.getName()).folder(true).build(), acl, "");
repositoryFileDao.getFileByAbsolutePath(tenant.getId());
Map<String, Serializable> fileMeta = repositoryFileDao.getFileMetadata(systemTenantFolder.getId());
fileMeta.put(ITenantManager.TENANT_ROOT, true);
fileMeta.put(ITenantManager.TENANT_ENABLED, true);
JcrRepositoryFileUtils.setFileMetadata(session, systemTenantFolder.getId(), fileMeta);
createRuntimeRolesFolderNode(session, new PentahoJcrConstants(session), tenant);
return systemTenantFolder;
}
});
}
use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.
the class AbstractBackingRepositoryLifecycleManager method addMetadataToRepository.
public void addMetadataToRepository(final String metadataProperty) {
txnTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(final TransactionStatus status) {
adminJcrTemplate.execute(new JcrCallback() {
@Override
public Object doInJcr(Session session) throws IOException, RepositoryException {
new PentahoJcrConstants(session);
String absPath = ServerRepositoryPaths.getPentahoRootFolderPath();
RepositoryFile rootFolder = JcrRepositoryFileUtils.getFileByAbsolutePath(session, absPath, pathConversionHelper, null, false, null);
if (rootFolder != null) {
Map<String, Serializable> metadataMap = JcrRepositoryFileUtils.getFileMetadata(session, rootFolder.getId());
if (metadataMap == null) {
metadataMap = new HashMap<String, Serializable>();
}
metadataMap.put(metadataProperty, Boolean.TRUE);
JcrRepositoryFileUtils.setFileMetadata(session, rootFolder.getId(), metadataMap);
} else {
throw new IllegalStateException("Repository has not been initialized properly");
}
session.save();
return null;
}
});
}
});
}
Aggregations