- 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-94f89814c4df
pull/2/head
tosmers 2016-01-27 15:54:52 +00:00
parent 2c72eb379c
commit 75ab72085a
15 changed files with 453 additions and 218 deletions

View File

@ -22,7 +22,6 @@ import com.opencsv.CSVWriter;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.libreccm.security.User; import org.libreccm.security.User;
import java.io.FileNotFoundException;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -41,9 +40,9 @@ import java.util.stream.Collectors;
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a> * @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version 13/01/2016 * @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 String filename = null;
private char separator = ','; private char separator = ',';
@ -71,17 +70,15 @@ public abstract class ObjectExporter<T> {
/** /**
* Empty constructor. * Empty constructor.
*/ */
public ObjectExporter() {} public AbstractExporter() {}
/** /**
* Exports a list of objects of type {@code T}, e.g. a list of * 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}. * {@link User}s, as a .csv-textfile with the specified {@code filename}.
* *
* @param exportObjects List of objects of type {@code T} to be exported * @param exportObjects List of objects of type {@code T} to be exported
* @throws FileNotFoundException
*/ */
public void exportToCSV(List<T> exportObjects) throws public void exportToCSV(List<T> exportObjects) {
FileNotFoundException {
try { try {
CSVWriter csvWriter = new CSVWriter(new FileWriter(filename), CSVWriter csvWriter = new CSVWriter(new FileWriter(filename),
separator); separator);
@ -93,9 +90,6 @@ public abstract class ObjectExporter<T> {
} catch (IOException e) { } catch (IOException e) {
log.error(String.format("A FileWriter with the name %s has not " + log.error(String.format("A FileWriter with the name %s has not " +
"been able to be created.", filename)); "been able to be created.", filename));
// Todo: throw Exception to modify in ui
throw new FileNotFoundException();
} }
} }

View File

@ -21,7 +21,6 @@ package org.libreccm.portation.importer;
import com.opencsv.CSVReader; import com.opencsv.CSVReader;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -38,9 +37,9 @@ import java.util.stream.Collectors;
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a> * @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version 07/01/2016 * @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 String filename = null;
private char separator = ','; private char separator = ',';
@ -68,15 +67,16 @@ public abstract class ObjectImporter<T> {
/** /**
* Empty constructor. * Empty constructor.
*/ */
public ObjectImporter() {} public AbstractImporter() {}
/** /**
* Imports object information as {@link String} from a .csv-textfile as new * Imports object information as {@link String} from a .csv-textfile as new
* objects of type {@code T} into the database. * 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 { try {
CSVReader csvReader = new CSVReader(new FileReader(filename), CSVReader csvReader = new CSVReader(new FileReader(filename),
separator); separator);
@ -96,9 +96,7 @@ public abstract class ObjectImporter<T> {
log.error(String.format("Either a FileReader with the name %s has " + log.error(String.format("Either a FileReader with the name %s has " +
"not been able to be created or the file could not be " + "not been able to be created or the file could not be " +
"read.", filename)); "read.", filename));
return null;
// Todo: throw Exception to modify in ui
throw new FileNotFoundException();
} }
} }

View File

@ -20,6 +20,8 @@ package org.libreccm.docrepo;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import javax.persistence.Table; import javax.persistence.Table;
@ -30,8 +32,22 @@ import javax.persistence.Table;
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a> * @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version 01/10/2015 * @version 01/10/2015
*/ */
@Entity @Entity(name = "DocRepoFile")
@Table(schema = "CCM_DOCREPO", name = "FILES") @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 { public class File extends Resource {
private static final long serialVersionUID = -504220783419811504L; private static final long serialVersionUID = -504220783419811504L;
@ -61,4 +77,28 @@ public class File extends Resource {
} }
//< End GETTER & SETTER //< 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"
};
}
} }

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -19,6 +19,8 @@
package org.libreccm.docrepo; package org.libreccm.docrepo;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import javax.persistence.Table; import javax.persistence.Table;
@ -31,8 +33,22 @@ import java.util.List;
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a> * @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version 01/10/2015 * @version 01/10/2015
*/ */
@Entity @Entity(name = "DocRepoFolder")
@Table(schema = "CCM_DOCREPO", name = "FOLDERS") @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 { public class Folder extends Resource {
private static final long serialVersionUID = 1561466556458872622L; private static final long serialVersionUID = 1561466556458872622L;

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -27,8 +27,6 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
@ -45,21 +43,8 @@ import java.util.Date;
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a> * @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version 01/10/2015 * @version 01/10/2015
*/ */
@Entity(name = "DocRepoResource") @Entity
@Table(schema = "CCM_DOCREPO", name = "RESOURCES") @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 { public abstract class Resource extends CcmObject {
private static final long serialVersionUID = -910317798106611214L; private static final long serialVersionUID = -910317798106611214L;
@ -249,7 +234,7 @@ public abstract class Resource extends CcmObject {
this.lastModifiedUser = lastModifiedUser; this.lastModifiedUser = lastModifiedUser;
} }
public Resource getParent() { public Folder getParent() {
return parent; return parent;
} }

View File

@ -18,11 +18,9 @@
*/ */
package org.libreccm.docrepo; package org.libreccm.docrepo;
import org.apache.log4j.Logger;
import org.apache.oro.text.perl.Perl5Util; import org.apache.oro.text.perl.Perl5Util;
import javax.activation.MimetypesFileTypeMap; import javax.activation.MimetypesFileTypeMap;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
/** /**
@ -31,70 +29,56 @@ import javax.inject.Inject;
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a> * @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version 01/10/2015 * @version 01/10/2015
*/ */
@RequestScoped public abstract class ResourceManager<T extends Resource> {
public class ResourceManager {
private static final Logger log = Logger.getLogger(ResourceManager.class);
@Inject @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 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) { public void copy(T original, T copy) {
Resource copy = original instanceof File ?
copyFileSpecifics(new File(), (File) original) :
copyFolderSpecifics(new Folder(), (Folder) original);
copy.setName(original.getName()); copy.setName(original.getName());
copy.setDescription(original.getDescription()); 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.setMimeType(original.getMimeType());
copy.setSize(original.getSize()); copy.setSize(original.getSize());
// for file: setContent
copy.setCreationDate(original.getCreationDate()); copy.setCreationDate(original.getCreationDate());
copy.setLastModifiedDate(original.getLastModifiedDate()); copy.setLastModifiedDate(original.getLastModifiedDate());
copy.setCreationIp(original.getCreationIp()); copy.setCreationIp(original.getCreationIp());
copy.setLastModifiedIp(original.getLastModifiedIp()); copy.setLastModifiedIp(original.getLastModifiedIp());
copy.setCreationUser(original.getCreationUser()); copy.setCreationUser(original.getCreationUser());
copy.setLastModifiedUser(original.getLastModifiedUser()); copy.setLastModifiedUser(original.getLastModifiedUser());
copy.setParent(folder); if (copy.getParent() == null)
copy.setRepository(folder.getRepository()); copy.setParent(original.getParent());
copy.setRepository(original.getRepository());
resourceRepository.save(copy); // 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 {@link Resource} to be copied
* @param original The originl {@link File} * @param copy The {@code copy} of the given {@code original}
* @return A {@link Resource} with the file-specific data from the * {@link Resource}
* original {@link File} * @param newParent The new parent of the copy. Equals an new location
*/ */
private Resource copyFileSpecifics(File copy, File original) { public void copy(T original, T copy, Folder newParent) {
copy.setContent(original.getContent()); copy.setParent(newParent);
return copy; 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 * Determines weather the given name is a valid new name for the also
* given {@link Resource}. * given {@link Resource}.
@ -113,7 +97,7 @@ public class ResourceManager {
* *
* @return true for a system-valid resource name, otherwise false * @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(); Perl5Util perl5Util = new Perl5Util();
final String INVALID_START_PATTERN = "/^[.]+/"; final String INVALID_START_PATTERN = "/^[.]+/";
@ -132,7 +116,8 @@ public class ResourceManager {
perl5Util.match(INVALID_NAME_PATTERN, name)); perl5Util.match(INVALID_NAME_PATTERN, name));
// checks duplication of the name; database access (mind performance) // 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 // checks that the name corresponds to a compatible MIME type
validName &= new MimetypesFileTypeMap().getContentType(name).equals validName &= new MimetypesFileTypeMap().getContentType(name).equals

View File

@ -24,11 +24,10 @@ import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.security.PermissionChecker; import org.libreccm.security.PermissionChecker;
import org.libreccm.security.User; import org.libreccm.security.User;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.TypedQuery; import javax.persistence.TypedQuery;
import java.util.Arrays; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; 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> * @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version 01/10/2015 * @version 01/10/2015
*/ */
@RequestScoped public abstract class ResourceRepository<T extends Resource> extends
public class ResourceRepository extends AbstractAuditedEntityRepository<Long, T> {
AbstractAuditedEntityRepository<Long, Resource> {
protected Class classOfT;
@Inject @Inject
private EntityManager entityManager; protected EntityManager entityManager;
@Override @Override
public Long getEntityId(Resource entity) { public Long getEntityId(T entity) {
return entity.getObjectId(); return entity.getObjectId();
} }
@Override @Override
public Class<Resource> getEntityClass() { public Class<T> getEntityClass() {
return Resource.class; return classOfT;
} }
@Override @Override
public boolean isNew(Resource entity) { public boolean isNew(T entity) {
if (entity == null) { if (entity == null) {
throw new IllegalArgumentException("Entity to save can't be 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 * @param resources The requested {@link Resource}s, found in the database
* @return A list of {@link Resource}s the subject is allowed to access * @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 CdiUtil cdiUtil = new CdiUtil();
final PermissionChecker permissionChecker = cdiUtil.findBean( final PermissionChecker permissionChecker = cdiUtil.findBean(
PermissionChecker.class); PermissionChecker.class);
@ -88,56 +88,8 @@ public class ResourceRepository extends
* @return A list of at most one {@link Resource} the subject is allowed to * @return A list of at most one {@link Resource} the subject is allowed to
* access * access
*/ */
private Resource permissionFilter(Resource resource) { private T permissionFilter(T resource) {
return permissionFilter(Arrays.asList(resource)).get(0); return permissionFilter(Collections.singletonList(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());
} }
/** /**
@ -147,11 +99,91 @@ public class ResourceRepository extends
* @return The {@link Resource}s with the given name, if there aren't * @return The {@link Resource}s with the given name, if there aren't
* any an {@code EmptyList} * any an {@code EmptyList}
*/ */
public List<Resource> findByName(final String name) { public List<T> findByName(final String name) {
final TypedQuery<Resource> query = entityManager.createNamedQuery( final TypedQuery<T> query = getFindByNameQuery();
"DocRepo.findResourcesByName", Resource.class); query.setParameter("name", name);
query.setParameter("name", name);
return permissionFilter(query.getResultList()); 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();
} }

View File

@ -19,8 +19,9 @@
package org.libreccm.docrepo.portation.exporter; package org.libreccm.docrepo.portation.exporter;
import org.libreccm.docrepo.File; 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.ArrayList;
import java.util.List; import java.util.List;
@ -31,7 +32,9 @@ import java.util.List;
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a> * @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version 13/01/2016 * @version 13/01/2016
*/ */
public class FileExporter extends ObjectExporter<File> { @RequestScoped
public class FileExporter extends AbstractExporter<File> {
@Override @Override
protected String[] getClassName() { protected String[] getClassName() {
return new String[] {File.class.getName()}; return new String[] {File.class.getName()};
@ -39,22 +42,7 @@ public class FileExporter extends ObjectExporter<File> {
@Override @Override
protected String[] getAttributeNames() { protected String[] getAttributeNames() {
return new String[] { return File.getAttributeNames();
"name",
"description",
"path",
"mimeType",
"size",
"blobObject_ID",
"creationDate",
"lastModifiedDate",
"creationIp",
"lastModifiedIp",
"creator_ID",
"modifier_ID",
"parent_ID",
"repo_ID"
};
} }
// Todo: change ID to UUID // Todo: change ID to UUID
@ -66,7 +54,7 @@ public class FileExporter extends ObjectExporter<File> {
list.add(exportObject.getDescription()); list.add(exportObject.getDescription());
list.add(exportObject.getPath()); list.add(exportObject.getPath());
list.add(exportObject.getMimeType() != null ? list.add(exportObject.getMimeType() != null ?
exportObject.getMimeType().toString() : ""); exportObject.getMimeType().getPrimaryType() : "");
list.add(String.valueOf(exportObject.getSize())); list.add(String.valueOf(exportObject.getSize()));
list.add(exportObject.getContent() != null ? String.valueOf( list.add(exportObject.getContent() != null ? String.valueOf(
exportObject.getContent().getBlobObjectId()) : ""); exportObject.getContent().getBlobObjectId()) : "");

View File

@ -18,7 +18,6 @@
*/ */
package org.libreccm.docrepo.portation.importer; package org.libreccm.docrepo.portation.importer;
import org.apache.log4j.Logger;
import org.libreccm.cdi.utils.CdiUtil; import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.docrepo.BlobObject; import org.libreccm.docrepo.BlobObject;
import org.libreccm.docrepo.BlobObjectRepository; import org.libreccm.docrepo.BlobObjectRepository;
@ -27,12 +26,13 @@ import org.libreccm.docrepo.Folder;
import org.libreccm.docrepo.Repository; import org.libreccm.docrepo.Repository;
import org.libreccm.docrepo.RepositoryRepository; import org.libreccm.docrepo.RepositoryRepository;
import org.libreccm.docrepo.ResourceRepository; 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.User;
import org.libreccm.security.UserRepository; import org.libreccm.security.UserRepository;
import javax.activation.MimeType; import javax.activation.MimeType;
import javax.activation.MimeTypeParseException; import javax.activation.MimeTypeParseException;
import javax.enterprise.context.RequestScoped;
import java.util.Date; import java.util.Date;
/** /**
@ -42,28 +42,12 @@ import java.util.Date;
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a> * @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version 20/01/2016 * @version 20/01/2016
*/ */
public class FileImporter extends ObjectImporter<File> { @RequestScoped
public class FileImporter extends AbstractImporter<File> {
private static final Logger log = Logger.getLogger(FileImporter.class);
@Override @Override
protected boolean checkAttributeNames(String[] attributeNames) { protected boolean checkAttributeNames(String[] attributeNames) {
return attributeNames.equals(new String[] { return attributeNames.equals(File.getAttributeNames());
"name",
"description",
"path",
"mimeType",
"size",
"blobObject_ID",
"creationDate",
"lastModifiedDate",
"creationIp",
"lastModifiedIp",
"creator_ID",
"modifier_ID",
"parent_ID",
"repo_ID"
});
} }
@Override @Override
@ -78,9 +62,7 @@ public class FileImporter extends ObjectImporter<File> {
MimeType mimeType = new MimeType(); MimeType mimeType = new MimeType();
try { try {
mimeType.setPrimaryType(importStrings[3]); mimeType.setPrimaryType(importStrings[3]);
} catch (MimeTypeParseException e) { } catch (MimeTypeParseException ignored) {
log.warn(String.format("Unable to cast %s to a MimeType.",
importStrings[3]));
} }
file.setMimeType(mimeType); file.setMimeType(mimeType);
@ -132,7 +114,7 @@ public class FileImporter extends ObjectImporter<File> {
file.setRepository(repository); file.setRepository(repository);
} }
//resourceRepository.save(file); resourceRepository.save(file);
return file; return file;
} }
} }

View File

@ -5,9 +5,6 @@
*/ */
package org.libreccm.docrepo.portation; 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.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence; import org.jboss.arquillian.junit.InSequence;
@ -26,20 +23,20 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.libreccm.categorization.Categorization;
import org.libreccm.core.CcmObject;
import org.libreccm.core.CcmObjectRepository; import org.libreccm.core.CcmObjectRepository;
import org.libreccm.jpa.EntityManagerProducer; import org.libreccm.jpa.EntityManagerProducer;
import org.libreccm.jpa.utils.MimeTypeConverter; import org.libreccm.jpa.utils.MimeTypeConverter;
import org.libreccm.l10n.LocalizedString; import org.libreccm.l10n.LocalizedString;
import org.libreccm.security.Permission;
import org.libreccm.tests.categories.IntegrationTest; import org.libreccm.tests.categories.IntegrationTest;
import org.libreccm.testutils.EqualsVerifier; import org.libreccm.testutils.EqualsVerifier;
import org.libreccm.web.CcmApplication;
import org.libreccm.workflow.Workflow; 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.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.assertThat;
/** /**
* *
@ -115,6 +112,7 @@ public class ArquillianExampleTest {
// //
// @Test // @Test
// public void hello() {} // public void hello() {}
@Test @Test
@InSequence(1) @InSequence(1)
public void entityManagerIsInjected() { public void entityManagerIsInjected() {

View File

@ -19,16 +19,24 @@
package org.libreccm.docrepo.portation; package org.libreccm.docrepo.portation;
import org.apache.log4j.Logger; 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.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.categories.Category; 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.File;
import org.libreccm.docrepo.ResourceRepository;
import org.libreccm.docrepo.portation.exporter.FileExporter; import org.libreccm.docrepo.portation.exporter.FileExporter;
import org.libreccm.docrepo.portation.importer.FileImporter; import org.libreccm.docrepo.portation.importer.FileImporter;
import org.libreccm.tests.categories.IntegrationTest;
import org.libreccm.tests.categories.UnitTest; import org.libreccm.tests.categories.UnitTest;
import java.io.FileNotFoundException; import javax.inject.Inject;
import java.util.Arrays; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -37,13 +45,26 @@ import java.util.List;
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a> * @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version 13/01/2016 * @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 { public class FilePortationTest {
private static final Logger log = Logger.getLogger(FilePortationTest.class); 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 File file;
private static String filename = private static String filename =
"src/test/java/org/libreccm/docrepo/portation/exportTest.csv"; "src/test/java/org/libreccm/docrepo/portation/csv/exportTest.csv";
@BeforeClass @BeforeClass
public static void createResource() { public static void createResource() {
@ -57,25 +78,14 @@ public class FilePortationTest {
@Test @Test
public void csvShouldBeCreated() { public void csvShouldBeCreated() {
FileExporter fileExporter = new FileExporter();
fileExporter.setFilename(filename); fileExporter.setFilename(filename);
try { fileExporter.exportToCSV(Collections.singletonList(file));
fileExporter.exportToCSV(Arrays.asList(file));
} catch (FileNotFoundException e) {
log.error("Error exporting files.");
}
} }
@Test @Test
public void fileShouldBeCreated() { public void fileShouldBeCreated() {
FileImporter fileImporter = new FileImporter();
fileImporter.setFilename(filename); fileImporter.setFilename(filename);
List<File> files; List<File> files = fileImporter.importFromCSV();
try { log.info(files.toString());
files = fileImporter.importFromCSV();
log.info(files.toString());
} catch (FileNotFoundException e) {
log.error("Error exporting files.");
}
} }
} }