Ignore:
Timestamp:
2003-11-24T14:26:53+13:00 (20 years ago)
Author:
cs025
Message:

Extensive additions to metadata

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFileSet.java

    r5800 r5945  
    88import java.util.Map;
    99import java.util.HashMap;
     10import java.util.List;
     11import java.util.ArrayList;
    1012import java.util.Iterator;
    1113
    1214import java.io.File;
    1315import java.io.PrintWriter;
     16
     17import java.sql.SQLException;
     18import java.sql.ResultSet;
     19
    1420import java.net.URL;
    1521
     
    2026
    2127public class METSFileSet
    22 { Map fileGroups;
     28{ Map    fileGroups;
     29  String reference;
    2330
    2431  public METSFileSet()
    2532  { this.fileGroups = new HashMap();
    2633    this.fileGroups.put("default", new METSFileGroup("default"));
    27   }
    28 
     34    this.reference = null;
     35  }
     36 
     37  /**
     38   *  Get the group that corresponds to the given name...
     39   *
     40   *  @param <code>String</code> the name of the group.
     41   *  @return <code>METSFileGroup</code> the group object - this will be
     42   *          <code>null</code> if the group is not found
     43   */
    2944  public METSFileGroup getGroup(String name)
    3045  { if (!this.fileGroups.containsKey(name))
     
    3449  }
    3550
     51  /**
     52   *  Get the Nth file from the default group...
     53   *
     54   *  @param <code>int</code> the index into the default group to use...
     55   *  @return <code>METSFile</code> the file.
     56   */
    3657  public METSFile getFile(int index)
    3758  { METSFileGroup group = this.getGroup("default");
     
    7091  }
    7192
     93  /**
     94   *  Add a group at the top level of the METS File set.
     95   *
     96   *  @param <code>METSFileGroup</code> the file group to add.
     97   */
    7298  public void addGroup(METSFileGroup group)
    7399  { this.fileGroups.put(group.getName(), group);
     100  }
     101
     102  /**
     103   *  Get all the groups that contain a given URL/file.
     104   *
     105   *  @param <code>URL</code> the location of the file.
     106   *  @return <code>List</code> the list of group references that contain the file.
     107   */
     108  public List findGroups(URL file)
     109  { List resultList = new ArrayList();
     110
     111    Iterator groups = this.fileGroups.values().iterator();
     112
     113    while (groups.hasNext())
     114    { METSFileGroup group = (METSFileGroup) groups.next();
     115     
     116      group.findGroups(file, resultList);
     117    }
     118    return resultList;
    74119  }
    75120
     
    86131  }
    87132
    88   public void writeSQL(DocumentInterface document, GS3SQLConnection connection)
     133  public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
    89134  { Iterator groups = this.fileGroups.values().iterator();
    90135
    91     GS3SQLInsert insert = new GS3SQLInsert("filesection");
    92     insert.addValue("DocID", document.getID().toString());
    93     insert.addValue("FileSecID", "test"); // TODO: remove magic string
    94 
    95     connection.execute(insert.toString());
     136    // write the set if the reference does not already exist...
     137    if (this.reference == null)
     138    { // Insert the file section into the database
     139      GS3SQLInsert insert = new GS3SQLInsert("filesection");
     140      insert.addValue("DocID", document.getID().toString());
     141      insert.addValue("FileSecID", "test"); // TODO: remove magic string
     142
     143      if (!connection.execute(insert.toString()))
     144      { return false;
     145      }
     146
     147      // find the file section number
     148      GS3SQLSelect select = new GS3SQLSelect("filesection");
     149      select.addField("*");
     150      GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileSecID", "=", "test");
     151      GS3SQLWhereItem whereDoc  = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     152      GS3SQLWhere where = new GS3SQLWhere(whereItem);
     153      where.add(whereDoc);
     154      select.setWhere(where);
     155
     156      try {
     157    connection.execute(select.toString());
     158   
     159    ResultSet set = connection.getResultSet();
     160    set.first();
     161    int sectionRef = set.getInt("FileSectionRef");
     162
     163    this.reference = Integer.toString(sectionRef);
     164      }
     165      catch (SQLException ex)
     166      { System.out.println(ex);
     167        return false;
     168      }
     169    }
    96170   
     171    // write out the children
    97172    while (groups.hasNext())
    98173    { METSFileGroup group = (METSFileGroup) groups.next();
    99 
    100       group.writeSQL(document, connection);
    101     }
    102   }
     174     
     175      if (!group.writeSQL(document, this.reference, true, connection))
     176      { return false;
     177      }
     178    }
     179
     180    return true;
     181  }
     182
     183  public static METSFileSet readSQL(DocumentInterface document, GS3SQLConnection connection)
     184  {
     185    METSFileSet set = new METSFileSet();   
     186
     187    // Get file sections from the filesection table (currently redundant)
     188    GS3SQLSelect select = new GS3SQLSelect("filesection");
     189    select.addField("*");
     190    GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
     191    select.setWhere(new GS3SQLWhere(whereItem));
     192    connection.execute(select.toString());
     193   
     194    // Get the identifier for this file set, etc.
     195    ResultSet sections = connection.getResultSet();
     196    int fileSetRef;
     197    try {
     198      sections.first();
     199      fileSetRef     = sections.getInt("FileSectionRef");
     200      set.reference = Integer.toString(fileSetRef);
     201    }
     202    catch (SQLException ex)
     203    { System.out.println(ex);
     204      return null;
     205    }
     206   
     207    // Get child file groups
     208    select = new GS3SQLSelect("filegroups");
     209    select.addField("*");
     210    whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());   
     211    GS3SQLWhere where = new GS3SQLWhere(whereItem);
     212    whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(fileSetRef),
     213                    GS3SQLField.INTEGER_TYPE);
     214    where.add(whereItem);
     215    whereItem = new GS3SQLWhereItem("ParentType", "=", METSFileGroup.SECTION_PARENT);
     216    where.add(whereItem);
     217    select.setWhere(where);
     218    connection.execute(select.toString());
     219
     220    // start going through the matching file groups
     221    try {
     222      ResultSet resultSet = connection.getResultSet();
     223      resultSet.first();
     224      do {
     225    METSFileGroup filegroup = METSFileGroup.readSQL(document, connection, resultSet);
     226    if (filegroup != null) {
     227      set.addGroup(filegroup);
     228    }
     229      } while (resultSet.next());
     230    }
     231    catch (SQLException sqlEx) {
     232      System.out.println(sqlEx);
     233      System.exit(1);
     234    }
     235
     236    return set;
     237  }
     238
    103239}
Note: See TracChangeset for help on using the changeset viewer.