source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSStructureSet.java@ 8742

Last change on this file since 8742 was 8742, checked in by kjdon, 19 years ago

changed the import statements for GS3SQLConnection and GS3SQLConnectionFactory to reflect their move to the database package

  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1package org.greenstone.gsdl3.gs3build.metadata;
2
3import java.io.PrintWriter;
4
5import java.util.HashMap;
6import java.util.Map;
7import java.util.List;
8import java.util.ArrayList;
9import java.util.Iterator;
10
11import java.sql.SQLException;
12import java.sql.ResultSet;
13
14import org.w3c.dom.Element;
15import org.w3c.dom.NodeList;
16
17
18
19import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
20
21import org.greenstone.gsdl3.gs3build.database.*;
22
23public class METSStructureSet
24{
25 Map children;
26
27 public static final String GSDL3_SECTION_STRUCTURE = "Section";
28
29 public METSStructureSet()
30 {
31 this.children = new HashMap();
32 }
33
34 public void addStructure(METSStructure structure)
35 {
36 this.children.put(structure.getID(), structure);
37 }
38
39 public METSStructure getStructure(String name)
40 {
41 return (METSStructure) this.children.get(name);
42 }
43
44 /**
45 * Find the divisions that contain a list of given files
46 *
47 * @param <code>List</code> the list of files to find...
48 * @return <code>List</code> the matching divisions.
49 */
50 public List findDivisionsForFiles(List listOfFileIdentifiers)
51 {
52 List reply = new ArrayList();
53
54 // iterate across all the children...
55 Iterator structures = this.children.values().iterator();
56
57 while (structures.hasNext()){
58 METSStructure structure = (METSStructure) structures.next();
59
60 structure.findDivisionsForFiles(listOfFileIdentifiers, reply);
61 }
62 return reply;
63 }
64
65 /**
66 * Add a division to an indicated structure.
67 *
68 * @param <code>String</code> the structure identifier
69 * @param <code>METSDivision</code> the division to be added.
70 */
71 public void addDivision(String structureId, METSDivision division)
72 {
73 METSStructure structure = (METSStructure) this.children.get(structureId);
74
75 if (structure != null){
76 structure.addDivision(division);
77 }
78 }
79
80 /**
81 * Add a division to an indicated division within structure.
82 *
83 * @param <code>String</code> the structure identifier
84 * @param <code>String</code> the division identifer, within which the division
85 * is to be placed.
86 * @param <code>METSDivision</code> the division to be added.
87 */
88 public void addSubDivision(String structureId, String divisionID, METSDivision division)
89 {
90 METSStructure structure = (METSStructure) this.children.get(structureId);
91
92 if (structure != null){
93 structure.addSubDivision(divisionID, division);
94 }
95 }
96
97 /**
98 * Get a division from within a given structure
99 *
100 * @param <code>String</code> the structure identifier
101 * @param <code>String</code> the division identifer
102 *
103 * @return <code>METSDivision</code> the division
104 */
105 public METSDivision getDivision(String structureId, String divisionId)
106 {
107 METSStructure structure = (METSStructure) this.children.get(structureId);
108
109 if (structure != null){
110 return structure.getDivision(divisionId);
111 }
112 return null;
113 }
114 public static METSStructureSet parseXML(NodeList structMapSecs)
115 {
116 METSStructureSet set = new METSStructureSet();
117
118 for (int c = 0; c < structMapSecs.getLength(); c++) {
119 METSStructure structure = METSStructure.parseXML((Element) structMapSecs.item(c));
120 set.addStructure(structure);
121 }
122 return set;
123 }
124
125
126 public void write(PrintWriter writer)
127 {
128 Iterator structures = this.children.values().iterator();
129
130 while (structures.hasNext()){
131 METSStructure group = (METSStructure) structures.next();
132
133 group.write(writer);
134 }
135 }
136
137 public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection)
138 {
139 Iterator structures = this.children.values().iterator();
140
141 while (structures.hasNext()){
142 METSStructure group = (METSStructure) structures.next();
143
144 if (!group.writeSQL(document, connection)){
145 return false;
146 }
147 }
148 return true;
149 }
150
151 public static METSStructureSet readSQL(DocumentInterface document, GS3SQLConnection connection)
152 {
153 METSStructureSet set = new METSStructureSet();
154
155 GS3SQLSelect select = new GS3SQLSelect("structure");
156 select.addField("*");
157 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("DocID", "=", document.getID().toString());
158 select.setWhere(new GS3SQLWhere(whereItem));
159 connection.execute(select.toString());
160
161 // start going through the matching metadata blocks
162 try {
163 ResultSet resultSet = connection.getResultSet();
164 resultSet.first();
165 do {
166 METSStructure structure = METSStructure.readSQL(document, connection, resultSet);
167 if (structure != null) {
168 set.addStructure(structure);
169 }
170 } while (resultSet.next());
171 }
172 catch (SQLException sqlEx) {
173 System.out.println(sqlEx);
174 System.exit(1);
175 }
176 return set;
177 }
178}
Note: See TracBrowser for help on using the repository browser.