[elvystrac] r1924 - tons of business logic upgraded...

  • From: elvys@xxxxxxxxxxxxxxxxxxxxxx
  • To: elvystrac@xxxxxxxxxxxxx
  • Date: Sat, 27 Feb 2010 20:16:47 +0100

Author: DavidK
Date: 2010-02-27 20:16:46 +0100 (Sat, 27 Feb 2010)
New Revision: 1924

Modified:
   trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBL.java
   trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImpl.java
   
trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImplTest.java
   trunk/server/elvysCommons/src/elvys/server/bl/elvysrel/ElvysBLImplTest.java
   trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBL.java
   trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBLImpl.java
   trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBLTest.java
   trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBL.java
   trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBLImpl.java
   trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBLTest.java
Log:
tons of business logic upgraded...

Modified: 
trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBL.java
===================================================================
--- trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBL.java     
2010-02-27 19:00:44 UTC (rev 1923)
+++ trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBL.java     
2010-02-27 19:16:46 UTC (rev 1924)
@@ -24,16 +24,26 @@
        
        /**
         * Returns list of all companies in system.
+        * @param type CompanyType type
         * @return list of companies
         */
-       List<Company> getListOfCompanies(CompanyType type) throws Exception;
+       List<Company> listCompaniesMakeSession(CompanyType type) throws 
Exception;
        
        /**
+        * @see CompanyBL#listCompaniesMakeSession(CompanyType)
+        */
+       List<Company> listCompanies(CompanyType type, Session sess) throws 
Exception;
+       
+       /**
         * Returns object of company by company id.
         * @param companyId
         * @return
         */
        public Company getCompanyByIdMakeSession(Integer companyId) throws 
ExecuteException;
+       
+       /**
+        * @see CompanyBL#getCompanyById(Integer, Session)
+        */
        public Company getCompanyById(Integer companyId,Session sess) throws 
ExecuteException;
        
        

Modified: 
trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImpl.java
===================================================================
--- trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImpl.java 
2010-02-27 19:00:44 UTC (rev 1923)
+++ trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImpl.java 
2010-02-27 19:16:46 UTC (rev 1924)
@@ -30,28 +30,40 @@
        }
        
        @Override
