Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.wintindustries.pdffilter.PFData; import com.wintindustries.pdffilter.PFLocation.PFFolder; import com.wintindustries.pdffilter.PFLocation.PFFolderConstants; import com.wintindustries.pdffilter.PFLocation.PFLocation; import com.wintindustries.pdffilter.PF_DATABASE_ENTITIES.PFPermissions; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.logging.Logger; import org.hibernate.Criteria; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Restrictions; /** * * @author Admin */ public class PFDatabase { static Logger log = Logger.getLogger(PFDatabase.class.getName()); Configuration config; SessionFactory factory; PFFolder rootFolder; PFDataService service; Set<PFFolder> folderCache; String name; // the name of the Datasource. Used to identify which datasourse is printing out log information. private PFSessionManager sessionManager; public PFSessionManager getSessionManager() { return sessionManager; } public synchronized void setService(PFDataService service) { this.service = service; } public synchronized boolean folderCacheIsValid() { // the folder Cache is only valid as long as the session is open if (sessionManager.currentSession == null) return false; else return sessionManager.currentSession.isOpen(); } public synchronized PFDataService getService() { return service; } public synchronized Configuration getConfiguration() { return config; } public synchronized void setConfiguraton(Configuration config) { this.config = config; } public synchronized SessionFactory getFactory() { return factory; } public synchronized void setFactory(SessionFactory factory) { this.factory = factory; } public synchronized void setName(String name) { this.name = name + " Database"; } public synchronized PFFolder getPFFolderFromID(Long hibernateid) { return (PFFolder) sessionManager.getSession().get(PFFolder.class, hibernateid); } public synchronized void reloadConfiguration() { // Configuration cfg = new Configuration() config = service.getConfiguraton().getHibernateConfiguration(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(config.getProperties()); factory = config.buildSessionFactory(builder.build()); sessionManager = new PFSessionManager(factory); } public synchronized void configure() { reloadConfiguration(); // first reload the configuration first } public synchronized void buildNewDatabase() { // start of by creating default filestructure System.out.println("CREATING NEW DB"); sessionManager.OpenSession(); Session session = sessionManager.getSession(); session.beginTransaction(); Set<PFFolder> rootFolder = PFFolderWithDefaultHeiarchy(); PFFolderSync sync = new PFFolderSync(service); PFPermissions ss = new PFPermissions(); session.save(ss); session.getTransaction().commit(); session.beginTransaction(); for (PFFolder f : rootFolder) { // System.out.println("WHOOOHA"); if (f.getParent() == null && (f.getDocuments() == null || f.getDocuments().isEmpty())) session.save(f); // service.mkdir(f); } session.getTransaction().commit(); // we have to do this again because the database won't properly link existing folders unless the folders in the root directory are both saved to the database and in the foldercache System.out.println("\n\nFinalizing Database - Pass #2\n\n"); this.flushFolderCache(); rootFolder = PFFolderWithDefaultHeiarchy(); session.beginTransaction(); for (PFFolder f : rootFolder) { // System.out.println("WHOOOHA"); session.save(f); sync.saveFolder(f); // service.mkdir(f); } session.getTransaction().commit(); rootFolder = null; System.out.println("FINISHED "); sync.sync(); PFFolder folder = PFLocation.getFolderFromLocation(new PFLocation("/Cash/Index/Index_Store"), this); System.out.println("FOUND FOLDER:"); System.out.println("ID : " + folder.getId()); System.out.println("NAME : " + folder.getName()); // now sync the folders with the filesystem // we can force close the session ONLY becuase we should be doing NOTHING else with the database while creating a new database sessionManager.forceCloseSession(); } public synchronized Set<PFFolder> PFFolderWithDefaultHeiarchy() { Set<PFFolder> defaultFolders = new HashSet<>(); PFFolder documentLibrary = PFFolder.PFFolderFromDatabase(PFFolderConstants.DOCUMENT_FOLDER, this); //PFFolder. (PFFolderConstants.DOCUMENT_FOLDER,this); System.err.println(documentLibrary.getLocation()); PFFolder pluginFolder = PFFolder.PFFolderFromDatabase(PFFolderConstants.PLUGIN_FOLDER, this); System.out.println("FOLDER PLUGIN : " + pluginFolder.getLocation()); PFFolder filterFolder = PFFolder.PFFolderFromDatabase(PFFolderConstants.FILTER_FOLDER, this); PFFolder cacheFolder = PFFolder.PFFolderFromDatabase(PFFolderConstants.CACHE_FOLDER, this); PFFolder cache_index = PFFolder.PFFolderFromDatabase(PFFolderConstants.CACHE_FOLDER_INDEX, this); System.out.println("FOLDER CACHE :" + cache_index.getLocation()); PFFolder cache_plugin = PFFolder.PFFolderFromDatabase(PFFolderConstants.CACHE_FOLDER_INDEX, this); PFFolder cache_recentdocs = PFFolder.PFFolderFromDatabase(PFFolderConstants.CACHE_FOLDER_RECENT, this); defaultFolders.add(documentLibrary); defaultFolders.add(pluginFolder); defaultFolders.add(filterFolder); defaultFolders.add(cacheFolder); defaultFolders.add(cache_index); defaultFolders.add(cache_plugin); defaultFolders.add(cache_recentdocs); return defaultFolders; } public synchronized Set<PFFolder> getRootDirectoriesFromDatabase() { return PFDatabase.getRootDirectoriesFromDatabase(this); } @Deprecated public synchronized static Set<PFFolder> getRootDirectoriesFromDatabase(PFDatabase db) { // finds all the folders with a null parent // if the parent of a proxyfolder is null, that means it is in the root directory return db.getFolderCache(); } /** * Recursively loads all the Folders inside a folder object. This prevents lazy loding exceptions when the session is closed. Does not load Files. * @param folder */ public synchronized void forceLoadFolderTree(PFFolder folder) { // be aware, this method is recursive. Recursive is evil if (folder.getFolders().size() == 0) return; // I know what you are thinking. Why make an expicit return statement if the below statement will implicity return when there are no more children left. // For clarities sake. For clarities sake, my friend. for (PFFolder f : folder.getFolders()) { Hibernate.initialize(f); this.forceLoadFolderTree(f); } } /** * Refreshes the Cache of root folders from the database */ public synchronized void refreshFolderCache() { // Session session = sessionManager.OpenSession(); // if sesion is not already open open it log.info(name + ": Refreshing FolderCache..."); if (folderCache == null) { log.info(name + ": Folder Cache was null, creating new folder cache"); folderCache = new HashSet<>(); } if (sessionManager.getSession() == null) throw new NullPointerException(name + ":The PFDatabase session object was null"); if (sessionManager.getSession().isOpen() == false) throw new RuntimeException(name + ":The Session cannot be used becuase it is closed."); Criteria criteria = sessionManager.getSession().createCriteria(PFFolder.class); criteria.add(Restrictions.isNull("parent")); List<PFFolder> fetchedQuery = criteria.list(); for (PFFolder f : fetchedQuery) { // make sure we don't add any duplicates boolean foundExistingFolder = false; for (PFFolder existingFolder : folderCache) { if (existingFolder.getLocation().toString().equalsIgnoreCase(f.getLocation().toString())) { foundExistingFolder = true; // we found our match, don't need to continue searching break; } } if (foundExistingFolder) continue; // if we already have a folder that exists, don't add it again and create a duplicate folderCache.add(f); log.info(name + ": Added new folder \"" + f.getLocation() + "\" to FolderCache"); } } /** * Empties out the Cache of root folders from the database and reloads everything. */ public synchronized void flushFolderCache() { log.info(name + ": Flushing all entires from FolderCache"); log.info(name + ": Reloading FolderCache..."); folderCache = new HashSet<>(); this.refreshFolderCache(); } public synchronized Set<PFFolder> getFolderCache() { if (folderCache == null) refreshFolderCache(); return folderCache; } }