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/METSFileGroup.java

    r5800 r5945  
    55import java.util.Iterator;
    66
     7import java.net.URL;
     8
     9import java.sql.SQLException;
     10import java.sql.ResultSet;
     11
    712import java.io.PrintWriter;
    813
     
    1318import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
    1419
     20/**
     21 *  Child groups are indicated by colons.  E.g. Chapter1:Section1 would indicate
     22 *  the group "Section1" inside the "Chapter1" group...
     23 */
    1524
    1625public class METSFileGroup
     
    1928  String id;
    2029
     30  public static final String SECTION_PARENT = "Section";
     31  public static final String GROUP_PARENT = "Group";
     32
    2133  public METSFileGroup(String id)
    2234  { this.children = new ArrayList();
     
    2537  }
    2638
    27     /**
    28      *  Add a filegroup to this group of files.
    29      *
    30      *  @param <code>METSFileGroup</code> the filegroup.
    31      */
    32     public void addGroup(METSFileGroup group)
    33     {   this.childGroups.add(group);
    34     }
    35 
    36     /**
    37      *  Add a file to this group of files.
    38      *
    39      *  @param <code>METSFile</code> the file in a METS File object.
    40      */
    41     public void addFile(METSFile file)
    42     {   this.children.add(file);
    43         file.setGroup(this);
    44         if (!file.getID().isDefined())
    45         { file.setID(new METSFileID(this.id + Integer.toString(this.children.size())));
    46         }
    47     }
    48 
    49     /**
    50      *  Get the nth child of the group
    51      *
    52      *  @param <code>int</code> the index into the group
    53      *  @return <code>METSFile</code> the corresponding child.  This may be <code>null</code>
    54      *          particularly if the index given falls outside of the valid range of child
    55      *          indexes.
    56      */
    57     public METSFile getFile(int index)
    58     { if (index < 0 || index >= this.children.size())
    59         {   return null;
    60         }
    61 
    62         return (METSFile) this.children.get(index);
    63     }
    64 
    65     /**
    66      *  Get all the files in the group as a <code>List</code>
    67      */
    68     public List getFiles()
    69     {   return this.children;
    70     }
    71 
    72     /**
    73      *  Get all the subgroups as a <code>List</code>
    74      */
    75     public List getSubgroups()
    76     {   return this.childGroups;
    77     }
    78 
    79     /**
    80      *  Get the number of files in the group
    81      *
    82      *  @return <code>int</code> the count
    83      */
    84     public int noOfFiles()
    85     {   return this.children.size();
    86     }
    87 
    88     /**
    89      *  Get the number of subgroups.
    90      */
    91     public int noOfSubgroups()
    92     {   return this.childGroups.size();
    93     }
    94 
    95     /**
    96      *  @return <code>String</code> the name of this group
    97      */
    98     public String getName()
    99     {   return this.id;
    100     }
     39  /**
     40   *  Find all the occurrences of a given file, and place them
     41   *  in a <code>List</code>.
     42   */
     43  public void findGroups(URL findFile, List resultList)
     44  {
     45    // find in this particular group
     46    Iterator childIter = children.iterator();
     47    while (childIter.hasNext())
     48    { METSFile file = (METSFile) childIter.next();
     49      if (file.equals(findFile))
     50      { resultList.add(this.id);
     51      }
     52    }
     53
     54    // iterate over the child groups
     55    childIter = childGroups.iterator();
     56    while (childIter.hasNext())
     57    { METSFileGroup fileGroup = (METSFileGroup) childIter.next();
     58
     59      fileGroup.findGroups(findFile, resultList);
     60    }
     61  }
     62
     63  /**
     64   *  Get a sub group of this METSFileGroup.
     65   *
     66   *  @param <code>String</code> the name of the subgroup
     67   *  @return <code>METSFileGroup</code> the child group, which will
     68   *          be <code>null</code> if the group is not known.
     69   */
     70  public METSFileGroup getSubgroup(String id)
     71  { String childId;
     72 
     73    int dotAt = id.indexOf(':');
     74    if (dotAt == -1)
     75    { childId = null;
     76    }
     77    else
     78    { childId = id.substring(dotAt+1);
     79      id = id.substring(0, dotAt);
     80    }
     81   
     82    // iterate over the child groups
     83    Iterator childIter = childGroups.iterator();
     84    while (childIter.hasNext())
     85    { METSFileGroup group = (METSFileGroup) childIter.next();
     86   
     87      if (group.id.equals(id)) {
     88    if (childId == null) {
     89      return group;
     90    }
     91    else {
     92      return group.getSubgroup(childId);
     93    }
     94      }
     95    }
     96    return null;
     97  }
     98
     99  /**
     100   *  Add a filegroup to this group of files.
     101   *
     102   *    @param <code>METSFileGroup</code> the filegroup.
     103   */
     104  public void addGroup(METSFileGroup group)
     105  { this.childGroups.add(group);
     106  }
     107
     108  /**
     109   *  Add a file to this group of files.
     110   *
     111   *    @param <code>METSFile</code> the file in a METS File object.
     112   */
     113  public void addFile(METSFile file)
     114  { this.children.add(file);
     115    file.setGroup(this);
     116    if (!file.getID().isDefined())
     117    { file.setID(new METSFileID(this.id + Integer.toString(this.children.size())));
     118    }
     119  }
     120
     121  /**
     122   *  Get the nth child of the group
     123   *
     124   *  @param <code>int</code> the index into the group
     125   *  @return <code>METSFile</code> the corresponding child.  This may be <code>null</code>
     126   *          particularly if the index given falls outside of the valid range of child
     127   *          indexes.
     128   */
     129  public METSFile getFile(int index)
     130  { if (index < 0 || index >= this.children.size())
     131    { return null;
     132    }
     133 
     134    return (METSFile) this.children.get(index);
     135  }
     136
     137  /**
     138   *  Get all the files in the group as a <code>List</code>
     139   *
     140   *  @return <code>List</code> the file childen of the group.
     141   */
     142  public List getFiles()
     143  { return this.children;
     144  }
     145
     146  /**
     147   *  Get all the subgroups as a <code>List</code>
     148   */
     149  public List getSubgroups()
     150  { return this.childGroups;
     151  }
     152
     153  /**
     154   *  Get the number of files in the group
     155   *
     156   *  @return <code>int</code> the count
     157   */
     158  public int noOfFiles()
     159  { return this.children.size();
     160  }
     161
     162  /**
     163   *  Get the number of subgroups.
     164   */
     165  public int noOfSubgroups()
     166  { return this.childGroups.size();
     167  }
     168
     169  /**
     170   *  @return <code>String</code> the name of this group
     171   */
     172  public String getName()
     173  { return this.id;
     174  }
    101175
    102176  /**
     
    118192  }
    119193
    120   public void writeSQL(DocumentInterface document, GS3SQLConnection connection)
    121   { // check if this node is in the
     194  public boolean writeSQL(DocumentInterface document, String parentRef, boolean parentIsSection,
     195              GS3SQLConnection connection)
     196  { int sqlId = -1;
     197
     198    // check if this node is in the database already
    122199    GS3SQLSelect select = new GS3SQLSelect("filegroups");
    123200    select.addField("*");
     
    128205    select.setWhere(where);
    129206
    130     System.out.println(select.toString());
    131207    connection.execute(select.toString());
    132208
    133     GS3SQLInsert insert = new GS3SQLInsert("filegroups");
    134     insert.addValue("DocID", document.getID().toString());
    135     insert.addValue("FileGroupID", this.id);
    136     insert.addValue("ParentGroup", "");
    137     System.out.println("filegroup done");
    138     connection.execute(insert.toString());
     209    ResultSet selectResult = connection.getResultSet();
     210
     211    try {
     212      if (selectResult == null ||
     213      !selectResult.first()) {
     214    GS3SQLInsert insert = new GS3SQLInsert("filegroups");
     215
     216    insert.addValue("DocID", document.getID().toString());
     217    insert.addValue("FileGroupID", this.id);
     218    insert.addValue("ParentRef", parentRef, GS3SQLField.INTEGER_TYPE);
     219    insert.addValue("ParentType", parentIsSection ? SECTION_PARENT : GROUP_PARENT);
     220   
     221    if (!connection.execute(insert.toString())) {
     222      return false;
     223    }
     224      }
     225      else {
     226    // TODO: update the data for this file group...
     227      }
     228    }
     229    catch (SQLException ex)
     230    { System.err.println(ex);
     231      return false;
     232    }
    139233       
    140     System.out.println("filegroup done");
    141 
    142     Iterator childIter = children.iterator();
     234    // get the filegroup reference now
     235    connection.execute(select.toString());
     236   
     237    try {
     238      ResultSet results = connection.getResultSet();
     239      if (results == null) {
     240    return false;
     241      }
     242      results.first();
     243      sqlId = results.getInt("FileGroupRef");
     244    }
     245    catch (SQLException sqlex) {
     246      System.out.println(sqlex);
     247      return false;
     248    }
     249
     250    // iterate over the child groups
     251    Iterator childIter = childGroups.iterator();
     252    while (childIter.hasNext())
     253    { METSFileGroup fileGroup = (METSFileGroup) childIter.next();
     254
     255      if (!fileGroup.writeSQL(document, Integer.toString(sqlId), false, connection))
     256      { return false;
     257      }
     258    }
     259
     260    // iterate over the child files
     261    childIter = children.iterator();
    143262    while (childIter.hasNext())
    144263    { METSFile file = (METSFile) childIter.next();
    145       file.writeSQL(connection);
    146     }
     264   
     265      if (!file.writeSQL(sqlId, connection))
     266      { return false;
     267      }
     268    }
     269
     270    return true;
     271  }
     272
     273  public static METSFileGroup readSQL(DocumentInterface document, GS3SQLConnection connection,
     274                      ResultSet resultSet)
     275  {
     276    try {
     277      String ID         = resultSet.getString("FileGroupID");
     278      String parentType = resultSet.getString("ParentType");
     279      String parentRef  = resultSet.getString("ParentRef");
     280
     281      // create the metadata block object
     282      METSFileGroup group = new METSFileGroup(ID);
     283     
     284      // get its metadata reference to retrieve divisions
     285      int groupRef = resultSet.getInt("FileGroupRef");
     286
     287      // query the database for matching groups which are children of this one
     288      GS3SQLSelect select = new GS3SQLSelect("filegroups");
     289      select.addField("*");
     290      GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ParentRef", "=", Integer.toString(groupRef),
     291                              GS3SQLField.INTEGER_TYPE);
     292      GS3SQLWhere where = new GS3SQLWhere(whereItem);
     293      whereItem = new GS3SQLWhereItem("ParentType", "=", METSFileGroup.GROUP_PARENT);
     294      where.add(whereItem);
     295      select.setWhere(where);
     296
     297      connection.execute(select.toString());
     298
     299      // parse through the child groups
     300      ResultSet childSet = connection.getResultSet();
     301      if (childSet.first()) {
     302    do {
     303      METSFileGroup fileGroup = METSFileGroup.readSQL(document, connection, childSet);
     304      if (fileGroup != null) {
     305        group.addGroup(fileGroup);
     306      }
     307    }
     308    while (childSet.next());
     309      }
     310
     311      // now scan for file members
     312      select = new GS3SQLSelect("files");
     313      select.addField("*");
     314      whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(groupRef),
     315                      GS3SQLField.INTEGER_TYPE);
     316      where = new GS3SQLWhere(whereItem);
     317      select.setWhere(where);
     318      connection.execute(select.toString());
     319
     320      ResultSet childFileSet = connection.getResultSet();
     321      if (childFileSet != null && childFileSet.first()) {
     322    do {
     323      METSFile file = METSFile.readSQL(connection, childFileSet);
     324      if (file != null) {
     325        group.addFile(file);
     326      }
     327    }
     328    while (childFileSet.next());
     329      }
     330
     331      return group;
     332    }
     333    catch (SQLException sqlEx)
     334    { System.out.println(sqlEx);
     335      System.exit(1);
     336    }
     337    return null;
    147338  }
    148339}
Note: See TracChangeset for help on using the changeset viewer.