-       public List<Company> getListOfCompanies(CompanyType type) throws 
ExecuteException {
+       public List<Company> listCompaniesMakeSession(CompanyType type) throws 
ExecuteException {
                Session sess = 
InitSessionFactory.getInstance().getCurrentSession();
                Transaction tx = HibUtils.startTx(sess);
                
                List<Company> companies = null;
                try { 
+                       companies = listCompanies(type, sess);
+                       tx.commit();
+               } catch (Exception e) {
+                       tx.rollback();
+                       throw new ExecuteException(e);
+               }
+               return companies;
+       }
+       
+       @Override
+       public List<Company> listCompanies(CompanyType type, Session sess)
+                       throws Exception {
+               List<Company> companies = null;
+               try {
                        String queryString = "from Company company " +          
                
-                                                                       "where 
company.disabled = 0 ";                                                         
                                 
+                                               "where company.disabled = 0 ";  
                                                                                
        
                        if (type == CompanyType.NOT_SYSTEM_ONLY ) {
+                               // lists only non-system
                                queryString += " AND system = 0 ";
                        } else if (type == CompanyType.SYSTEM_ONLY ) {
+                               // lists only system
                                queryString += " AND system = 1 ";
                        } else {
                                // ALL - no Restriction needed
                        }
-                       
                        Query query = sess.createQuery(queryString);
-                       
                        companies = query.list();
-                       tx.commit();
                } catch (Exception e) {
-                       tx.rollback();
                        String message = CommonUtils.prepareErrorMessage(
                                        CANNOT_LOAD_COMPANIES, 
"whichCompanies", type);
                        log.error(message,e);
@@ -90,7 +102,8 @@
                                company = null;
                        }
                } catch (Exception e) {
-                       String message = 
CommonUtils.prepareErrorMessage("couldn't get company", "companyId", companyId);
+                       String message = CommonUtils.prepareErrorMessage(
+                                       CANNOT_LOAD_COMPANY, "companyId", 
companyId);
                        throw new RuntimeException(message,e);
                }
                return company;
@@ -129,7 +142,7 @@
                                company = (Company)q.list().get(0);
                        }
                } catch (Exception e) {
-                       String message = 
CommonUtils.prepareErrorMessage("couldn't load company",
+                       String message = 
CommonUtils.prepareErrorMessage(CANNOT_LOAD_COMPANY,
                                        "groupId", groupId);
                        throw new ExecuteException(message,e);
                }

Modified: 
trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImplTest.java
===================================================================
--- 
trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImplTest.java 
    2010-02-27 19:00:44 UTC (rev 1923)
+++ 
trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImplTest.java 
    2010-02-27 19:16:46 UTC (rev 1924)
@@ -34,19 +34,19 @@
        
        @Test
        public void getListOfNonSystemCompanies() throws Exception {
-               List<Company> companies = 
cbl.getListOfCompanies(CompanyType.NOT_SYSTEM_ONLY);
+               List<Company> companies = 
cbl.listCompaniesMakeSession(CompanyType.NOT_SYSTEM_ONLY);
                printCompanies(companies);
        }
        
        @Test
        public void getListOfSystemCompanies() throws Exception {
-               List<Company> companies = 
cbl.getListOfCompanies(CompanyType.SYSTEM_ONLY);
+               List<Company> companies = 
cbl.listCompaniesMakeSession(CompanyType.SYSTEM_ONLY);
                printCompanies(companies);
        }
        
        @Test
        public void getListOfAllCompanies() throws Exception {
-               List<Company> companies = 
cbl.getListOfCompanies(CompanyType.ALL);
+               List<Company> companies = 
cbl.listCompaniesMakeSession(CompanyType.ALL);
                printCompanies(companies);
        }
        

Modified: 
trunk/server/elvysCommons/src/elvys/server/bl/elvysrel/ElvysBLImplTest.java
===================================================================
--- trunk/server/elvysCommons/src/elvys/server/bl/elvysrel/ElvysBLImplTest.java 
2010-02-27 19:00:44 UTC (rev 1923)
+++ trunk/server/elvysCommons/src/elvys/server/bl/elvysrel/ElvysBLImplTest.java 
2010-02-27 19:16:46 UTC (rev 1924)
@@ -134,7 +134,7 @@
                // file types
                List<FileType> types = 
fbl.listCompanyAllowedFileTypesMakeSession(companyId);
                // files
-               List<File> files = fbl.listFilesOfCompany(companyId, types);
+               List<File> files = fbl.listFilesOfCompanyMakeSession(companyId, 
types);
                
                
                for(File file: files) {

Modified: trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBL.java
===================================================================
--- trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBL.java       
2010-02-27 19:00:44 UTC (rev 1923)
+++ trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBL.java       
2010-02-27 19:16:46 UTC (rev 1924)
@@ -5,6 +5,7 @@
 
 import org.hibernate.Session;
 
+import cz.elvys.webServer.HIBgen.File;
 import cz.elvys.webServer.HIBgen.FileType;
 import cz.elvys.webServer.toSpring.exception.ExecuteException;
 
@@ -46,6 +47,8 @@
         */
        public List<FileType> listCompanyAllowedFileTypes(Integer companyId, 
Session sess) throws ExecuteException;
        
+       
+       
        /**
         * Gets file type object connected with the given id.
         * @param fileTypeID id of the fileType. Not Null.
@@ -76,16 +79,38 @@
        
        
        /**
+        * gets file by its id
+        * @param fileId
+        * @return FIle object
+        */
+       public File getFileByIdMakeSession(Integer fileId) throws 
ExecuteException;
+       
+       
+       /**
+        * @see FileManagerBL#getFileByIdMakeSession(Integer)
+        */
+       public File getFileById(Integer fileId, Session sess) throws 
ExecuteException;
+       
+       
+       
+       
+       /**
         * Gets files which belong to logged user's company
         * @param companyID company identifier (a DB company's identifier)
         *              NOT NULL
         * @param types list of FileType object. It allows to list files by 
type. 
         * @return
         */
-       public List<cz.elvys.webServer.HIBgen.File> listFilesOfCompany(int 
companyID, List<FileType> types) throws ExecuteException;
+       public List<cz.elvys.webServer.HIBgen.File> 
listFilesOfCompanyMakeSession(int companyID, List<FileType> types) throws 
ExecuteException;
        
        
        /**
+        * @see FileManagerBL#listFilesOfCompanyMakeSession(int, List)
+        */
+       public List<cz.elvys.webServer.HIBgen.File> listFilesOfCompany(int 
companyID, List<FileType> types, Session sess) throws ExecuteException;
+       
+       
+       /**
         * Stores uploaded file. <BR/>
         * Destination is: 
'FILE_MANAGER_PATH'/'company_name'/'unique_id_from_db'.'suffix'.
         * @param origFile original file (beware of using name- it can be 
'hfqyvxxafrqv.upload')
@@ -114,20 +139,12 @@
        /**
         * Deletes all planning of given file and mark file as "deleted".
         * Not really deletes the file.
-        * @param file file to "delete"
+        * @param file file id to "delete"
         */
-       public void deleteFile(cz.elvys.webServer.HIBgen.File file) throws 
ExecuteException;
+       public void deleteFile(Integer file) throws ExecuteException;
        
        
        /**
-        * Tells, whether file has existing planning or not. Usefull before 
deleting file.
-        * @param file a file
-        * @return true (has planning) or false (doesn't have planning)
-        */
-       public boolean fileHasFuturePlanning(cz.elvys.webServer.HIBgen.File 
file) throws ExecuteException;
-       
-       
-       /**
         * @param file file object
         * @return MD5 hash of the file 
         * @throws IOException when file doesn't exist
@@ -142,6 +159,11 @@
         */
        public Integer getFileSize(java.io.File file) throws ExecuteException;
        
+       /**
+        * Repairs hashes on all files
+        * @return count of repaired 
+        */
+       public Integer repairFileHashesAndSizes() throws ExecuteException;
        
        /**
         * Gets list of "conflicting" files. I.e. files with same MD5Hash.

Modified: 
trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBLImpl.java
===================================================================
--- trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBLImpl.java   
2010-02-27 19:00:44 UTC (rev 1923)
+++ trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBLImpl.java   
2010-02-27 19:16:46 UTC (rev 1924)
@@ -27,16 +27,18 @@
 import cz.elvys.commons.def.PropertyManager;
 import cz.elvys.commons.utils.CommonUtils;
 import cz.elvys.commons.utils.HibUtils;
+import cz.elvys.webServer.HIBgen.Company;
 import cz.elvys.webServer.HIBgen.ContentType;
-import cz.elvys.webServer.HIBgen.FileFrameContent;
 import cz.elvys.webServer.HIBgen.FileType;
-import cz.elvys.webServer.HIBgen.Timeline;
 import cz.elvys.webServer.HIBgen.User;
 import cz.elvys.webServer.db.InitSessionFactory;
 import cz.elvys.webServer.toSpring.exception.ExecuteException;
 import elvys.server.bl.Factory;
+import elvys.server.bl.companyRel.CompanyBL;
+import elvys.server.bl.companyRel.CompanyBL.CompanyType;
 import elvys.server.bl.permTypesRel.PermittedSchedulingBL;
 
+
 public class FileManagerBLImpl implements FileManagerBL {
 
        // consts
@@ -53,17 +55,19 @@
        
        
        private static final String list_files_SELECT_FILES = "Unable to get a 
list of company files";
+       private static final String list_files_GET_FILE = "Unable to get file 
from the DB";
        private static final String store_file_ERROR = "Unable to store the 
file.";
        private static final String ERROR_FILE_DELETING = "File deleting 
failed- file couldn't be deleted from DB";
-       private static final String ERROR_LOOKUP_FILE_PLANNING = "Error lookup 
file planning";
        private static final String ERROR_FIND_CONFLICTING_FILES = "Error when 
looking for conflicting files in db..";
        
        // bl fields
        private PermittedSchedulingBL psbl;
+       private CompanyBL cbl;
        
        // init
        {
                psbl = Factory.getPermittedSchedulingBL();
+               cbl = Factory.getCompanyBL();
        }
        
        @Override
@@ -187,7 +191,9 @@
                FileType fileType = null;
                try {
                        fileType = getCompanyAllowedFileType(fileTypeID, 
companyId, sess);
+                       tx.commit();
                } catch (Exception e) {
+                       tx.rollback();
                        String message = CommonUtils.prepareErrorMessage(
                                get_type_CANNOT_GET_ALLOWED_TYPE, "companyId", 
companyId,
                                "fileTypeID", fileTypeID);
@@ -230,7 +236,9 @@
                FileType fileType = null;
                try {
                        fileType = getCompanyAllowedFileTypeByType(type, 
companyId, sess);
+                       tx.commit();
                } catch (Exception e) {
+                       tx.rollback();
                        String message = CommonUtils.prepareErrorMessage(
                                get_type_CANNOT_GET_ALLOWED_TYPE, "companyId", 
companyId,
                                "type", type);
@@ -268,28 +276,91 @@
        
        
        
+       @Override
+       public cz.elvys.webServer.HIBgen.File getFileByIdMakeSession(Integer 
fileId)
+                       throws ExecuteException {
+               if (fileId == null) throw new ExecuteException("fileId==null");
+               
+               Session sess = 
InitSessionFactory.getInstance().getCurrentSession();
+               Transaction tx = HibUtils.startTx(sess);
+               cz.elvys.webServer.HIBgen.File file = null;
+               // get list from DB
+               try {
+                       file = getFileById(fileId, sess);
+                       tx.commit();
+               } catch (Exception e) {
+                       tx.rollback();
+                       throw new ExecuteException(e);
+               }
+               return file;
+       }
        
-       
        @Override
-       public List<cz.elvys.webServer.HIBgen.File> listFilesOfCompany(
+       public cz.elvys.webServer.HIBgen.File getFileById(Integer fileId,
+                       Session sess) throws ExecuteException {
+               // check contract
+               if (fileId == null) throw new ExecuteException("fileId==null");
+               
+               cz.elvys.webServer.HIBgen.File file = null;
+               try {
+                       // create filelist request by types 
+                       Query query = sess.createQuery("select file from File 
as file " +
+                                       "inner join fetch file.user " +
+                                       "inner join fetch file.fileType ftype " 
+
+                                       "where file.id = :fileId");
+                       query.setInteger("fileId", fileId);
+                       List<cz.elvys.webServer.HIBgen.File> files = 
query.list();
+                       if (files.size() > 0) {
+                               file = files.get(0);
+                       } else {
+                               file = null;
+                       }
+               } catch (Exception e) {
+                       String message = CommonUtils.prepareErrorMessage(
+                               list_files_GET_FILE, "fileId", fileId);
+                       throw new ExecuteException(message,e);
+               }
+               return file;
+       }
+
+
+       @Override
+       public List<cz.elvys.webServer.HIBgen.File> 
listFilesOfCompanyMakeSession(
                        int companyID, List<FileType> types) throws 
ExecuteException {
                if (types == null) throw new ExecuteException("types==null");
                if (types.size() == 0) return new 
ArrayList<cz.elvys.webServer.HIBgen.File>();
                
-               
-               // gets list of company's files
+               Session sess = 
InitSessionFactory.getInstance().getCurrentSession();
+               Transaction tx = HibUtils.startTx(sess);
                List<cz.elvys.webServer.HIBgen.File> files = null;
-               Session sess = 
InitSessionFactory.getInstance().getCurrentSession();
-               
-               // create filelist request by types 
-               Integer[] fileTypeIds = new Integer[types.size()]; 
-               for (int i=0; i<types.size(); i++) {
-                       FileType type = types.get(i);
-                       fileTypeIds[i] = type.getId();
-               }
                // get list from DB
-               Transaction tx = HibUtils.startTx(sess);
                try {
+                       files = listFilesOfCompany(companyID, types, sess);
+                       tx.commit();
+               } catch (Exception e) {
+                       tx.rollback();
+                       throw new ExecuteException(e);
+               }
+               return files;
+       }
+       
+       
+       @Override
+       public List<cz.elvys.webServer.HIBgen.File> listFilesOfCompany(
+                       int companyID, List<FileType> types, Session sess)
+                       throws ExecuteException {
+               // check contract
+               if (types == null) throw new ExecuteException("types==null");
+               if (types.size() == 0) return new 
ArrayList<cz.elvys.webServer.HIBgen.File>();
+               
+               List<cz.elvys.webServer.HIBgen.File> files = null;
+               try {
+                       // create filelist request by types 
+                       Integer[] fileTypeIds = new Integer[types.size()]; 
+                       for (int i=0; i<types.size(); i++) {
+                               FileType type = types.get(i);
+                               fileTypeIds[i] = type.getId();
+                       }
                        Query query = sess.createQuery("select file from File 
as file " +
                                        "inner join fetch file.user " +
                                        "inner join fetch file.fileType ftype " 
+
@@ -300,9 +371,7 @@
                        query.setInteger("companyID", companyID);
                        query.setParameterList("fileTypeIds", fileTypeIds, 
Hibernate.INTEGER);
                        files = query.list();
-                       tx.commit();
                } catch (Exception e) {
-                       tx.rollback();
                        String message = 
CommonUtils.prepareErrorMessage(list_files_SELECT_FILES,
                                "companyID", companyID);
                        throw new ExecuteException(message,e);
@@ -310,7 +379,6 @@
                return files;
        }
        
-       
        @Override
        public void storeFile(File origFile, String realName, Integer 
companyId, Integer userId, 
                FileType type, String MD5Hash, String description, boolean 
deleteOldFiles) throws ExecuteException {
@@ -444,78 +512,64 @@
        }
        
        @Override
-       public void deleteFile(cz.elvys.webServer.HIBgen.File file) throws 
ExecuteException {
-               if (file == null) throw new ExecuteException(new 
NullPointerException("file null"));
-               // TODO prozkoumat, jestli funguje
+       public void deleteFile(Integer fileId) throws ExecuteException {
+               if (fileId == null) throw new ExecuteException(new 
NullPointerException("file null"));
                
                // mark selected document in DB as disabled
                Session sess = 
InitSessionFactory.getInstance().getCurrentSession();
                Transaction tx = HibUtils.startTx(sess);
                try {
+                       
+                       Query q = sess.createQuery(
+                               "DELETE FROM Timeline tl " +
+                               " WHERE tl.frameContent in " +
+                               "           ( select frc.id " +
+                               "               from FileFrameContent frc, " +
+                               "               File file " +
+                               "               WHERE frc.file = file.id" +
+                               "                 and file.id = :fileid)");
+                       q.setInteger("fileid", fileId);
+                       int res1 = q.executeUpdate();
+                       
+                       
+                       q = sess.createQuery(
+                               "DELETE FROM FileFrameContent frc " +
+                               "WHERE frc.file = :fileid");
+                       q.setInteger("fileid", fileId);
+                       int res2 = q.executeUpdate();
+                       
+                       cz.elvys.webServer.HIBgen.File file = 
getFileById(fileId, sess);
+                       
+                       // check whether count of deleted are equal
+                       if (res1 != res2) {
+                               String message = 
CommonUtils.prepareErrorMessage(
+                                               "deleted timelines count <> 
deleted framecontent size or are empty", "tl",res1, "frc", res2);
+                               throw new Exception(message);
+                       }
+                       
+                       
                        // set document as disabled
                        file.setDisabled(true);
                        sess.update(file);
                        
-                       // delete all plannings to this document
-                       Set<FileFrameContent> contents = 
(Set<FileFrameContent>) file.getFileFrameContents();
-                       for (FileFrameContent contentToDelete: contents) {
-                               Set<Timeline> timelines = (Set<Timeline>) 
contentToDelete.getTimelines();
-                               
-                               // get Timeline for current FrameContent - 1:1 
cardinality
-                               Timeline timelineToDelete = 
timelines.iterator().next();
-                               // delete planning
-                               sess.delete(timelineToDelete);
-                               
-                               // delete information about document planning
-                               sess.delete(contentToDelete);
-                       }
+                       
+                       // remove selected document from the filesystem
+                       Integer companyId = file.getUser().getCompany().getId();
+                       String path = PropertyManager.getFileFolderPath() + 
File.separatorChar +
+                               companyId.toString() + File.separatorChar + 
file.getUniqName();
+                       File todel = new File(path);
+                       if (!todel.delete()) {
+                               throw new Exception("File deleting failed- file 
couldn't be deleted from filesystem");
+                       }                       
                        tx.commit();
                } catch (Exception e) {
                        tx.rollback();
-                       String message = 
CommonUtils.prepareErrorMessage(ERROR_FILE_DELETING, "fileId", file.getId(), 
"fileName", file.getOrigName());
+                       String message = 
CommonUtils.prepareErrorMessage(ERROR_FILE_DELETING, "fileId", fileId);
                        throw new ExecuteException(message,e);
                }
-               
-               // don't remove selected document from disc
-               /*File file = new File(path);
-               if (!file.delete()) {
-                       throw new Exception("File deleting failed- file 
couldn't be deleted from harddisc");
-               }*/
        }
        
-       
-       
        @Override
-       public boolean fileHasFuturePlanning(cz.elvys.webServer.HIBgen.File 
file) throws ExecuteException {
-               // TODO prozkoumat, jestli funguje (spolecne s mazanim souboru)
-               
-               // TODO tohle musi jit do FileStorageManagera, je to 
file-specific :/
-               // TODO kecy, nemuze.. pac to je planning-type specific a ne 
file-specific :/
-               // TODO kam teda s tim? navrh - zatim to tady tupe rozepsat, 
potom zmenit DB model a udelat predka "FrameContent" - "FrameContentWithFile" a 
od nej podedi File i Document
-               // some document is chosen, try to find some plannings
-//             Session sess = 
InitSessionFactory.getInstance().getCurrentSession();
-//             Transaction tx = HibUtils.startTx(sess);
-//             try {
-//                     Query q = sess.createQuery("from Timeline as timeline, 
DocumentContent as doccon " +
-//                                     "where timeline.showEnd > :now and 
timeline.frameContent.id = doccon.id and doccon.document.id = :docid");
-//                     q.setTimestamp("now", new Date());
-//                     q.setInteger("docid", file.getId());
-//                     if (q.list().size() > 0) {
-//                             tx.commit();
-//                             return true;
-//                     } else {
-//                             tx.commit();
-//                             return false;
-//                     }
-//             } catch (Exception e) {
-//                     String message = 
CommonUtils.prepareErrorMessage(ERROR_LOOKUP_FILE_PLANNING, file.getOrigName());
-//                     throw new ExecuteException(message,e);
-//             }
-               return true;
-       }
-       
-       
-       @Override
        public String getMD5Hash(File file) throws IOException {
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);
@@ -530,7 +584,67 @@
                size = ((Long) file.length()).intValue();
                return size;
        }
