use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-kettle by pentaho.
the class StreamToTransNodeConverter method convert.
public IRepositoryFileData convert(final InputStream inputStream, final String charset, final String mimeType) {
try {
long size = inputStream.available();
TransMeta transMeta = new TransMeta();
Repository repository = connectToRepository();
Document doc = PDIImportUtil.loadXMLFrom(inputStream);
transMeta.loadXML(doc.getDocumentElement(), repository, false);
TransDelegate delegate = new TransDelegate(repository, this.unifiedRepository);
saveSharedObjects(repository, transMeta);
return new NodeRepositoryFileData(delegate.elementToDataNode(transMeta), size);
} catch (Exception e) {
logger.error(e);
return null;
}
}
use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.
the class PDIImportFileHandler method updateFile.
@Override
protected RepositoryFile updateFile(final RepositoryFileImportBundle bundle, final RepositoryFile file, final IRepositoryFileData data) throws PlatformImportException {
RepositoryFile updatedFile = null;
if (isNodeRepositoryFileData(file)) {
updatedFile = getRepository().updateFile(file, data, bundle.getComment());
} else {
String fileName = bundle.getName();
getLogger().trace("The file [" + fileName + "] will be recreated because it content-type was changed.");
RepositoryFileAcl originFileAcl = getRepository().getAcl(file.getId());
getRepository().deleteFile(file.getId(), true, null);
RepositoryFileAcl newFileAcl = bundle.getAcl();
bundle.setAcl(originFileAcl);
updatedFile = createFile(bundle, file.getPath(), data);
bundle.setAcl(newFileAcl);
}
return updatedFile;
}
use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.
the class RepositoryFileImportFileHandler method copyFileToRepository.
/**
* Copies the file bundle into the repository
*
* @param bundle
* @param repositoryPath
* @param file
*/
protected boolean copyFileToRepository(final RepositoryFileImportBundle bundle, final String repositoryPath, final RepositoryFile file) throws PlatformImportException {
// Compute the file extension
final String name = bundle.getName();
final String ext = RepositoryFilenameUtils.getExtension(name);
if (StringUtils.isEmpty(ext)) {
getLogger().debug("Skipping file without extension: " + name);
return false;
}
// Check the mime type
final String mimeType = bundle.getMimeType();
if (mimeType == null) {
getLogger().debug("Skipping file without mime-type: " + name);
return false;
}
// Copy the file into the repository
try {
getLogger().trace("copying file to repository: " + name);
if (getMimeTypeMap().get(mimeType) == null) {
getLogger().debug("Skipping file - mime type of " + mimeType + " is not registered :" + name);
}
Converter converter = getMimeTypeMap().get(mimeType).getConverter();
if (converter == null) {
getLogger().debug("Skipping file without converter: " + name);
return false;
}
RepositoryFile repositoryFile;
IRepositoryFileData data = converter.convert(bundle.getInputStream(), bundle.getCharset(), mimeType);
if (null == file) {
repositoryFile = createFile(bundle, repositoryPath, data);
if (repositoryFile != null) {
updateAclFromBundle(true, bundle, repositoryFile);
}
} else {
repositoryFile = updateFile(bundle, file, data);
updateAclFromBundle(false, bundle, repositoryFile);
}
converter.convertPostRepoSave(repositoryFile);
if (repositoryFile != null) {
getImportSession().addImportedRepositoryFile(repositoryFile);
}
return true;
} catch (IOException e) {
// TODO make sure
getLogger().warn(messages.getString("DefaultImportHandler.WARN_0003_IOEXCEPTION", name), e);
// string exists
return false;
}
}
use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.
the class FileService method doSetMetadata.
/**
* Set the metadata on a file
*
* @param pathId
* @param metadata
* @throws GeneralSecurityException
*/
public void doSetMetadata(String pathId, List<StringKeyStringValueDto> metadata) throws GeneralSecurityException {
RepositoryFileDto file = getRepoWs().getFile(idToPath(pathId));
RepositoryFileAclDto fileAcl = getRepoWs().getAcl(file.getId());
boolean canManage = getSession().getName().equals(fileAcl.getOwner()) || (getPolicy().isAllowed(RepositoryReadAction.NAME) && getPolicy().isAllowed(RepositoryCreateAction.NAME) && getPolicy().isAllowed(AdministerSecurityAction.NAME));
if (!canManage) {
if (fileAcl.isEntriesInheriting()) {
List<RepositoryFileAclAceDto> aces = getRepoWs().getEffectiveAces(file.getId());
fileAcl.setAces(aces, fileAcl.isEntriesInheriting());
}
for (int i = 0; i < fileAcl.getAces().size(); i++) {
RepositoryFileAclAceDto acl = fileAcl.getAces().get(i);
if (acl.getRecipient().equals(getSession().getName())) {
if (acl.getPermissions().contains(RepositoryFilePermission.ACL_MANAGEMENT.ordinal()) || acl.getPermissions().contains(RepositoryFilePermission.ALL.ordinal())) {
canManage = true;
break;
}
}
}
}
if (canManage) {
Map<String, Serializable> fileMetadata = getRepository().getFileMetadata(file.getId());
boolean isHidden = RepositoryFile.HIDDEN_BY_DEFAULT;
boolean isSchedulable = RepositoryFile.SCHEDULABLE_BY_DEFAULT;
fileMetadata.remove(RepositoryFile.HIDDEN_KEY);
for (StringKeyStringValueDto nv : metadata) {
// don't add hidden to the list because it is not actually part of the metadata node
String key = nv.getKey();
if (RepositoryFile.HIDDEN_KEY.equalsIgnoreCase(key)) {
isHidden = BooleanUtils.toBoolean(nv.getValue());
continue;
}
if (RepositoryFile.SCHEDULABLE_KEY.equalsIgnoreCase(key)) {
isSchedulable = BooleanUtils.toBoolean(nv.getValue());
}
fileMetadata.put(key, nv.getValue());
}
// now update the rest of the metadata
if (!file.isFolder()) {
getRepository().setFileMetadata(file.getId(), fileMetadata);
}
// handle hidden flag if it is different
if (file.isHidden() != isHidden) {
file.setHidden(isHidden);
file.setNotSchedulable(!isSchedulable);
/*
* Since we cannot simply set the new value, use the RepositoryFileAdapter to create a new instance and then
* update the original.
*/
RepositoryFile sourceFile = getRepository().getFileById(file.getId());
RepositoryFileDto destFileDto = toFileDto(sourceFile, null, false);
destFileDto.setHidden(isHidden);
destFileDto.setNotSchedulable(!isSchedulable);
RepositoryFile destFile = toFile(destFileDto);
// add the existing acls and file data
RepositoryFileAcl acl = getRepository().getAcl(sourceFile.getId());
if (!file.isFolder()) {
IRepositoryFileData data = RepositoryFileHelper.getFileData(sourceFile);
getRepository().updateFile(destFile, data, null);
getRepository().updateAcl(acl);
} else {
getRepository().updateFolder(destFile, null);
}
}
} else {
throw new GeneralSecurityException();
}
}
use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.
the class CopyFilesOperation method copyRenameMode.
private void copyRenameMode(RepositoryFile repoFile) {
// First try to see if regular name is available
String repoFileName = repoFile.getName();
String copyText = "";
String rootCopyText = "";
String nameNoExtension = repoFileName;
String extension = "";
int indexOfDot = repoFileName.lastIndexOf('.');
if (!(indexOfDot == -1)) {
nameNoExtension = repoFileName.substring(0, indexOfDot);
extension = repoFileName.substring(indexOfDot);
}
RepositoryFileDto testFile = // $NON-NLS-1$
getRepoWs().getFile(path + FileUtils.PATH_SEPARATOR + nameNoExtension + extension);
if (testFile != null) {
// Second try COPY_PREFIX, If the name already ends with a COPY_PREFIX don't append twice
if (!nameNoExtension.endsWith(Messages.getInstance().getString("FileResource.COPY_PREFIX"))) {
// $NON-NLS-1$
copyText = rootCopyText = Messages.getInstance().getString("FileResource.COPY_PREFIX");
repoFileName = nameNoExtension + copyText + extension;
testFile = getRepoWs().getFile(path + FileUtils.PATH_SEPARATOR + repoFileName);
}
}
// Third try COPY_PREFIX + DUPLICATE_INDICATOR
Integer nameCount = 1;
while (testFile != null) {
nameCount++;
copyText = rootCopyText + Messages.getInstance().getString("FileResource.DUPLICATE_INDICATOR", nameCount);
repoFileName = nameNoExtension + copyText + extension;
testFile = getRepoWs().getFile(path + FileUtils.PATH_SEPARATOR + repoFileName);
}
IRepositoryFileData data = RepositoryFileHelper.getFileData(repoFile);
RepositoryFileAcl acl = getRepository().getAcl(repoFile.getId());
RepositoryFile duplicateFile = null;
final RepositoryFile repositoryFile;
if (repoFile.isFolder()) {
// If the title is different than the source file, copy it separately
if (!repoFile.getName().equals(repoFile.getTitle())) {
duplicateFile = new RepositoryFile.Builder(repoFileName).title(RepositoryFile.DEFAULT_LOCALE, repoFile.getTitle() + copyText).hidden(repoFile.isHidden()).versioned(repoFile.isVersioned()).folder(true).build();
} else {
duplicateFile = new RepositoryFile.Builder(repoFileName).hidden(repoFile.isHidden()).folder(true).build();
}
repositoryFile = getRepository().createFolder(destDir.getId(), duplicateFile, acl, null);
performFolderDeepCopy(repoFile, repositoryFile, DEFAULT_DEEPNESS);
} else {
// If the title is different than the source file, copy it separately
if (!repoFile.getName().equals(repoFile.getTitle())) {
duplicateFile = new RepositoryFile.Builder(repoFileName).title(RepositoryFile.DEFAULT_LOCALE, repoFile.getTitle() + copyText).hidden(repoFile.isHidden()).versioned(repoFile.isVersioned()).build();
} else {
duplicateFile = new RepositoryFile.Builder(repoFileName).hidden(repoFile.isHidden()).build();
}
repositoryFile = getRepository().createFile(destDir.getId(), duplicateFile, data, acl, null);
}
if (repositoryFile == null) {
throw new UnifiedRepositoryAccessDeniedException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED_CREATE", destDir.getId()));
}
getRepository().setFileMetadata(repositoryFile.getId(), getRepository().getFileMetadata(repoFile.getId()));
}
Aggregations