001    /*
002     * $Revision: 15621 $ 
003     * $Date: 2009-07-25 08:32:01 +0200 (Sat, 25 Jul 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.datamodel.ifs2;
025    
026    import java.io.InputStream;
027    import java.util.ArrayList;
028    import java.util.Date;
029    import java.util.Iterator;
030    import java.util.List;
031    
032    import org.mycore.common.MCRConfiguration;
033    import org.mycore.common.MCRConfigurationException;
034    import org.mycore.common.MCRPersistenceException;
035    import org.mycore.datamodel.common.MCRObjectIDDate;
036    import org.mycore.datamodel.common.MCRXMLTableInterface;
037    import org.mycore.datamodel.ifs2.MCRContent;
038    import org.mycore.datamodel.ifs2.MCRMetadataStore;
039    import org.mycore.datamodel.ifs2.MCRStoredMetadata;
040    import org.mycore.datamodel.metadata.MCRObjectID;
041    
042    /**
043     * Stores XML metadata of MCRObject in IFS2
044     */
045    public class MCRObjectMetadataStoreIFS2 
046      implements MCRXMLTableInterface 
047    {
048      private MCRMetadataStore store;
049    
050      private String project;
051      
052      public MCRObjectMetadataStoreIFS2(){}
053    
054     /**
055      * Reads the configuration and initializes the store.
056      * 
057      * MCR.IFS2.Store.{type}.Class=org.mycore.datamodel.ifs2.MCRVersioningMetadataStore
058      * MCR.IFS2.Store.{type}.BaseDir={/path/to/store/directory/}
059      * MCR.IFS2.Store.{type}.SlotLayout=4-2-2
060      * MCR.IFS2.Store.{type}.SVNRepositoryURL=file:///{where/the/svn/store/should/be/created/}
061      * 
062      * @param type the MCRObjectID type
063      */
064      public void init( String type )
065      {
066        if( ( type == null ) || ( ( type = type.trim() ).length() == 0 ) ) 
067          throw new MCRConfigurationException( "Trying to init object metadata store with empty type ID" );
068    
069        MCRConfiguration config = MCRConfiguration.instance();
070    
071        boolean defined = config.getBoolean( "MCR.Metadata.Type." + type, false );
072        if( ! defined ) 
073          throw new MCRConfigurationException( "The configuration property MCR.Metadata.Type." + type + " is not defined!" );
074    
075        this.store = MCRMetadataStore.getStore(type);
076        this.project = config.getString("MCR.SWF.Project.ID");
077        this.project = config.getString("MCR.SWF.Project.ID." + type, this.project );
078      }
079    
080      public synchronized void create( String mcrid, byte[] xml, Date lastModified )
081      {
082        int ID = new MCRObjectID( mcrid ).getNumberAsInteger();
083        try
084        {
085          MCRStoredMetadata sm = store.create( MCRContent.readFrom( xml ), ID );
086          sm.setLastModified( lastModified );
087        }
088        catch( Exception ex )
089        { throw new MCRPersistenceException( "Exception storing XML of " + mcrid, ex ); }
090      }
091    
092      public synchronized void delete( String mcrid )
093      {
094        int ID = new MCRObjectID( mcrid ).getNumberAsInteger();
095        try
096        {
097          store.delete( ID );
098        }
099        catch( Exception ex )
100        { throw new MCRPersistenceException( "Exception deleting stored XML of " + mcrid, ex ); }
101      }
102    
103      public synchronized void update( String mcrid, byte[] xml, Date lastModified ) 
104      {
105        int ID = new MCRObjectID( mcrid ).getNumberAsInteger();
106        try
107        {
108          MCRStoredMetadata sm = store.retrieve( ID );
109          sm.update( MCRContent.readFrom( xml ) );
110        }
111        catch( Exception ex )
112        { throw new MCRPersistenceException( "Exception updating XML of " + mcrid, ex ); }
113      }
114    
115      public InputStream retrieve( String mcrid ) 
116      {
117        int ID = new MCRObjectID( mcrid ).getNumberAsInteger();
118        try
119        {
120          MCRStoredMetadata sm = store.retrieve( ID );
121          return sm.getMetadata().getInputStream();
122        }
123        catch( Exception ex )
124        { throw new MCRPersistenceException( "Exception retrieving XML of " + mcrid, ex ); }
125      }
126    
127      public synchronized int getHighestStoredID()
128      {
129        return store.getHighestStoredID();
130      }
131    
132      public boolean exists( String mcrid ) 
133      {
134        int ID = new MCRObjectID( mcrid ).getNumberAsInteger();
135        try
136        {
137          return store.exists( ID );
138        }
139        catch( Exception ex )
140        { throw new MCRPersistenceException( "Exception checking existence of " + mcrid, ex ); }
141      }
142    
143      public List<String> retrieveAllIDs() 
144      { 
145        Iterator<Integer> IDs = store.listIDs( MCRMetadataStore.ASCENDING );
146        List<String> list = new ArrayList<String>();
147        MCRObjectID oid = new MCRObjectID( project + "_" + store.getID() + "_1" );
148        while( IDs.hasNext() )
149        {
150          oid.setNumber( IDs.next() );
151          list.add( oid.getId() );
152        }
153        return list;
154      }
155    
156      public List<MCRObjectIDDate> listObjectDates( String type ) 
157      {
158        Iterator<Integer> IDs = store.listIDs( MCRMetadataStore.ASCENDING );
159        List<MCRObjectIDDate> list = new ArrayList<MCRObjectIDDate>();
160        MCRObjectID oid = new MCRObjectID( project + "_" + store.getID() + "_1" );
161        while( IDs.hasNext() )
162        {
163          int ID = IDs.next();
164          oid.setNumber( ID );
165          try
166          {
167            MCRStoredMetadata sm = store.retrieve( ID );
168            list.add( new MyMCRObjectIDDate( oid.getId(), sm.getLastModified() ) );
169          }
170          catch( Exception ex )
171          { throw new MCRPersistenceException( "Exception retrieving data of " + ID, ex ); }
172        }
173    
174        return list;
175      }
176       
177      class MyMCRObjectIDDate implements MCRObjectIDDate
178      {
179        String oid;
180        Date date;
181        
182        MyMCRObjectIDDate( String oid, Date lastModified )
183        {
184          this.oid = oid;
185          this.date = lastModified;
186        }
187        
188        public String getId()
189        { return oid; }
190        
191        public Date getLastModified()
192        { return date; }
193      }
194    }