+
+       @Override
+       public Integer repairFileHashesAndSizes() throws ExecuteException {
+               Session sess = 
InitSessionFactory.getInstance().getCurrentSession();
+               Transaction tx = HibUtils.startTx(sess);
+               int count = 0;
+               try {
+                       // get companies
+                       List<Company> companies = 
cbl.listCompanies(CompanyType.ALL, sess);
+                       // get permitted types
+                       
+                       // get filefolder root
+                       String ffRoot = PropertyManager.getFileFolderPath();
+                       
+                       // iterate through all companies
+                       for (Company comp: companies) {
+                               // folder path
+                               String folderPath = ffRoot + File.separatorChar 
+ comp.getId().toString() + File.separatorChar ;
+                               // get permitted filetypes
+                               List<FileType> fileTypes = 
listCompanyAllowedFileTypes(comp.getId(), sess);
+                               // get files
+                               List<cz.elvys.webServer.HIBgen.File> files = 
listFilesOfCompany(comp.getId(), fileTypes, sess); 
+                               
+                               // iterate through all company files
+                               for(cz.elvys.webServer.HIBgen.File file: files) 
{
+                                       // has good hash?
+                                       if (file.getHashcode().length() == 32 
&& file.getSize() != 0) {
+                                               // ok, go on
+                                               continue;
+                                       }
+
+                                       String filePath = folderPath + 
file.getUniqName();
+                                       File fo = new File(filePath);
+                                       
+                                       if (fo.exists()) {
+                                               // repair hashcode
+                                               String newHC = getMD5Hash(fo);
+                                               file.setHashcode(newHC);
+                                               // repair size
+                                               Integer size = getFileSize(fo);
+                                               file.setSize(size);
+                                               // update file
+                                               sess.update(file);
+                                               count++;
+                                       } else {
+                                               // disable file that is not 
having corresponding file on filesystem
+                                               // TODO - zatim nechceme?
+                                       }
+                               }
+                               
+                       }
+                       tx.commit();
+               } catch (Exception e){
+                       String message = CommonUtils.prepareErrorMessage(
+                                       "couldn't repair hashcodes of files");
+                       throw new ExecuteException(message,e);
+               }
+               return count;
+       }
        
