001    /*
002     * 
003     * $Revision: 14987 $ $Date: 2009-03-20 22:10:57 +0100 (Fri, 20 Mar 2009) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.backend.hibernate;
025    
026    import java.sql.Timestamp;
027    import java.util.GregorianCalendar;
028    import java.util.List;
029    import java.util.Vector;
030    
031    import org.apache.log4j.Logger;
032    import org.hibernate.Criteria;
033    import org.hibernate.Session;
034    import org.hibernate.criterion.Restrictions;
035    import org.mycore.backend.hibernate.tables.MCRFSNODES;
036    import org.mycore.common.MCRPersistenceException;
037    import org.mycore.datamodel.ifs.MCRDirectory;
038    import org.mycore.datamodel.ifs.MCRFile;
039    import org.mycore.datamodel.ifs.MCRFileMetadataManager;
040    import org.mycore.datamodel.ifs.MCRFileMetadataStore;
041    import org.mycore.datamodel.ifs.MCRFilesystemNode;
042    
043    /**
044     * This class implements the MCRFileMetadataStore.
045     * 
046     */
047    public class MCRHIBFileMetadataStore implements MCRFileMetadataStore {
048        protected String table;
049    
050        // logger
051        static Logger logger = Logger.getLogger(MCRHIBLinkTableStore.class.getName());
052    
053        private Session getSession() {
054            return MCRHIBConnection.instance().getSession();
055        }
056    
057        public MCRHIBFileMetadataStore() throws MCRPersistenceException {
058        }
059    
060        public void storeNode(MCRFilesystemNode node) throws MCRPersistenceException {
061    
062            String ID = node.getID();
063            String PID = node.getParentID();
064            String OWNER = node.getOwnerID();
065            String NAME = node.getName();
066            String LABEL = node.getLabel();
067            long SIZE = node.getSize();
068    
069            GregorianCalendar DATE = node.getLastModified();
070    
071            String TYPE = null;
072            String STOREID = null;
073            String STORAGEID = null;
074            String FCTID = null;
075            String MD5 = null;
076    
077            int NUMCHDD = 0;
078            int NUMCHDF = 0;
079            int NUMCHTD = 0;
080            int NUMCHTF = 0;
081    
082            if (node instanceof MCRFile) {
083                MCRFile file = (MCRFile) node;
084    
085                TYPE = "F";
086                STOREID = file.getStoreID();
087                STORAGEID = file.getStorageID();
088                FCTID = file.getContentTypeID();
089                MD5 = file.getMD5();
090            } else if (node instanceof MCRDirectory) {
091                MCRDirectory dir = (MCRDirectory) node;
092    
093                TYPE = "D";
094                NUMCHDD = dir.getNumChildren(MCRDirectory.DIRECTORIES, MCRDirectory.HERE);
095                NUMCHDF = dir.getNumChildren(MCRDirectory.FILES, MCRDirectory.HERE);
096                NUMCHTD = dir.getNumChildren(MCRDirectory.DIRECTORIES, MCRDirectory.TOTAL);
097                NUMCHTF = dir.getNumChildren(MCRDirectory.FILES, MCRDirectory.TOTAL);
098            } else {
099                throw new MCRPersistenceException("MCRFilesystemNode must be either MCRFile or MCRDirectory");
100            }
101    
102            Session session = getSession();
103            MCRFSNODES fs = (MCRFSNODES) session.get(MCRFSNODES.class, ID);
104            if (fs == null) {
105                fs = new MCRFSNODES();
106                fs.setId(ID);
107            }
108            fs.setPid(PID);
109            fs.setType(TYPE);
110            fs.setOwner(OWNER);
111            fs.setName(NAME);
112            fs.setLabel(LABEL);
113            fs.setSize(SIZE);
114            fs.setDate(new Timestamp(DATE.getTime().getTime()));
115            fs.setStoreid(STOREID);
116            fs.setStorageid(STORAGEID);
117            fs.setFctid(FCTID);
118            fs.setMd5(MD5);
119            fs.setNumchdd(NUMCHDD);
120            fs.setNumchdf(NUMCHDF);
121            fs.setNumchtd(NUMCHTD);
122            fs.setNumchtf(NUMCHTF);
123            session.saveOrUpdate(fs);
124        }
125    
126        @SuppressWarnings("unchecked")
127        public String retrieveRootNodeID(String ownerID) throws MCRPersistenceException {
128            Session session = getSession();
129            if (ownerID.equals("imgCache")) {
130                //TODO: use projection to retrieve ID
131                StringBuffer sb = (new StringBuffer("from MCRFSNODES where OWNER = '")).append(ownerID).append("' and PID is NULL");
132                List<MCRFSNODES> l = session.createQuery(sb.toString()).list();
133                if (l.size() < 1) {
134                    logger.warn("There is no fsnode with OWNER = " + ownerID);
135                    return null;
136                }
137                return l.get(0).getId();
138            } else {
139                StringBuffer sb = (new StringBuffer("select id from MCRFSNODES where OWNER = '")).append(ownerID).append("' and PID is NULL");
140                String nodeID = (String) session.createQuery(sb.toString()).uniqueResult();
141                if (nodeID == null) {
142                    logger.warn("There is no fsnode with OWNER = " + ownerID);
143                    return null;
144                }
145                return nodeID;
146            }
147        }
148    
149        public MCRFilesystemNode retrieveChild(String parentID, String name) {
150            Session session = getSession();
151            Criteria c = session.createCriteria(MCRFSNODES.class);
152            c.add(Restrictions.eq("pid", parentID));
153            c.add(Restrictions.eq("name", name));
154            MCRFSNODES node = (MCRFSNODES) c.uniqueResult();
155    
156            if (node == null) {
157                return null;
158            }
159    
160            return buildNode(node);
161        }
162    
163        @SuppressWarnings("unchecked")
164        public Vector<String> retrieveChildrenIDs(String parentID) throws MCRPersistenceException {
165            Session session = getSession();
166            List<MCRFSNODES> l = session.createQuery("from MCRFSNODES where PID = '" + parentID + "'").list();
167    
168            if (l.size() == 0) {
169                return new Vector<String>();
170            }
171    
172            Vector<String> v = new Vector<String>(l.size());
173    
174            for (int t = 0; t < l.size(); t++) {
175                v.add(t, l.get(t).getId());
176            }
177    
178            return v;
179        }
180    
181        public void deleteNode(String ID) throws MCRPersistenceException {
182            Session session = getSession();
183            session.delete(session.get(MCRFSNODES.class, ID));
184        }
185    
186        public MCRFilesystemNode retrieveNode(String ID) throws MCRPersistenceException {
187            Session session = getSession();
188            MCRFSNODES node = (MCRFSNODES) session.get(MCRFSNODES.class, ID);
189            if (node == null) {
190                logger.warn("There is no FSNODE with ID = " + ID);
191                return null;
192            }
193    
194            return buildNode(node);
195        }
196    
197        public MCRFilesystemNode buildNode(MCRFSNODES node) {
198            GregorianCalendar greg = new GregorianCalendar();
199            greg.setTime(node.getDate());
200    
201            return MCRFileMetadataManager.instance().buildNode(node.getType(), node.getId(), node.getPid(), node.getOwner(), node.getName(), node.getLabel(),
202                    node.getSize(), greg, node.getStoreid(), node.getStorageid(), node.getFctid(), node.getMd5(), node.getNumchdd(), node.getNumchdf(),
203                    node.getNumchtd(), node.getNumchtf());
204        }
205    }