From 75ab72085a8b18914fb89dcf109ffb6c446dbdff Mon Sep 17 00:00:00 2001 From: tosmers Date: Wed, 27 Jan 2016 15:54:52 +0000 Subject: [PATCH] [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-94f89814c4df --- ...ectExporter.java => AbstractExporter.java} | 14 +- ...ectImporter.java => AbstractImporter.java} | 16 +- .../main/java/org/libreccm/docrepo/File.java | 42 ++++- .../org/libreccm/docrepo/FileManager.java | 43 +++++ .../org/libreccm/docrepo/FileRepository.java | 60 +++++++ .../java/org/libreccm/docrepo/Folder.java | 18 +- .../org/libreccm/docrepo/FolderManager.java | 44 +++++ .../libreccm/docrepo/FolderRepository.java | 60 +++++++ .../java/org/libreccm/docrepo/Resource.java | 19 +- .../org/libreccm/docrepo/ResourceManager.java | 75 ++++---- .../libreccm/docrepo/ResourceRepository.java | 162 +++++++++++------- .../portation/exporter/FileExporter.java | 26 +-- .../portation/importer/FileImporter.java | 32 +--- .../portation/ArquillianExampleTest.java | 14 +- .../docrepo/portation/FilePortationTest.java | 46 +++-- 15 files changed, 453 insertions(+), 218 deletions(-) rename ccm-core/src/main/java/org/libreccm/portation/exporter/{ObjectExporter.java => AbstractExporter.java} (91%) rename ccm-core/src/main/java/org/libreccm/portation/importer/{ObjectImporter.java => AbstractImporter.java} (92%) create mode 100644 ccm-docrepo/src/main/java/org/libreccm/docrepo/FileManager.java create mode 100644 ccm-docrepo/src/main/java/org/libreccm/docrepo/FileRepository.java create mode 100644 ccm-docrepo/src/main/java/org/libreccm/docrepo/FolderManager.java create mode 100644 ccm-docrepo/src/main/java/org/libreccm/docrepo/FolderRepository.java diff --git a/ccm-core/src/main/java/org/libreccm/portation/exporter/ObjectExporter.java b/ccm-core/src/main/java/org/libreccm/portation/exporter/AbstractExporter.java similarity index 91% rename from ccm-core/src/main/java/org/libreccm/portation/exporter/ObjectExporter.java rename to ccm-core/src/main/java/org/libreccm/portation/exporter/AbstractExporter.java index e8f6f9e65..a6f812e92 100644 --- a/ccm-core/src/main/java/org/libreccm/portation/exporter/ObjectExporter.java +++ b/ccm-core/src/main/java/org/libreccm/portation/exporter/AbstractExporter.java @@ -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 Tobias Osmers * @version 13/01/2016 */ -public abstract class ObjectExporter { +public abstract class AbstractExporter { - 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 { /** * 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 exportObjects) throws - FileNotFoundException { + public void exportToCSV(List exportObjects) { try { CSVWriter csvWriter = new CSVWriter(new FileWriter(filename), separator); @@ -93,9 +90,6 @@ public abstract class ObjectExporter { } 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(); } } diff --git a/ccm-core/src/main/java/org/libreccm/portation/importer/ObjectImporter.java b/ccm-core/src/main/java/org/libreccm/portation/importer/AbstractImporter.java similarity index 92% rename from ccm-core/src/main/java/org/libreccm/portation/importer/ObjectImporter.java rename to ccm-core/src/main/java/org/libreccm/portation/importer/AbstractImporter.java index 188ba288a..ae8162479 100644 --- a/ccm-core/src/main/java/org/libreccm/portation/importer/ObjectImporter.java +++ b/ccm-core/src/main/java/org/libreccm/portation/importer/AbstractImporter.java @@ -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 Tobias Osmers * @version 07/01/2016 */ -public abstract class ObjectImporter { +public abstract class AbstractImporter { - 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 { /** * 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 importFromCSV() throws FileNotFoundException { + public List importFromCSV() { try { CSVReader csvReader = new CSVReader(new FileReader(filename), separator); @@ -96,9 +96,7 @@ public abstract class ObjectImporter { 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; } } diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/File.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/File.java index a550e9d17..e019f0227 100644 --- a/ccm-docrepo/src/main/java/org/libreccm/docrepo/File.java +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/File.java @@ -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 Tobias Osmers * @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" + }; + } } diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/FileManager.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/FileManager.java new file mode 100644 index 000000000..8eac8b99c --- /dev/null +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/FileManager.java @@ -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 Tobias Osmers + * @version 27.01.2016 + */ +@RequestScoped +public class FileManager extends ResourceManager { + + @Inject + private FileRepository fileRepository; + + @Override + public void copy(File original, File copy) { + super.copy(original, copy); + copy.setContent(original.getContent()); + + fileRepository.save(copy); + } +} diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/FileRepository.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/FileRepository.java new file mode 100644 index 000000000..b6745afac --- /dev/null +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/FileRepository.java @@ -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 Tobias Osmers + * @version 27.01.2016 + */ +@RequestScoped +public class FileRepository extends ResourceRepository { + + public FileRepository() { + classOfT = File.class; + } + + @Override + public TypedQuery getFindByNameQuery() { + return entityManager.createNamedQuery( + "DocRepo.findFileByName", File.class); + } + + @Override + public TypedQuery getFindByPathNameQuery() { + return entityManager.createNamedQuery( + "DocRepo.findFileByPath", File.class); + } + + @Override + public TypedQuery getFindForCreatorQuery() { + return entityManager.createNamedQuery( + "DocRepo.findCreatedFileFromUser", File.class); + } + + @Override + public TypedQuery getFindForModifierQuery() { + return entityManager.createNamedQuery( + "DocRepo.findModifiedFileFromUser", File.class); + } +} diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/Folder.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/Folder.java index f382c2f0b..c5dd9f676 100644 --- a/ccm-docrepo/src/main/java/org/libreccm/docrepo/Folder.java +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/Folder.java @@ -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 Tobias Osmers * @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; diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/FolderManager.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/FolderManager.java new file mode 100644 index 000000000..55bae15cd --- /dev/null +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/FolderManager.java @@ -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 Tobias Osmers + * @version 27.01.2016 + */ +@RequestScoped +public class FolderManager extends ResourceManager { + + @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); + } +} diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/FolderRepository.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/FolderRepository.java new file mode 100644 index 000000000..8acd23e5f --- /dev/null +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/FolderRepository.java @@ -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 Tobias Osmers + * @version 27.01.2016 + */ +@RequestScoped +public class FolderRepository extends ResourceRepository { + + public FolderRepository() { + classOfT = Folder.class; + } + + @Override + public TypedQuery getFindByNameQuery() { + return entityManager.createNamedQuery( + "DocRepo.findFolderByName", Folder.class); + } + + @Override + public TypedQuery getFindByPathNameQuery() { + return entityManager.createNamedQuery( + "DocRepo.findFolderByPath", Folder.class); + } + + @Override + public TypedQuery getFindForCreatorQuery() { + return entityManager.createNamedQuery( + "DocRepo.findCreatedFolderFromUser", Folder.class); + } + + @Override + public TypedQuery getFindForModifierQuery() { + return entityManager.createNamedQuery( + "DocRepo.findModifiedFolderFromUser", Folder.class); + } +} diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/Resource.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/Resource.java index d2f5e6e8d..10a6e1b6c 100644 --- a/ccm-docrepo/src/main/java/org/libreccm/docrepo/Resource.java +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/Resource.java @@ -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 Tobias Osmers * @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; } diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/ResourceManager.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/ResourceManager.java index 7f8cddc1a..d7d9f0904 100644 --- a/ccm-docrepo/src/main/java/org/libreccm/docrepo/ResourceManager.java +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/ResourceManager.java @@ -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 Tobias Osmers * @version 01/10/2015 */ -@RequestScoped -public class ResourceManager { - private static final Logger log = Logger.getLogger(ResourceManager.class); +public abstract class ResourceManager { @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 original The {@link Resource} to be copied + * @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 diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/ResourceRepository.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/ResourceRepository.java index 72a474916..323dd97a1 100644 --- a/ccm-docrepo/src/main/java/org/libreccm/docrepo/ResourceRepository.java +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/ResourceRepository.java @@ -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 Tobias Osmers * @version 01/10/2015 */ -@RequestScoped -public class ResourceRepository extends - AbstractAuditedEntityRepository { +public abstract class ResourceRepository extends + AbstractAuditedEntityRepository { + + 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 getEntityClass() { - return Resource.class; + public Class 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 permissionFilter(List resources) { + private List permissionFilter(List 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 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 findForCreator(final User creator) { - final TypedQuery 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 findForModifier(final User modifier) { - final TypedQuery 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 findByName(final String name) { - final TypedQuery query = entityManager.createNamedQuery( - "DocRepo.findResourcesByName", Resource.class); - query.setParameter("name", name); + public List findByName(final String name) { + final TypedQuery 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 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 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 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 findForCreator(final User creator) { + final TypedQuery 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 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 findForModifier(final User modifier) { + final TypedQuery 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 getFindForModifierQuery(); } diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/portation/exporter/FileExporter.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/portation/exporter/FileExporter.java index 379178a19..96aef32ac 100644 --- a/ccm-docrepo/src/main/java/org/libreccm/docrepo/portation/exporter/FileExporter.java +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/portation/exporter/FileExporter.java @@ -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 Tobias Osmers * @version 13/01/2016 */ -public class FileExporter extends ObjectExporter { +@RequestScoped +public class FileExporter extends AbstractExporter { + @Override protected String[] getClassName() { return new String[] {File.class.getName()}; @@ -39,22 +42,7 @@ public class FileExporter extends ObjectExporter { @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 { 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()) : ""); diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/portation/importer/FileImporter.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/portation/importer/FileImporter.java index 03c43f6f6..761dd3028 100644 --- a/ccm-docrepo/src/main/java/org/libreccm/docrepo/portation/importer/FileImporter.java +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/portation/importer/FileImporter.java @@ -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 Tobias Osmers * @version 20/01/2016 */ -public class FileImporter extends ObjectImporter { - - private static final Logger log = Logger.getLogger(FileImporter.class); +@RequestScoped +public class FileImporter extends AbstractImporter { @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 { 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.setRepository(repository); } - //resourceRepository.save(file); + resourceRepository.save(file); return file; } } \ No newline at end of file diff --git a/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/ArquillianExampleTest.java b/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/ArquillianExampleTest.java index 189c31f47..48a997dc6 100644 --- a/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/ArquillianExampleTest.java +++ b/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/ArquillianExampleTest.java @@ -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() { diff --git a/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/FilePortationTest.java b/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/FilePortationTest.java index 56b879769..01edbb937 100644 --- a/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/FilePortationTest.java +++ b/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/FilePortationTest.java @@ -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 Tobias Osmers * @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 files; - try { - files = fileImporter.importFromCSV(); - log.info(files.toString()); - } catch (FileNotFoundException e) { - log.error("Error exporting files."); - } + List files = fileImporter.importFromCSV(); + log.info(files.toString()); } }