+       
        @Override
        public List<cz.elvys.webServer.HIBgen.File> getFilesWithSameHash(
                        String MD5Hash, Integer companyId) throws 
ExecuteException {

Modified: 
trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBLTest.java
===================================================================
--- trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBLTest.java   
2010-02-27 19:00:44 UTC (rev 1923)
+++ trunk/server/elvysCommons/src/elvys/server/bl/file/FileManagerBLTest.java   
2010-02-27 19:16:46 UTC (rev 1924)
@@ -8,6 +8,7 @@
 import org.hibernate.Transaction;
 import org.hibernate.classic.Session;
 import org.junit.BeforeClass;
+import org.junit.Ignore;
 import org.junit.Test;
 import static org.junit.Assert.*;
 
@@ -35,7 +36,17 @@
        }
        
        
+       
+       
        @Test
+       public void WRITINGtestDeleteFile() throws Exception {
+               int fileid = 39;
+               fbl.deleteFile(fileid);
+               
+       }
+       
+       
+       @Test
        public void testListFileTypes() throws Exception {
                
                // company allowed file types
@@ -82,34 +93,11 @@
                assertFileTypeList(types);
                
                // company allowed file types 
-               List<File> files = fbl.listFilesOfCompany(companyId, types);
+               List<File> files = fbl.listFilesOfCompanyMakeSession(companyId, 
types);
                assertFileList(files);
        }
        
        @Test
