1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: Shaoqun Wu, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 2006 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU General Public License
|
---|
33 | * along with this program; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 | package org.greenstone.gatherer.gems;
|
---|
38 |
|
---|
39 | import org.w3c.dom.Document;
|
---|
40 | import org.w3c.dom.Element;
|
---|
41 | import org.greenstone.gatherer.util.XMLTools;
|
---|
42 |
|
---|
43 |
|
---|
44 | import java.util.Observable;
|
---|
45 | import java.util.ArrayList;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @author Shaoqun Wu, Greenstone Digital Library, University of Waikato
|
---|
49 | * @version 2.4
|
---|
50 | */
|
---|
51 | public class MetadataSetModel
|
---|
52 | extends Observable implements MetadataSetListener, AttributeListener{
|
---|
53 |
|
---|
54 | private Document metadata_doc;
|
---|
55 |
|
---|
56 | private MetadataSetManager meta_manager;
|
---|
57 |
|
---|
58 | private ArrayList model;
|
---|
59 |
|
---|
60 | private MetadataSetInfo meta_info;
|
---|
61 |
|
---|
62 | private boolean changed = false;
|
---|
63 |
|
---|
64 |
|
---|
65 | //construt a new MetadataSet
|
---|
66 | public MetadataSetModel(MetadataSetManager msm){
|
---|
67 | meta_manager = msm;
|
---|
68 | model = new ArrayList();
|
---|
69 | }
|
---|
70 |
|
---|
71 | public String getCurrentLanguage(){
|
---|
72 | return meta_manager.getCurrentLanguage();
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void metadataSetChanged(MetadataSetEvent mse){
|
---|
76 | if (meta_info !=null) save(true); //save the last metadata set
|
---|
77 |
|
---|
78 | model = new ArrayList();
|
---|
79 | meta_info = mse.getMetadataSetInfo();
|
---|
80 | meta_info.setMetadataSetModel(this);
|
---|
81 | metadata_doc = meta_manager.getMetadataSetDocument(meta_info.getFilePath());
|
---|
82 |
|
---|
83 | constructModel();
|
---|
84 |
|
---|
85 | setChanged();
|
---|
86 | notifyObservers();
|
---|
87 | clearChanged();
|
---|
88 | changed = false;
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void valueChanged(){
|
---|
93 | setChanged();
|
---|
94 | notifyObservers(new Boolean(false));
|
---|
95 | clearChanged();
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | private void constructModel(){
|
---|
100 | if (metadata_doc == null) return;
|
---|
101 | ArrayList elements =XMLTools.getChildElementsByTagName(metadata_doc.getDocumentElement(),GEMSConstants.ELEMENT_ELEMENT);
|
---|
102 | for(int i=0;i<elements.size();i++){
|
---|
103 | Element element = (Element)elements.get(i);
|
---|
104 | model.add(new MetadataElementModel(element,meta_info));
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | public boolean doesChildWithThisNameExist(String name) {
|
---|
109 | for (int i=0; i<model.size(); i++) {
|
---|
110 | if (name.equals(((MetadataElementModel)model.get(i)).getName())) {
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 |
|
---|
117 | public Document getMetadataSetDocument(){
|
---|
118 | return metadata_doc;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public void setMetadataSetDocument(Document doc){
|
---|
122 | metadata_doc = doc;
|
---|
123 | }
|
---|
124 |
|
---|
125 | public MetadataSetManager getMetadataSetManager(){
|
---|
126 | return meta_manager;
|
---|
127 | }
|
---|
128 |
|
---|
129 | public ArrayList getMetadataSetModel(){
|
---|
130 | return model;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | public void addChild(MetadataElementModel element_model){
|
---|
135 | model.add(element_model);
|
---|
136 | changed = true;
|
---|
137 | }
|
---|
138 |
|
---|
139 | public void removeChild(MetadataElementModel element_model){
|
---|
140 | model.remove(element_model);
|
---|
141 | changed = true;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | public void attributeChanged(AttributeEvent ae){
|
---|
146 | changed = true;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public void setChanged(boolean ischanged){
|
---|
150 | changed = ischanged;
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void save(boolean confirm){
|
---|
154 | if ( meta_info == null || (!changed && !meta_info.isNew())) return;
|
---|
155 |
|
---|
156 | meta_info.save();
|
---|
157 |
|
---|
158 | if (metadata_doc !=null){
|
---|
159 | //remove all element elements
|
---|
160 | Element doc_element = metadata_doc.getDocumentElement();
|
---|
161 | ArrayList elements = XMLTools.getChildElementsByTagName(doc_element,GEMSConstants.ELEMENT_ELEMENT);
|
---|
162 | for(int i=0;i< elements.size();i++){
|
---|
163 | Element element = (Element)elements.get(i);
|
---|
164 | doc_element.removeChild(element);
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | for(int i=0;i< model.size();i++){
|
---|
169 | MetadataElementModel element_model = (MetadataElementModel)model.get(i);
|
---|
170 | element_model.save();
|
---|
171 | }
|
---|
172 |
|
---|
173 | meta_manager.save(confirm);
|
---|
174 | changed = false;
|
---|
175 |
|
---|
176 | }
|
---|
177 |
|
---|
178 | public MetadataSetInfo getMetadataSetInfo(){
|
---|
179 | return meta_info;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public void metadataSetDeleted(){
|
---|
183 | meta_info = null;
|
---|
184 | setChanged();
|
---|
185 | notifyObservers();
|
---|
186 | clearChanged();
|
---|
187 | changed = false;
|
---|
188 |
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | public boolean moveUp(MetadataElementModel child){
|
---|
193 | int index = model.indexOf(child);
|
---|
194 | if (index-1 >=0){
|
---|
195 | changed = true;
|
---|
196 | model.remove(child);
|
---|
197 | model.add(index-1,child);
|
---|
198 | return true;
|
---|
199 | }
|
---|
200 |
|
---|
201 | return false;
|
---|
202 | }
|
---|
203 |
|
---|
204 | public boolean moveDown(MetadataElementModel child){
|
---|
205 | int index = model.indexOf(child);
|
---|
206 | if (index+1 < model.size()){
|
---|
207 | changed = true;
|
---|
208 | model.remove(child);
|
---|
209 | model.add(index+1,child);
|
---|
210 | return true;
|
---|
211 | }
|
---|
212 |
|
---|
213 | return false;
|
---|
214 | }
|
---|
215 |
|
---|
216 | }
|
---|