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