-       public void testFileHasFuturePlanning() throws Exception {
-               int futurePlannedFiles = 0; // files planned in the future
-               
-               // company allowed types
-               List<FileType> types = 
fbl.listCompanyAllowedFileTypesMakeSession(companyId);
-               assertFileTypeList(types);
-               
-               // company allowed file types 
-               List<File> files = fbl.listFilesOfCompany(companyId, types);
-               assertFileList(files);
-               
-               for(File file: files) {
-                       // find future planning for each file
-                       boolean fhfp = fbl.fileHasFuturePlanning(file);
-                       if (fhfp) {
-                               futurePlannedFiles++;
-                       }
-               }
-
-               assertTrue(futurePlannedFiles > 0);
-       }
-       
-       @Test
        public void filesHaveUniqueHash() throws Exception {
                int numInCollision = 0; // must be null
                
@@ -120,7 +108,7 @@
                assertFileTypeList(types);
                
                // company allowed file types 
-               List<File> files = fbl.listFilesOfCompany(companyId, types);
+               List<File> files = fbl.listFilesOfCompanyMakeSession(companyId, 
types);
                assertFileList(files);
                
                for(File file : files) {
@@ -169,6 +157,14 @@
                assertTrue(found >0);
        }
        
+       @Ignore
+       @Test
+       public void WRITINGrepairFileHashes() throws Exception{
+               int result = fbl.repairFileHashesAndSizes();
+               
+               System.out.println("repaired hashcodes: "+result);
+               assertTrue(result == 0);
+       }
        
        
        
@@ -183,9 +179,6 @@
        
        
        
-       
-       
-       
        // check list of FileType
        private void assertFileTypeList(List<FileType> list) {
                assertTrue(list != null && list.size() > 0);

Modified: trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBL.java
===================================================================
--- trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBL.java      
2010-02-27 19:00:44 UTC (rev 1923)
+++ trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBL.java      
2010-02-27 19:16:46 UTC (rev 1924)
@@ -4,10 +4,16 @@
 import java.util.List;
 import java.util.Map;
 
+import org.hibernate.Query;
 import org.hibernate.Session;
+import org.hibernate.Transaction;
 
+import cz.elvys.commons.utils.CommonUtils;
+import cz.elvys.commons.utils.HibUtils;
 import cz.elvys.webServer.HIBgen.ContentType;
+import cz.elvys.webServer.HIBgen.File;
 import cz.elvys.webServer.HIBgen.Timeline;
+import cz.elvys.webServer.db.InitSessionFactory;
 import cz.elvys.webServer.toSpring.exception.ExecuteException;
 
 public interface PlanningBL {
@@ -125,6 +131,16 @@
        
        
        /**
+        * Checks, whether file has future plannings or not - Usefull before 
deleting file.
+        * @param file file to check
+        * @return true/false
+        * @throws ExecuteException if something went wrong
+        */
+       public boolean fileHasFuturePlanning(File file) throws ExecuteException;
+       
+       
+       
+       /**
         * Returns list of company plannings, that are of given contet_type
         * Fetches: type-specific frame_content (not file, not modif. user)
         * @param companyId id of the company

Modified: 
trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBLImpl.java
===================================================================
--- trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBLImpl.java  
2010-02-27 19:00:44 UTC (rev 1923)
+++ trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBLImpl.java  
2010-02-27 19:16:46 UTC (rev 1924)
@@ -36,6 +36,7 @@
        private static final String CANNOT_GET_RID_OF_TIMELINES = "Couldn't get 
rid of Timelines. ";
        private static final String CANNOT_LIST_FUTURE_TIMELINES = "Couldn't 
list future timelines!";
        private static final String CANNOT_REPLICATE_TIMELINE = "Couldn't 
replicate timeline.";
+       private static final String ERROR_LOOKUP_FILE_PLANNING = "Error lookup 
file planning";
        
        // static fields
        private static Logger log = null;
@@ -406,6 +407,36 @@
        
        
        @Override
+       public boolean fileHasFuturePlanning(cz.elvys.webServer.HIBgen.File 
file) throws ExecuteException {
+               Session sess = 
InitSessionFactory.getInstance().getCurrentSession();
+               Transaction tx = HibUtils.startTx(sess);
+               boolean ret = true;
+               try {
+                       Query q = sess.createQuery(
+                                       "from Timeline as timeline, " +
+                                       "FileFrameContent ffc " +
+                                       "where timeline.showEnd > :now " +
+                                       "  and timeline.frameContent.id = 
ffc.id " +
+                                       "  and ffc.file.id = :fileid");
+                       q.setTimestamp("now", new Date());
+                       q.setInteger("fileid", file.getId());
+                       if (q.list().size() > 0) {
+                               ret = true;
+                       } else {
+                               ret = false;
+                       }
+                       tx.commit();
+               } catch (Exception e) {
+                       tx.rollback();
+                       String message = 
CommonUtils.prepareErrorMessage(ERROR_LOOKUP_FILE_PLANNING, file.getOrigName());
+                       throw new ExecuteException(message,e);
+               }
+               return ret;
+       }
+       
+       
+       
+       @Override
        public List<Timeline> listTimelinesOfTypeForCompanyMakeSession(
                        Integer companyId, Integer contentId) throws 
ExecuteException {
                Session sess = 
InitSessionFactory.getInstance().getCurrentSession();

Modified: 
trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBLTest.java
===================================================================
--- trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBLTest.java  
2010-02-27 19:00:44 UTC (rev 1923)
+++ trunk/server/elvysCommons/src/elvys/server/bl/planning/PlanningBLTest.java  
2010-02-27 19:16:46 UTC (rev 1924)
@@ -14,8 +14,9 @@
 import cz.elvys.webServer.HIBgen.Category;
 import cz.elvys.webServer.HIBgen.CategoryPlanning;
 import cz.elvys.webServer.HIBgen.ContentType;
-import cz.elvys.webServer.HIBgen.Elvys;
 import cz.elvys.webServer.HIBgen.ElvysGroup;
+import cz.elvys.webServer.HIBgen.File;
+import cz.elvys.webServer.HIBgen.FileType;
 import cz.elvys.webServer.HIBgen.Layout;
 import cz.elvys.webServer.HIBgen.Panel;
 import cz.elvys.webServer.HIBgen.Timeline;
@@ -24,6 +25,7 @@
 import elvys.server.bl.Factory;
 import elvys.server.bl.categrel.CategoryBL;
 import elvys.server.bl.elvysrel.ElvysBL;
+import elvys.server.bl.file.FileManagerBL;
 import elvys.server.bl.layoutrel.LayoutBL;
 import elvys.server.bl.permTypesRel.PermittedSchedulingBL;
 import static org.junit.Assert.*;
@@ -38,6 +40,7 @@
        private static CategoryBL cbl;
        private static ElvysBL ebl;
        private static LayoutBL lbl;
+       private static FileManagerBL fbl;
        private static PermittedSchedulingBL psbl;
        
        @BeforeClass
@@ -46,6 +49,7 @@
                cbl = Fact.getCategoryBL();
                ebl = Fact.getElvysBL();
                lbl = Fact.getLayoutBL();
+               fbl = Fact.getFileManagerBL();
                psbl = Factory.getPermittedSchedulingBL();
        }
        
@@ -169,6 +173,26 @@
                assertTrue(foundCount > 0);
        }
        
+       @Test
+       public void testFileHasFuturePlanning() throws Exception {
+               int futurePlannedFiles = 0; // files planned in the future
+               
+               // company allowed types
+               List<FileType> types = 
fbl.listCompanyAllowedFileTypesMakeSession(companyId);
+               
+               // company allowed file types 
+               List<File> files = fbl.listFilesOfCompanyMakeSession(companyId, 
types);
+               
+               for(File file: files) {
+                       // find future planning for each file
+                       boolean fhfp = pbl.fileHasFuturePlanning(file);
+                       if (fhfp) {
+                               futurePlannedFiles++;
+                       }
+               }
+               assertTrue(futurePlannedFiles > 0);
+       }
+       
        @Test 
        public void testListTimelinesOfTypeForCompany() throws Exception {
                int found = 0;


Other related posts:

  • » [elvystrac] r1924 - tons of business logic upgraded... - elvys