source: trunk/gli/src/org/greenstone/gatherer/msm/MSMProfiler.java@ 6318

Last change on this file since 6318 was 6094, checked in by jmt12, 21 years ago

Fixed crappy 'how do I import IGNORE?' problem

  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
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: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 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 */
37package org.greenstone.gatherer.msm;
38
39import java.io.*;
40import java.util.*;
41import org.greenstone.gatherer.Gatherer;
42import org.greenstone.gatherer.msm.MSMAdapter;
43import org.greenstone.gatherer.util.HashMap3D;
44import org.greenstone.gatherer.util.Utility;
45import org.w3c.dom.*;
46
47/** This class is essentially records what actions a user took when merging two metadata sets, so we can avoid having to get the user to repeat the proceedure.
48 * @author John Thompson, Greenstone Digital Library, University of Waikato
49 * @version 2.2
50 */
51public class MSMProfiler
52 extends MSMAdapter {
53 /** The file the profile should be loaded from and saved to. */
54 private File profile_file = null;
55 /** A mapping of known profile actions. */
56 private HashMap3D profiles = null;
57 static final private String IGNORE = "IGNORE";
58 /** The constructor loads the previous profile for this collection if there is one.
59 * @see org.greenstone.gatherer.collection.CollectionManager
60 * @see org.greenstone.gatherer.msm.MetadataSetManager
61 */
62 public MSMProfiler() {
63 this.profile_file = new File(Gatherer.c_man.getCollectionMetadata(), "profile.xml");
64 this.profiles = new HashMap3D();
65 // Load an existing profile if there is one.
66 if(profile_file.exists()) {
67 Document document = Utility.parse(profile_file, false);
68 if(document != null) {
69 load(document);
70 document = null;
71 }
72 }
73 }
74 /** Adds a new action mapping to the profile. Such a mapping records that for a certain collection (file) and metadata element a specific action must occur. The action is either a renaming to a new metadata element name, also provided, or an instruction to ignore this particular metadata element, is the target is null.
75 * @param collection_file The aboslute path name to the collection where the metadata was sourced, as a <strong>String</strong>.
76 * @param source A <strong>String</strong> containing the fully qualified name of the source metadata element.
77 * @param target Another <strong>String</strong> which is either the fully qualified name of the target element, or <i>null</i> if this is actually an ignore action addition.
78 */
79 public void addAction(String collection_file, String source, String target) {
80 if(target == null) {
81 target = IGNORE;
82 }
83 profiles.put(collection_file, source, target);
84 }
85
86 public void addSource(String collection_file) {
87 profiles.put(collection_file, new HashMap());
88 }
89
90 /** Determine if an action exists for the given collection and source.
91 * @param collection_file The aboslute path name to the collection where the metadata was sourced, as a <strong>String</strong>.
92 * @param source A <strong>String</strong> containing the fully qualified name of the source metadata element.
93 * @return <i>true</i> if such an action exists, <i>false</i> otherwise.
94 */
95 public boolean containsAction(String collection_file, String source) {
96 return profiles.contains(collection_file, source);
97 }
98
99 public boolean containsSource(String collection_file) {
100 return profiles.containsKey(collection_file);
101 }
102
103 /** Destructor to ensure that no memory leaks. */
104 public void destroy() {
105 //save();
106 profiles.clear();
107 profile_file = null;
108 profiles = null;
109 }
110 /** Method that is called whenever an element within a set is changed or modified, in which case we must modify the action profiles to suit. If an element was removed, remove all action profiles with this element as the target. If an elements name changes, update all matching targets to reflect new name.
111 * @param event A <strong>MSMEvent</strong> containing details of the event that caused this message to be fired.
112 */
113 public void elementChanged(MSMEvent event) {
114 Gatherer.println("Element changed: " + event);
115 String new_name = null;
116 String old_name = event.getValue();
117 ElementWrapper element = event.getElement();
118 boolean delete = false;
119 boolean rename = false;
120 // First we check if the element has been removed.
121 if(element == null) {
122 delete = true;
123 }
124 // Next we determine if its name has changed
125 else if(!(new_name = element.toString()).equals(old_name)) {
126 rename = true;
127 }
128 // Perform any action we need to.
129 if(delete || rename) {
130 Iterator iterator_value_one = profiles.values().iterator();
131 while(iterator_value_one.hasNext()) {
132 HashMap map = (HashMap) iterator_value_one.next();
133 Iterator iterator_key_two = map.keySet().iterator();
134 while(iterator_key_two.hasNext()) {
135 String key_two = (String) iterator_key_two.next();
136 String value = (String) map.get(key_two);
137 if(value.equals(old_name)) {
138 if(delete) {
139 map.remove(key_two);
140 }
141 else {
142 map.put(key_two, new_name);
143 }
144 }
145 key_two = null;
146 value = null;
147 }
148 map = null;
149 iterator_key_two = null;
150 }
151 iterator_value_one = null;
152 }
153 // Otherwise nothing for us to do (must be an add, or possibly a move if I can ever get round to the editor).
154 element = null;
155 old_name = null;
156 new_name = null;
157 }
158 /** Search the profilerer for any previous actions regarding the indicated collection and metadata element.
159 * @param collection_file The absolute path name to the collection where the metadata was sourced, as a <strong>String</strong>.
160 * @param source A <strong>String</strong> containing the fully qualified name of the source metadata element.
161 * @return The fully qualified name of the target metadata element, as a <strong>String</strong>, or <i>null</i> if we are to ignore this metadata. Note that the target elements name may be exactly the source elements one.
162 */
163 public String getAction(String collection_file, String source) {
164 String result = (String) profiles.get(collection_file, source);
165 ///ystem.err.println("getAction() == " + result);
166 if(result.equals(IGNORE)) {
167 result = null;
168 }
169 return result;
170 }
171
172 public HashMap getActions(String collection_file) {
173 return (HashMap) profiles.get(collection_file);
174 }
175
176 public ArrayList getCollections() {
177 return new ArrayList(profiles.keySet());
178 }
179
180 public ArrayList getSources(String collection_file) {
181 ArrayList result = new ArrayList();
182 HashMap map = (HashMap) profiles.get(collection_file);
183 if(map != null) {
184 result.addAll(map.keySet());
185 }
186 return result;
187 }
188
189 public void removeProfile(String collection_file) {
190 profiles.remove(collection_file);
191 }
192
193 public void removeAction(String collection_file, String source) {
194 HashMap map = (HashMap) profiles.get(collection_file);
195 if(map != null) {
196 map.remove(source);
197 }
198 }
199
200 /** Save the profile document. */
201 public void save() {
202 // While we're at it save the profiles.
203 try {
204 // Create backup.
205 if(profile_file.exists()) {
206 File backup = new File(profile_file.getAbsolutePath() + "~");
207 backup.deleteOnExit();
208 if(!profile_file.renameTo(backup)) {
209 Gatherer.println("Error in MSMProfiler.save(): FileRenamedException");
210 }
211 backup = null;
212 }
213
214 // read in the default profile file - has all the dtd in it
215 Document document = Utility.parse(Utility.PROFILE_TEMPLATE, true);
216 if(document == null) {
217 Gatherer.println("Error in MSMProfiler.save(): couldn't find and parse the profile template file!");
218 return;
219 }
220
221
222 // Add the current profile info into the document
223 Element profile_elem = document.getDocumentElement();
224
225 Iterator iterator_key_one = profiles.keySet().iterator();
226 while(iterator_key_one.hasNext()) {
227 String key_one = (String) iterator_key_one.next();
228 HashMap map = (HashMap) profiles.get(key_one);
229 Iterator iterator_key_two = map.keySet().iterator();
230 while(iterator_key_two.hasNext()) {
231 String key_two = (String) iterator_key_two.next();
232 String value = (String) map.get(key_two);
233 Element action = document.createElement("Action");
234 profile_elem.appendChild(action);
235 action.setAttribute("collection", key_one);
236 action.setAttribute("source", key_two);
237 action.setAttribute("target", value);
238 key_two = null;
239 value = null;
240 }
241 key_one = null;
242 map = null;
243 iterator_key_two = null;
244 }
245 iterator_key_one = null;
246
247 // Now write it to file.
248 Utility.export(document, profile_file);
249 }
250 catch (Exception error) {
251 Gatherer.printStackTrace(error);
252 }
253 }
254 /** Reload the mapping information stored in this document.
255 * @param document The <strong>Document</strong> to parse.
256 */
257 private void load(Document document) {
258 Element root = document.getDocumentElement();
259 NodeList actions = root.getElementsByTagName("Action");
260 for(int i = 0; i < actions.getLength(); i++) {
261 Element action = (Element) actions.item(i);
262 String collection = action.getAttribute("collection");
263 String source = action.getAttribute("source");
264 String target = action.getAttribute("target");
265 if(collection.length() > 0 && source.length() > 0) {
266 if(target.length() == 0) {
267 target = null;
268 }
269 profiles.put(collection, source, target);
270 }
271 action = null;
272 collection = null;
273 source = null;
274 target = null;
275 }
276 root = null;
277 actions = null;
278 }
279}
Note: See TracBrowser for help on using the repository browser.