001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.gast.lib;
025:
026: import java.io.File;
027: import java.util.List;
028: import jeeves.resources.dbms.Dbms;
029: import jeeves.utils.Xml;
030: import org.fao.geonet.constants.Geonet;
031: import org.fao.geonet.constants.Params;
032: import org.fao.geonet.kernel.DataManager;
033: import org.fao.geonet.kernel.XmlSerializer;
034: import org.fao.geonet.kernel.search.SearchManager;
035: import org.jdom.Element;
036:
037: //=============================================================================
038:
039: public class MetadataLib {
040: //---------------------------------------------------------------------------
041: //---
042: //--- Constructor
043: //---
044: //---------------------------------------------------------------------------
045:
046: public MetadataLib(String appPath) throws Exception {
047: this .appPath = appPath;
048:
049: // searchMan = new SearchManager(appPath +"/web/geonetwork/", Lib.config.getLuceneDir());
050: }
051:
052: //---------------------------------------------------------------------------
053: //---
054: //--- API methods
055: //---
056: //---------------------------------------------------------------------------
057:
058: public Element getMetadata(Dbms dbms, String id) throws Exception {
059: return XmlSerializer.select(dbms, "Metadata", id);
060: }
061:
062: //---------------------------------------------------------------------------
063:
064: public boolean canConvert(String fromSchema, String toSchema) {
065: String format = fromSchema + "-to-" + toSchema;
066: String path = appPath + "/web/geonetwork/conversion/" + format
067: + "/main.xsl";
068:
069: return new File(path).exists();
070: }
071:
072: //---------------------------------------------------------------------------
073:
074: public Element convert(Element md, String fromSchema,
075: String toSchema) throws Exception {
076: if (!canConvert(fromSchema, toSchema))
077: throw new Exception("Cannot convert to schema :" + toSchema);
078:
079: String format = fromSchema + "-to-" + toSchema;
080: String path = appPath + "/web/geonetwork/conversion/" + format;
081:
082: Element result = Xml.transform(md, path + "/main.xsl");
083: Element unmapped = Xml.transform(md, path + "/unmapped.xsl");
084:
085: Element metadata = new Element("metadata").addContent(result);
086:
087: return new Element("result").addContent(metadata).addContent(
088: unmapped);
089: }
090:
091: //---------------------------------------------------------------------------
092: /** Transactional */
093:
094: public void sync(Dbms dbms) throws Exception {
095: try {
096: List list = dbms.select(
097: "SELECT * FROM Metadata WHERE isTemplate='n'")
098: .getChildren();
099: dbms.commit();
100:
101: String siteURL = Lib.site.getSiteURL(dbms);
102:
103: for (int i = 0; i < list.size(); i++) {
104: Element record = (Element) list.get(i);
105:
106: String id = record.getChildText("id");
107: String schema = record.getChildText("schemaid");
108: String data = record.getChildText("data");
109: String uuid = record.getChildText("uuid");
110: String date = record.getChildText("createdate");
111:
112: Element md = updateFixedInfo(id, Xml.loadString(data,
113: false), uuid, date, schema, siteURL);
114:
115: XmlSerializer.update(dbms, id, md, date);
116: dbms.commit();
117: }
118: } catch (Exception e) {
119: dbms.abort();
120: throw e;
121: }
122: }
123:
124: //--------------------------------------------------------------------------
125:
126: public Element updateFixedInfo(String id, Element md, String uuid,
127: String date, String schema, String siteURL)
128: throws Exception {
129: md.detach();
130:
131: //--- setup environment
132:
133: Element env = new Element("env");
134:
135: env.addContent(new Element("id").setText(id));
136: env.addContent(new Element("uuid").setText(uuid));
137: env.addContent(new Element("changeDate").setText(date));
138: env.addContent(new Element("siteURL").setText(siteURL));
139:
140: //--- setup root element
141:
142: Element root = new Element("root");
143:
144: root.addContent(md);
145: root.addContent(env);
146:
147: //--- do an XSL transformation
148:
149: String styleSheet = appPath + "/web/geonetwork/xml/schemas/"
150: + schema + "/" + Geonet.File.UPDATE_FIXED_INFO;
151:
152: return Xml.transform(root, styleSheet);
153: }
154:
155: //---------------------------------------------------------------------------
156:
157: // public void index(Dbms dbms, String id) throws Exception
158: // {
159: // DataManager.indexMetadata(dbms, id, searchMan);
160: // }
161:
162: //---------------------------------------------------------------------------
163:
164: public void clearIndexes() throws Exception {
165: File dir = new File(appPath + "/web/geonetwork/"
166: + Lib.config.getLuceneDir());
167: Lib.io.cleanDir(dir);
168: }
169:
170: //--------------------------------------------------------------------------
171:
172: public Element getThumbnails(Dbms dbms, String schema, String id)
173: throws Exception {
174: Element md = XmlSerializer.select(dbms, "Metadata", id);
175:
176: if (md == null)
177: return null;
178:
179: //--- do an XSL transformation
180:
181: String styleSheet = appPath + "/web/geonetwork/xml/schemas/"
182: + schema + "/" + Geonet.File.EXTRACT_THUMBNAILS;
183:
184: return Xml.transform(md, styleSheet);
185: }
186:
187: //--------------------------------------------------------------------------
188:
189: public String getDataDir() {
190: String dataDir = Lib.config
191: .getHandlerProp(Geonet.Config.DATA_DIR);
192:
193: if (!new File(dataDir).isAbsolute())
194: dataDir = appPath + "/web/geonetwork/" + dataDir;
195:
196: return dataDir;
197: }
198:
199: //--------------------------------------------------------------------------
200:
201: public String getDir(int id, String access) {
202: String group = pad(id / 100, 3);
203: String groupDir = group + "00-" + group + "99";
204: String subDir = (access != null && access
205: .equals(Params.Access.PUBLIC)) ? Params.Access.PUBLIC
206: : Params.Access.PRIVATE;
207:
208: return getDataDir() + "/" + groupDir + "/" + id + "/" + subDir
209: + "/";
210: }
211:
212: //--------------------------------------------------------------------------
213:
214: public Element setUUID(String schema, String uuid, Element md)
215: throws Exception {
216: //--- setup environment
217:
218: Element env = new Element("env");
219: env.addContent(new Element("uuid").setText(uuid));
220:
221: //--- setup root element
222:
223: Element root = new Element("root");
224: root.addContent(md);
225: root.addContent(env);
226:
227: //--- do an XSL transformation
228:
229: String styleSheet = appPath + "/web/geonetwork/xml/schemas/"
230: + schema + "/" + Geonet.File.SET_UUID;
231:
232: return Xml.transform(root, styleSheet);
233: }
234:
235: //-----------------------------------------------------------------------------
236:
237: public void insertMetadata(Dbms dbms, String schema, Element md,
238: int id, String source, String createDate,
239: String changeDate, String uuid, int owner,
240: String groupOwner, String template, String title)
241: throws Exception {
242: //--- force namespace prefix for iso19139 metadata
243: DataManager.setNamespacePrefix(md);
244:
245: XmlSerializer.insert(dbms, schema, md, id, source, uuid,
246: createDate, changeDate, template, title, owner,
247: groupOwner);
248: }
249:
250: //-----------------------------------------------------------------------------
251: //---
252: //--- Private methods
253: //---
254: //-----------------------------------------------------------------------------
255:
256: private String pad(int group, int lenght) {
257: String text = Integer.toString(group);
258:
259: while (text.length() < lenght)
260: text = "0" + text;
261:
262: return text;
263: }
264:
265: //---------------------------------------------------------------------------
266: //---
267: //--- Variables
268: //---
269: //---------------------------------------------------------------------------
270:
271: private String appPath;
272:
273: private SearchManager searchMan;
274: }
275:
276: //=============================================================================
|