[UPDATE]
- adds Manager and Repository for both File and Folder classes in DocRepo - changed class definition of Resource, ResourceManager and ResourceRepository to abstract git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3828 8810af33-2d31-482b-a856-94f89814c4dfpull/2/head
parent
2c72eb379c
commit
75ab72085a
|
|
@ -22,7 +22,6 @@ import com.opencsv.CSVWriter;
|
|||
import org.apache.log4j.Logger;
|
||||
import org.libreccm.security.User;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -41,9 +40,9 @@ import java.util.stream.Collectors;
|
|||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 13/01/2016
|
||||
*/
|
||||
public abstract class ObjectExporter<T> {
|
||||
public abstract class AbstractExporter<T> {
|
||||
|
||||
private static final Logger log = Logger.getLogger(ObjectExporter.class);
|
||||
private static final Logger log = Logger.getLogger(AbstractExporter.class);
|
||||
|
||||
private String filename = null;
|
||||
private char separator = ',';
|
||||
|
|
@ -71,17 +70,15 @@ public abstract class ObjectExporter<T> {
|
|||
/**
|
||||
* Empty constructor.
|
||||
*/
|
||||
public ObjectExporter() {}
|
||||
public AbstractExporter() {}
|
||||
|
||||
/**
|
||||
* Exports a list of objects of type {@code T}, e.g. a list of
|
||||
* {@link User}s, as a .csv-textfile with the specified {@code filename}.
|
||||
*
|
||||
* @param exportObjects List of objects of type {@code T} to be exported
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public void exportToCSV(List<T> exportObjects) throws
|
||||
FileNotFoundException {
|
||||
public void exportToCSV(List<T> exportObjects) {
|
||||
try {
|
||||
CSVWriter csvWriter = new CSVWriter(new FileWriter(filename),
|
||||
separator);
|
||||
|
|
@ -93,9 +90,6 @@ public abstract class ObjectExporter<T> {
|
|||
} catch (IOException e) {
|
||||
log.error(String.format("A FileWriter with the name %s has not " +
|
||||
"been able to be created.", filename));
|
||||
|
||||
// Todo: throw Exception to modify in ui
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -21,7 +21,6 @@ package org.libreccm.portation.importer;
|
|||
import com.opencsv.CSVReader;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
|
@ -38,9 +37,9 @@ import java.util.stream.Collectors;
|
|||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 07/01/2016
|
||||
*/
|
||||
public abstract class ObjectImporter<T> {
|
||||
public abstract class AbstractImporter<T> {
|
||||
|
||||
private static final Logger log = Logger.getLogger(ObjectImporter.class);
|
||||
private static final Logger log = Logger.getLogger(AbstractImporter.class);
|
||||
|
||||
private String filename = null;
|
||||
private char separator = ',';
|
||||
|
|
@ -68,15 +67,16 @@ public abstract class ObjectImporter<T> {
|
|||
/**
|
||||
* Empty constructor.
|
||||
*/
|
||||
public ObjectImporter() {}
|
||||
public AbstractImporter() {}
|
||||
|
||||
/**
|
||||
* Imports object information as {@link String} from a .csv-textfile as new
|
||||
* objects of type {@code T} into the database.
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
* @return A list of objects of type {@code T} having been imported from a
|
||||
* .csv-textfile.
|
||||
*/
|
||||
public List<T> importFromCSV() throws FileNotFoundException {
|
||||
public List<T> importFromCSV() {
|
||||
try {
|
||||
CSVReader csvReader = new CSVReader(new FileReader(filename),
|
||||
separator);
|
||||
|
|
@ -96,9 +96,7 @@ public abstract class ObjectImporter<T> {
|
|||
log.error(String.format("Either a FileReader with the name %s has " +
|
||||
"not been able to be created or the file could not be " +
|
||||
"read.", filename));
|
||||
|
||||
// Todo: throw Exception to modify in ui
|
||||
throw new FileNotFoundException();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -20,6 +20,8 @@ package org.libreccm.docrepo;
|
|||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.NamedQueries;
|
||||
import javax.persistence.NamedQuery;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
|
|
@ -30,8 +32,22 @@ import javax.persistence.Table;
|
|||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 01/10/2015
|
||||
*/
|
||||
@Entity
|
||||
@Entity(name = "DocRepoFile")
|
||||
@Table(schema = "CCM_DOCREPO", name = "FILES")
|
||||
@NamedQueries({
|
||||
@NamedQuery(name = "DocRepo.findFileByName",
|
||||
query = "SELECT r FROM DocRepoFile r WHERE " +
|
||||
"r.name = :name"),
|
||||
@NamedQuery(name = "DocRepo.findFileByPath",
|
||||
query = "SELECT r FROM DocRepoFile r WHERE " +
|
||||
"r.path = :pathName"),
|
||||
@NamedQuery(name = "DocRepo.findCreatedFileFromUser",
|
||||
query = "SELECT r FROM DocRepoFile r WHERE " +
|
||||
"r.creationUser = :user"),
|
||||
@NamedQuery(name = "DocRepo.findModifiedFileFromUser",
|
||||
query = "SELECT r FROM DocRepoFile r WHERE " +
|
||||
"r.lastModifiedUser = :user")
|
||||
})
|
||||
public class File extends Resource {
|
||||
|
||||
private static final long serialVersionUID = -504220783419811504L;
|
||||
|
|
@ -61,4 +77,28 @@ public class File extends Resource {
|
|||
}
|
||||
|
||||
//< End GETTER & SETTER
|
||||
|
||||
/**
|
||||
* Returns the attribute names of this object class as a list of strings.
|
||||
*
|
||||
* @return List of strings with the attribute names of this class
|
||||
*/
|
||||
public static String[] getAttributeNames() {
|
||||
return new String[] {
|
||||
"name",
|
||||
"description",
|
||||
"path",
|
||||
"mimeType",
|
||||
"size",
|
||||
"blobObject_ID",
|
||||
"creationDate",
|
||||
"lastModifiedDate",
|
||||
"creationIp",
|
||||
"lastModifiedIp",
|
||||
"creator_ID",
|
||||
"modifier_ID",
|
||||
"parent_ID",
|
||||
"repo_ID"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright (C) 2015 LibreCCM Foundation.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*/
|
||||
package org.libreccm.docrepo;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Manager class for complex operations on {@code File}-objects.
|
||||
*
|
||||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 27.01.2016
|
||||
*/
|
||||
@RequestScoped
|
||||
public class FileManager extends ResourceManager<File> {
|
||||
|
||||
@Inject
|
||||
private FileRepository fileRepository;
|
||||
|
||||
@Override
|
||||
public void copy(File original, File copy) {
|
||||
super.copy(original, copy);
|
||||
copy.setContent(original.getContent());
|
||||
|
||||
fileRepository.save(copy);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (C) 2015 LibreCCM Foundation.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*/
|
||||
package org.libreccm.docrepo;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
/**
|
||||
* Repository class for retrieving, storing and deleting {@code File}s.
|
||||
*
|
||||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 27.01.2016
|
||||
*/
|
||||
@RequestScoped
|
||||
public class FileRepository extends ResourceRepository<File> {
|
||||
|
||||
public FileRepository() {
|
||||
classOfT = File.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedQuery<File> getFindByNameQuery() {
|
||||
return entityManager.createNamedQuery(
|
||||
"DocRepo.findFileByName", File.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedQuery<File> getFindByPathNameQuery() {
|
||||
return entityManager.createNamedQuery(
|
||||
"DocRepo.findFileByPath", File.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedQuery<File> getFindForCreatorQuery() {
|
||||
return entityManager.createNamedQuery(
|
||||
"DocRepo.findCreatedFileFromUser", File.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedQuery<File> getFindForModifierQuery() {
|
||||
return entityManager.createNamedQuery(
|
||||
"DocRepo.findModifiedFileFromUser", File.class);
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,8 @@
|
|||
package org.libreccm.docrepo;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.NamedQueries;
|
||||
import javax.persistence.NamedQuery;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
|
@ -31,8 +33,22 @@ import java.util.List;
|
|||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 01/10/2015
|
||||
*/
|
||||
@Entity
|
||||
@Entity(name = "DocRepoFolder")
|
||||
@Table(schema = "CCM_DOCREPO", name = "FOLDERS")
|
||||
@NamedQueries({
|
||||
@NamedQuery(name = "DocRepo.findFolderByName",
|
||||
query = "SELECT r FROM DocRepoFolder r WHERE " +
|
||||
"r.name = :name"),
|
||||
@NamedQuery(name = "DocRepo.findFolderByPath",
|
||||
query = "SELECT r FROM DocRepoFolder r WHERE " +
|
||||
"r.path = :pathName"),
|
||||
@NamedQuery(name = "DocRepo.findCreatedFolderFromUser",
|
||||
query = "SELECT r FROM DocRepoFolder r WHERE " +
|
||||
"r.creationUser = :user"),
|
||||
@NamedQuery(name = "DocRepo.findModifiedFolderFromUser",
|
||||
query = "SELECT r FROM DocRepoFolder r WHERE " +
|
||||
"r.lastModifiedUser = :user")
|
||||
})
|
||||
public class Folder extends Resource {
|
||||
|
||||
private static final long serialVersionUID = 1561466556458872622L;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (C) 2015 LibreCCM Foundation.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*/
|
||||
package org.libreccm.docrepo;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Manager class for complex operations on {@code Folder}-objects.
|
||||
*
|
||||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 27.01.2016
|
||||
*/
|
||||
@RequestScoped
|
||||
public class FolderManager extends ResourceManager<Folder> {
|
||||
|
||||
@Inject
|
||||
private FolderRepository folderRepository;
|
||||
|
||||
@Override
|
||||
public void copy(Folder original, Folder copy) {
|
||||
super.copy(original, copy);
|
||||
copy.setImmediateChildren(original.getImmediateChildren());
|
||||
copy.setRootAssignedRepository(original.getRootAssignedRepository());
|
||||
|
||||
folderRepository.save(copy);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (C) 2015 LibreCCM Foundation.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*/
|
||||
package org.libreccm.docrepo;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
/**
|
||||
* Repository class for retrieving, storing and deleting {@code File}s.
|
||||
*
|
||||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 27.01.2016
|
||||
*/
|
||||
@RequestScoped
|
||||
public class FolderRepository extends ResourceRepository<Folder> {
|
||||
|
||||
public FolderRepository() {
|
||||
classOfT = Folder.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedQuery<Folder> getFindByNameQuery() {
|
||||
return entityManager.createNamedQuery(
|
||||
"DocRepo.findFolderByName", Folder.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedQuery<Folder> getFindByPathNameQuery() {
|
||||
return entityManager.createNamedQuery(
|
||||
"DocRepo.findFolderByPath", Folder.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedQuery<Folder> getFindForCreatorQuery() {
|
||||
return entityManager.createNamedQuery(
|
||||
"DocRepo.findCreatedFolderFromUser", Folder.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedQuery<Folder> getFindForModifierQuery() {
|
||||
return entityManager.createNamedQuery(
|
||||
"DocRepo.findModifiedFolderFromUser", Folder.class);
|
||||
}
|
||||
}
|
||||
|
|
@ -27,8 +27,6 @@ import javax.persistence.Column;
|
|||
import javax.persistence.Entity;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.NamedQueries;
|
||||
import javax.persistence.NamedQuery;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
|
|
@ -45,21 +43,8 @@ import java.util.Date;
|
|||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 01/10/2015
|
||||
*/
|
||||
@Entity(name = "DocRepoResource")
|
||||
@Entity
|
||||
@Table(schema = "CCM_DOCREPO", name = "RESOURCES")
|
||||
@NamedQueries({
|
||||
@NamedQuery(name = "DocRepo.findResourceByPath",
|
||||
query = "SELECT r FROM DocRepoResource r WHERE " +
|
||||
"r.path = :pathName"),
|
||||
@NamedQuery(name = "DocRepo.findResourcesByName",
|
||||
query = "SELECT r FROM DocRepoResource r WHERE " +
|
||||
"r.name = :name"),
|
||||
@NamedQuery(name = "DocRepo.findCreatedResourcesFromUser",
|
||||
query = "SELECT r FROM DocRepoResource r WHERE " +
|
||||
"r.creationUser = :user"),
|
||||
@NamedQuery(name = "DocRepo.findModifiedResourcesFromUser",
|
||||
query = "SELECT r FROM DocRepoResource r WHERE " +
|
||||
"r.lastModifiedUser = :user")})
|
||||
public abstract class Resource extends CcmObject {
|
||||
|
||||
private static final long serialVersionUID = -910317798106611214L;
|
||||
|
|
@ -249,7 +234,7 @@ public abstract class Resource extends CcmObject {
|
|||
this.lastModifiedUser = lastModifiedUser;
|
||||
}
|
||||
|
||||
public Resource getParent() {
|
||||
public Folder getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,11 +18,9 @@
|
|||
*/
|
||||
package org.libreccm.docrepo;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.oro.text.perl.Perl5Util;
|
||||
|
||||
import javax.activation.MimetypesFileTypeMap;
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
|
|
@ -31,70 +29,56 @@ import javax.inject.Inject;
|
|||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 01/10/2015
|
||||
*/
|
||||
@RequestScoped
|
||||
public class ResourceManager {
|
||||
private static final Logger log = Logger.getLogger(ResourceManager.class);
|
||||
public abstract class ResourceManager<T extends Resource> {
|
||||
|
||||
@Inject
|
||||
private ResourceRepository resourceRepository;
|
||||
private FileRepository fileRepository;
|
||||
|
||||
@Inject
|
||||
private FolderRepository folderRepository;
|
||||
|
||||
/**
|
||||
* Copies a given {@link File} to a given {@link Folder}.
|
||||
* Makes a copy of a given {@link Resource}.
|
||||
*
|
||||
* @param original The {@link Resource} to be copied
|
||||
* @param folder The {@link Folder} to copy to
|
||||
* @param copy The {@code copy} of the given {@code original}
|
||||
* {@link Resource}
|
||||
*/
|
||||
public void copyToFolder(Resource original, Folder folder) {
|
||||
Resource copy = original instanceof File ?
|
||||
copyFileSpecifics(new File(), (File) original) :
|
||||
copyFolderSpecifics(new Folder(), (Folder) original);
|
||||
|
||||
public void copy(T original, T copy) {
|
||||
copy.setName(original.getName());
|
||||
copy.setDescription(original.getDescription());
|
||||
copy.setPath(String.format("%s/%s", folder.getPath(), copy.getName()));
|
||||
copy.setPath(String.format("%s/%s", original.getPath(), copy.getName()));
|
||||
copy.setMimeType(original.getMimeType());
|
||||
copy.setSize(original.getSize());
|
||||
// for file: setContent
|
||||
copy.setCreationDate(original.getCreationDate());
|
||||
copy.setLastModifiedDate(original.getLastModifiedDate());
|
||||
copy.setCreationIp(original.getCreationIp());
|
||||
copy.setLastModifiedIp(original.getLastModifiedIp());
|
||||
copy.setCreationUser(original.getCreationUser());
|
||||
copy.setLastModifiedUser(original.getLastModifiedUser());
|
||||
copy.setParent(folder);
|
||||
copy.setRepository(folder.getRepository());
|
||||
|
||||
resourceRepository.save(copy);
|
||||
if (copy.getParent() == null)
|
||||
copy.setParent(original.getParent());
|
||||
copy.setRepository(original.getRepository());
|
||||
// for folder: setImmediateChildren
|
||||
// for folder: setRootAssignedRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to copy the {@link File} specific data to the copy.
|
||||
* Overloads previous method and sets a new parent (aka location) before
|
||||
* calling the method to copy the {@code original} {@link Resource} into
|
||||
* the {@code copy}.
|
||||
*
|
||||
* @param copy The copied {@link File}
|
||||
* @param original The originl {@link File}
|
||||
* @return A {@link Resource} with the file-specific data from the
|
||||
* original {@link File}
|
||||
* @param original The {@link Resource} to be copied
|
||||
* @param copy The {@code copy} of the given {@code original}
|
||||
* {@link Resource}
|
||||
* @param newParent The new parent of the copy. Equals an new location
|
||||
*/
|
||||
private Resource copyFileSpecifics(File copy, File original) {
|
||||
copy.setContent(original.getContent());
|
||||
return copy;
|
||||
public void copy(T original, T copy, Folder newParent) {
|
||||
copy.setParent(newParent);
|
||||
copy(original, copy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to copy the {@link Folder} specific data to the copy.
|
||||
*
|
||||
* @param copy The copied {@link Folder}
|
||||
* @param original The originl {@link Folder}
|
||||
* @return A {@link Resource} with the folder-specific data from the
|
||||
* original {@link Folder}
|
||||
*/
|
||||
private Resource copyFolderSpecifics(Folder copy, Folder original) {
|
||||
copy.setImmediateChildren(original.getImmediateChildren());
|
||||
copy.setRootAssignedRepository(original.getRootAssignedRepository());
|
||||
return copy;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Determines weather the given name is a valid new name for the also
|
||||
* given {@link Resource}.
|
||||
|
|
@ -113,7 +97,7 @@ public class ResourceManager {
|
|||
*
|
||||
* @return true for a system-valid resource name, otherwise false
|
||||
*/
|
||||
public boolean isValidNewResourceName(String name, Resource resource) {
|
||||
public boolean isValidNewResourceName(String name, T resource) {
|
||||
Perl5Util perl5Util = new Perl5Util();
|
||||
|
||||
final String INVALID_START_PATTERN = "/^[.]+/";
|
||||
|
|
@ -132,7 +116,8 @@ public class ResourceManager {
|
|||
perl5Util.match(INVALID_NAME_PATTERN, name));
|
||||
|
||||
// checks duplication of the name; database access (mind performance)
|
||||
validName &= resourceRepository.findByName(name).isEmpty();
|
||||
validName &= (fileRepository.findByName(name).isEmpty() &
|
||||
folderRepository.findByName(name).isEmpty());
|
||||
|
||||
// checks that the name corresponds to a compatible MIME type
|
||||
validName &= new MimetypesFileTypeMap().getContentType(name).equals
|
||||
|
|
|
|||
|
|
@ -24,11 +24,10 @@ import org.libreccm.cdi.utils.CdiUtil;
|
|||
import org.libreccm.security.PermissionChecker;
|
||||
import org.libreccm.security.User;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.TypedQuery;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
|
@ -38,25 +37,26 @@ import java.util.stream.Collectors;
|
|||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 01/10/2015
|
||||
*/
|
||||
@RequestScoped
|
||||
public class ResourceRepository extends
|
||||
AbstractAuditedEntityRepository<Long, Resource> {
|
||||
public abstract class ResourceRepository<T extends Resource> extends
|
||||
AbstractAuditedEntityRepository<Long, T> {
|
||||
|
||||
protected Class classOfT;
|
||||
|
||||
@Inject
|
||||
private EntityManager entityManager;
|
||||
protected EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public Long getEntityId(Resource entity) {
|
||||
public Long getEntityId(T entity) {
|
||||
return entity.getObjectId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Resource> getEntityClass() {
|
||||
return Resource.class;
|
||||
public Class<T> getEntityClass() {
|
||||
return classOfT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNew(Resource entity) {
|
||||
public boolean isNew(T entity) {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity to save can't be null.");
|
||||
}
|
||||
|
|
@ -71,7 +71,7 @@ public class ResourceRepository extends
|
|||
* @param resources The requested {@link Resource}s, found in the database
|
||||
* @return A list of {@link Resource}s the subject is allowed to access
|
||||
*/
|
||||
private List<Resource> permissionFilter(List<Resource> resources) {
|
||||
private List<T> permissionFilter(List<T> resources) {
|
||||
final CdiUtil cdiUtil = new CdiUtil();
|
||||
final PermissionChecker permissionChecker = cdiUtil.findBean(
|
||||
PermissionChecker.class);
|
||||
|
|
@ -88,56 +88,8 @@ public class ResourceRepository extends
|
|||
* @return A list of at most one {@link Resource} the subject is allowed to
|
||||
* access
|
||||
*/
|
||||
private Resource permissionFilter(Resource resource) {
|
||||
return permissionFilter(Arrays.asList(resource)).get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a {@code Resource} by its {@code path}.
|
||||
*
|
||||
* @param pathName The {@code path} to the {@code Resource}.
|
||||
*
|
||||
* @return The {@code Resource} identified by the given {@code path}, if there is
|
||||
* such a {@code Resource}, {@code null} if not.
|
||||
*/
|
||||
public Resource findByPathName(final String pathName) {
|
||||
final TypedQuery<Resource> query = entityManager.createNamedQuery(
|
||||
"DocRepo.findResourceByPath", Resource.class);
|
||||
query.setParameter("pathName", pathName);
|
||||
|
||||
return permissionFilter(query.getSingleResult());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the {@code Resource}s, a given {@link User} created.
|
||||
*
|
||||
* @param creator The {@link User}, who created the {@code Resource}s.
|
||||
*
|
||||
* @return The {@code Resource}s, created by the given {@link User}, if there
|
||||
* are such {@code Resource}s, {@code EmptyList} if not.
|
||||
*/
|
||||
public List<Resource> findForCreator(final User creator) {
|
||||
final TypedQuery<Resource> query = entityManager.createNamedQuery(
|
||||
"DocRepo.findCreatedResourcesFromUser", Resource.class);
|
||||
query.setParameter("user", creator);
|
||||
|
||||
return permissionFilter(query.getResultList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the {@code Resource}s, a given {@link User} last modified.
|
||||
*
|
||||
* @param modifier The {@link User}, who last modified the {@code Resource}s.
|
||||
*
|
||||
* @return The {@code Resource}s, last modified by the given {@link User}, if
|
||||
* there are such {@code Resource}s, {@code EmptyList} if not.
|
||||
*/
|
||||
public List<Resource> findForModifier(final User modifier) {
|
||||
final TypedQuery<Resource> query = entityManager.createNamedQuery(
|
||||
"DocRepo.findModifiedResourcesFromUser", Resource.class);
|
||||
query.setParameter("user", modifier);
|
||||
|
||||
return permissionFilter(query.getResultList());
|
||||
private T permissionFilter(T resource) {
|
||||
return permissionFilter(Collections.singletonList(resource)).get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -147,11 +99,91 @@ public class ResourceRepository extends
|
|||
* @return The {@link Resource}s with the given name, if there aren't
|
||||
* any an {@code EmptyList}
|
||||
*/
|
||||
public List<Resource> findByName(final String name) {
|
||||
final TypedQuery<Resource> query = entityManager.createNamedQuery(
|
||||
"DocRepo.findResourcesByName", Resource.class);
|
||||
public List<T> findByName(final String name) {
|
||||
final TypedQuery<T> query = getFindByNameQuery();
|
||||
query.setParameter("name", name);
|
||||
|
||||
return permissionFilter(query.getResultList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method to get a {@link TypedQuery}, specifically implemented
|
||||
* in the subclasses matching their own database requests, finding the
|
||||
* {@code T}-typed objects by name.
|
||||
*
|
||||
* @return A {@link TypedQuery} to find objects by name
|
||||
*/
|
||||
protected abstract TypedQuery<T> getFindByNameQuery();
|
||||
|
||||
/**
|
||||
* Retrieve a {@code Resource} by its {@code path}.
|
||||
*
|
||||
* @param pathName The {@code path} to the {@code Resource}.
|
||||
*
|
||||
* @return The {@code Resource} identified by the given {@code path}, if
|
||||
* there is such a {@code Resource}, {@code null} if not.
|
||||
*/
|
||||
public T findByPathName(final String pathName) {
|
||||
final TypedQuery<T> query = getFindByPathNameQuery();
|
||||
query.setParameter("pathName", pathName);
|
||||
|
||||
return permissionFilter(query.getSingleResult());
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method to get a {@link TypedQuery}, specifically implemented
|
||||
* in the subclasses matching their own database requests, finding the
|
||||
* {@code T}-typed objects by path name.
|
||||
*
|
||||
* @return A {@link TypedQuery} to find objects by path name
|
||||
*/
|
||||
protected abstract TypedQuery<T> getFindByPathNameQuery();
|
||||
|
||||
/**
|
||||
* Retrieve the {@code Resource}s, a given {@link User} created.
|
||||
*
|
||||
* @param creator The {@link User}, who created the {@code Resource}s.
|
||||
*
|
||||
* @return The {@code Resource}s, created by the given {@link User}, if
|
||||
* there are such {@code Resource}s, {@code EmptyList} if not.
|
||||
*/
|
||||
public List<T> findForCreator(final User creator) {
|
||||
final TypedQuery<T> query = getFindForCreatorQuery();
|
||||
query.setParameter("user", creator);
|
||||
|
||||
return permissionFilter(query.getResultList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method to get a {@link TypedQuery}, specifically implemented
|
||||
* in the subclasses matching their own database requests, finding the
|
||||
* {@code T}-typed objects created by a given/set {@link User}.
|
||||
*
|
||||
* @return A {@link TypedQuery} to find objects for creator.
|
||||
*/
|
||||
protected abstract TypedQuery<T> getFindForCreatorQuery();
|
||||
|
||||
/**
|
||||
* Retrieve the {@code Resource}s, a given {@link User} last modified.
|
||||
*
|
||||
* @param modifier The {@link User} who last modified the {@code Resource}s.
|
||||
*
|
||||
* @return The {@code Resource}s, last modified by the given {@link User},
|
||||
* if there are such {@code Resource}s, {@code EmptyList} if not.
|
||||
*/
|
||||
public List<T> findForModifier(final User modifier) {
|
||||
final TypedQuery<T> query = getFindForModifierQuery();
|
||||
query.setParameter("user", modifier);
|
||||
|
||||
return permissionFilter(query.getResultList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method to get a {@link TypedQuery}, specifically implemented
|
||||
* in the subclasses matching their own database requests, finding the
|
||||
* {@code T}-typed objects last modified for a given/set {@link User}.
|
||||
*
|
||||
* @return A {@link TypedQuery} to find objects for last modifier.
|
||||
*/
|
||||
protected abstract TypedQuery<T> getFindForModifierQuery();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,8 +19,9 @@
|
|||
package org.libreccm.docrepo.portation.exporter;
|
||||
|
||||
import org.libreccm.docrepo.File;
|
||||
import org.libreccm.portation.exporter.ObjectExporter;
|
||||
import org.libreccm.portation.exporter.AbstractExporter;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -31,7 +32,9 @@ import java.util.List;
|
|||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 13/01/2016
|
||||
*/
|
||||
public class FileExporter extends ObjectExporter<File> {
|
||||
@RequestScoped
|
||||
public class FileExporter extends AbstractExporter<File> {
|
||||
|
||||
@Override
|
||||
protected String[] getClassName() {
|
||||
return new String[] {File.class.getName()};
|
||||
|
|
@ -39,22 +42,7 @@ public class FileExporter extends ObjectExporter<File> {
|
|||
|
||||
@Override
|
||||
protected String[] getAttributeNames() {
|
||||
return new String[] {
|
||||
"name",
|
||||
"description",
|
||||
"path",
|
||||
"mimeType",
|
||||
"size",
|
||||
"blobObject_ID",
|
||||
"creationDate",
|
||||
"lastModifiedDate",
|
||||
"creationIp",
|
||||
"lastModifiedIp",
|
||||
"creator_ID",
|
||||
"modifier_ID",
|
||||
"parent_ID",
|
||||
"repo_ID"
|
||||
};
|
||||
return File.getAttributeNames();
|
||||
}
|
||||
|
||||
// Todo: change ID to UUID
|
||||
|
|
@ -66,7 +54,7 @@ public class FileExporter extends ObjectExporter<File> {
|
|||
list.add(exportObject.getDescription());
|
||||
list.add(exportObject.getPath());
|
||||
list.add(exportObject.getMimeType() != null ?
|
||||
exportObject.getMimeType().toString() : "");
|
||||
exportObject.getMimeType().getPrimaryType() : "");
|
||||
list.add(String.valueOf(exportObject.getSize()));
|
||||
list.add(exportObject.getContent() != null ? String.valueOf(
|
||||
exportObject.getContent().getBlobObjectId()) : "");
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@
|
|||
*/
|
||||
package org.libreccm.docrepo.portation.importer;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.libreccm.cdi.utils.CdiUtil;
|
||||
import org.libreccm.docrepo.BlobObject;
|
||||
import org.libreccm.docrepo.BlobObjectRepository;
|
||||
|
|
@ -27,12 +26,13 @@ import org.libreccm.docrepo.Folder;
|
|||
import org.libreccm.docrepo.Repository;
|
||||
import org.libreccm.docrepo.RepositoryRepository;
|
||||
import org.libreccm.docrepo.ResourceRepository;
|
||||
import org.libreccm.portation.importer.ObjectImporter;
|
||||
import org.libreccm.portation.importer.AbstractImporter;
|
||||
import org.libreccm.security.User;
|
||||
import org.libreccm.security.UserRepository;
|
||||
|
||||
import javax.activation.MimeType;
|
||||
import javax.activation.MimeTypeParseException;
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
|
|
@ -42,28 +42,12 @@ import java.util.Date;
|
|||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 20/01/2016
|
||||
*/
|
||||
public class FileImporter extends ObjectImporter<File> {
|
||||
|
||||
private static final Logger log = Logger.getLogger(FileImporter.class);
|
||||
@RequestScoped
|
||||
public class FileImporter extends AbstractImporter<File> {
|
||||
|
||||
@Override
|
||||
protected boolean checkAttributeNames(String[] attributeNames) {
|
||||
return attributeNames.equals(new String[] {
|
||||
"name",
|
||||
"description",
|
||||
"path",
|
||||
"mimeType",
|
||||
"size",
|
||||
"blobObject_ID",
|
||||
"creationDate",
|
||||
"lastModifiedDate",
|
||||
"creationIp",
|
||||
"lastModifiedIp",
|
||||
"creator_ID",
|
||||
"modifier_ID",
|
||||
"parent_ID",
|
||||
"repo_ID"
|
||||
});
|
||||
return attributeNames.equals(File.getAttributeNames());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -78,9 +62,7 @@ public class FileImporter extends ObjectImporter<File> {
|
|||
MimeType mimeType = new MimeType();
|
||||
try {
|
||||
mimeType.setPrimaryType(importStrings[3]);
|
||||
} catch (MimeTypeParseException e) {
|
||||
log.warn(String.format("Unable to cast %s to a MimeType.",
|
||||
importStrings[3]));
|
||||
} catch (MimeTypeParseException ignored) {
|
||||
}
|
||||
file.setMimeType(mimeType);
|
||||
|
||||
|
|
@ -132,7 +114,7 @@ public class FileImporter extends ObjectImporter<File> {
|
|||
file.setRepository(repository);
|
||||
}
|
||||
|
||||
//resourceRepository.save(file);
|
||||
resourceRepository.save(file);
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,9 +5,6 @@
|
|||
*/
|
||||
package org.libreccm.docrepo.portation;
|
||||
|
||||
import java.io.File;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.EntityManager;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.arquillian.junit.InSequence;
|
||||
|
|
@ -26,20 +23,20 @@ import org.junit.BeforeClass;
|
|||
import org.junit.Test;
|
||||
import org.junit.experimental.categories.Category;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.libreccm.categorization.Categorization;
|
||||
import org.libreccm.core.CcmObject;
|
||||
import org.libreccm.core.CcmObjectRepository;
|
||||
import org.libreccm.jpa.EntityManagerProducer;
|
||||
import org.libreccm.jpa.utils.MimeTypeConverter;
|
||||
import org.libreccm.l10n.LocalizedString;
|
||||
import org.libreccm.security.Permission;
|
||||
import org.libreccm.tests.categories.IntegrationTest;
|
||||
import org.libreccm.testutils.EqualsVerifier;
|
||||
import org.libreccm.web.CcmApplication;
|
||||
import org.libreccm.workflow.Workflow;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.EntityManager;
|
||||
import java.io.File;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -115,6 +112,7 @@ public class ArquillianExampleTest {
|
|||
//
|
||||
// @Test
|
||||
// public void hello() {}
|
||||
|
||||
@Test
|
||||
@InSequence(1)
|
||||
public void entityManagerIsInjected() {
|
||||
|
|
|
|||
|
|
@ -19,16 +19,24 @@
|
|||
package org.libreccm.docrepo.portation;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.arquillian.persistence.CreateSchema;
|
||||
import org.jboss.arquillian.transaction.api.annotation.TransactionMode;
|
||||
import org.jboss.arquillian.transaction.api.annotation.Transactional;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.experimental.categories.Category;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.libreccm.core.CcmObjectRepository;
|
||||
import org.libreccm.docrepo.File;
|
||||
import org.libreccm.docrepo.ResourceRepository;
|
||||
import org.libreccm.docrepo.portation.exporter.FileExporter;
|
||||
import org.libreccm.docrepo.portation.importer.FileImporter;
|
||||
import org.libreccm.tests.categories.IntegrationTest;
|
||||
import org.libreccm.tests.categories.UnitTest;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Arrays;
|
||||
import javax.inject.Inject;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -37,13 +45,26 @@ import java.util.List;
|
|||
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
|
||||
* @version 13/01/2016
|
||||
*/
|
||||
@Category(UnitTest.class)
|
||||
@Category(IntegrationTest.class)
|
||||
@RunWith(Arquillian.class)
|
||||
@Transactional(TransactionMode.COMMIT)
|
||||
@CreateSchema({"create_ccm_docrepo_schema.sql"})
|
||||
public class FilePortationTest {
|
||||
|
||||
private static final Logger log = Logger.getLogger(FilePortationTest.class);
|
||||
|
||||
@Inject
|
||||
private FileExporter fileExporter;
|
||||
|
||||
@Inject
|
||||
private FileImporter fileImporter;
|
||||
|
||||
@Inject
|
||||
private ResourceRepository ccmObjectRepository;
|
||||
|
||||
private static File file;
|
||||
private static String filename =
|
||||
"src/test/java/org/libreccm/docrepo/portation/exportTest.csv";
|
||||
"src/test/java/org/libreccm/docrepo/portation/csv/exportTest.csv";
|
||||
|
||||
@BeforeClass
|
||||
public static void createResource() {
|
||||
|
|
@ -57,25 +78,14 @@ public class FilePortationTest {
|
|||
|
||||
@Test
|
||||
public void csvShouldBeCreated() {
|
||||
FileExporter fileExporter = new FileExporter();
|
||||
fileExporter.setFilename(filename);
|
||||
try {
|
||||
fileExporter.exportToCSV(Arrays.asList(file));
|
||||
} catch (FileNotFoundException e) {
|
||||
log.error("Error exporting files.");
|
||||
}
|
||||
fileExporter.exportToCSV(Collections.singletonList(file));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fileShouldBeCreated() {
|
||||
FileImporter fileImporter = new FileImporter();
|
||||
fileImporter.setFilename(filename);
|
||||
List<File> files;
|
||||
try {
|
||||
files = fileImporter.importFromCSV();
|
||||
List<File> files = fileImporter.importFromCSV();
|
||||
log.info(files.toString());
|
||||
} catch (FileNotFoundException e) {
|
||||
log.error("Error exporting files.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue