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.geonet.kernel.mef;
025:
026: import java.io.File;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.UUID;
033: import jeeves.resources.dbms.Dbms;
034: import jeeves.server.context.ServiceContext;
035: import jeeves.utils.BinaryFile;
036: import jeeves.utils.Log;
037: import jeeves.utils.Xml;
038: import org.fao.geonet.GeonetContext;
039: import org.fao.geonet.constants.Geonet;
040: import org.fao.geonet.kernel.AccessManager;
041: import org.fao.geonet.kernel.DataManager;
042: import org.fao.geonet.lib.Lib;
043: import org.fao.geonet.util.ISODate;
044: import org.jdom.Element;
045:
046: import static org.fao.geonet.kernel.mef.MEFConstants.*;
047:
048: //=============================================================================
049:
050: class Importer {
051: //--------------------------------------------------------------------------
052: //---
053: //--- API methods
054: //---
055: //--------------------------------------------------------------------------
056:
057: public static int doImport(final ServiceContext context,
058: File mefFile) throws Exception {
059: final GeonetContext gc = (GeonetContext) context
060: .getHandlerContext(Geonet.CONTEXT_NAME);
061: final DataManager dm = gc.getDataManager();
062:
063: final Dbms dbms = (Dbms) context.getResourceManager().open(
064: Geonet.Res.MAIN_DB);
065:
066: final String id[] = { "" };
067: final Element md[] = { null };
068:
069: //--- import metadata from MEF file
070:
071: MEFLib.visit(mefFile, new MEFVisitor() {
072: public void handleMetadata(Element metadata)
073: throws Exception {
074: Log.debug(Geonet.MEF, "Collecting metadata:\n"
075: + Xml.getString(metadata));
076: md[0] = metadata;
077: }
078:
079: //--------------------------------------------------------------------
080:
081: public void handleInfo(Element info) throws Exception {
082: Element general = info.getChild("general");
083:
084: String uuid = general.getChildText("uuid");
085: String createDate = general.getChildText("createDate");
086: String changeDate = general.getChildText("changeDate");
087: String source = general.getChildText("siteId");
088: String sourceName = general.getChildText("siteName");
089: String schema = general.getChildText("schema");
090: String isTemplate = general.getChildText("isTemplate")
091: .equals("true") ? "y" : "n";
092: String rating = general.getChildText("rating");
093: String popularity = general.getChildText("popularity");
094:
095: boolean dcore = schema.equals("dublin-core");
096: boolean fgdc = schema.equals("fgdc-std");
097: boolean iso115 = schema.equals("iso19115");
098: boolean iso139 = schema.equals("iso19139");
099:
100: if (!dcore && !fgdc && !iso115 && !iso139)
101: throw new Exception("Unknown schema format : "
102: + schema);
103:
104: if (uuid == null) {
105: uuid = UUID.randomUUID().toString();
106: source = null;
107:
108: //--- set uuid inside metadata
109: md[0] = dm.setUUID(schema, uuid, md[0]);
110: } else {
111: if (sourceName == null)
112: sourceName = "???";
113:
114: Lib.sources.update(dbms, source, sourceName, true);
115: }
116:
117: Log.debug(Geonet.MEF, "Adding metadata with uuid="
118: + uuid);
119:
120: id[0] = dm.insertMetadataExt(dbms, schema, md[0],
121: context.getSerialFactory(), source, createDate,
122: changeDate, uuid, context.getUserSession()
123: .getUserIdAsInt(), null);
124:
125: int iId = Integer.parseInt(id[0]);
126:
127: if (rating != null)
128: dbms.execute(
129: "UPDATE Metadata SET rating=? WHERE id=?",
130: new Integer(rating), iId);
131:
132: if (popularity != null)
133: dbms
134: .execute(
135: "UPDATE Metadata SET popularity=? WHERE id=?",
136: new Integer(popularity), iId);
137:
138: dm.setTemplate(dbms, iId, isTemplate, null);
139: dm.setHarvested(dbms, iId, null);
140:
141: String pubDir = Lib.resource.getDir(context, "public",
142: id[0]);
143: String priDir = Lib.resource.getDir(context, "private",
144: id[0]);
145:
146: new File(pubDir).mkdirs();
147: new File(priDir).mkdirs();
148:
149: addCategories(dm, dbms, id[0], info
150: .getChild("categories"));
151: addPrivileges(dm, dbms, id[0], info
152: .getChild("privileges"));
153: dm.indexMetadata(dbms, id[0]);
154: }
155:
156: //--------------------------------------------------------------------
157:
158: public void handlePublicFile(String file,
159: String changeDate, InputStream is)
160: throws IOException {
161: Log.debug(Geonet.MEF, "Adding public file with name="
162: + file);
163: saveFile(context, id[0], "public", file, changeDate, is);
164: }
165:
166: //--------------------------------------------------------------------
167:
168: public void handlePrivateFile(String file,
169: String changeDate, InputStream is)
170: throws IOException {
171: Log.debug(Geonet.MEF, "Adding private file with name="
172: + file);
173: saveFile(context, id[0], "private", file, changeDate,
174: is);
175: }
176: });
177:
178: return Integer.parseInt(id[0]);
179: }
180:
181: //--------------------------------------------------------------------------
182:
183: private static void saveFile(ServiceContext context, String id,
184: String access, String file, String changeDate,
185: InputStream is) throws IOException {
186: String dir = Lib.resource.getDir(context, access, id);
187:
188: File outFile = new File(dir, file);
189: FileOutputStream os = new FileOutputStream(outFile);
190: BinaryFile.copy(is, os, false, true);
191:
192: outFile
193: .setLastModified(new ISODate(changeDate).getSeconds() * 1000);
194: }
195:
196: //--------------------------------------------------------------------------
197: //--- Categories
198: //--------------------------------------------------------------------------
199:
200: private static void addCategories(DataManager dm, Dbms dbms,
201: String id, Element categ) throws Exception {
202: List locCats = dbms.select("SELECT id,name FROM Categories")
203: .getChildren();
204: List list = categ.getChildren("category");
205:
206: for (Iterator j = list.iterator(); j.hasNext();) {
207: String catName = ((Element) j.next())
208: .getAttributeValue("name");
209: String catId = mapLocalEntity(locCats, catName);
210:
211: if (catId == null)
212: Log.debug(Geonet.MEF,
213: " - Skipping inesistent category : " + catName);
214: else {
215: //--- metadata category exists locally
216:
217: Log.debug(Geonet.MEF, " - Setting category : "
218: + catName);
219: dm.setCategory(dbms, id, catId);
220: }
221: }
222: }
223:
224: //--------------------------------------------------------------------------
225: //--- Privileges
226: //--------------------------------------------------------------------------
227:
228: private static void addPrivileges(DataManager dm, Dbms dbms,
229: String id, Element privil) throws Exception {
230: List locGrps = dbms.select("SELECT id,name FROM Groups")
231: .getChildren();
232: List list = privil.getChildren("group");
233:
234: for (Object g : list) {
235: Element group = (Element) g;
236: String grpName = group.getAttributeValue("name");
237: String grpId = mapLocalEntity(locGrps, grpName);
238:
239: if (grpId == null)
240: Log.debug(Geonet.MEF, " - Skipping inesistent group : "
241: + grpName);
242: else {
243: //--- metadata group exists locally
244:
245: Log.debug(Geonet.MEF,
246: " - Setting privileges for group : " + grpName);
247: addOperations(dm, dbms, group, id, grpId);
248: }
249: }
250: }
251:
252: //--------------------------------------------------------------------------
253:
254: private static void addOperations(DataManager dm, Dbms dbms,
255: Element group, String id, String grpId) throws Exception {
256: List opers = group.getChildren("operation");
257:
258: for (int j = 0; j < opers.size(); j++) {
259: Element oper = (Element) opers.get(j);
260: String opName = oper.getAttributeValue("name");
261:
262: int opId = dm.getAccessManager().getPrivilegeId(opName);
263:
264: if (opId == -1)
265: Log.debug(Geonet.MEF, " Skipping --> " + opName);
266: else {
267: //--- operation exists locally
268:
269: Log.debug(Geonet.MEF, " Adding --> " + opName);
270: dm.setOperation(dbms, id, grpId, opId + "");
271: }
272: }
273: }
274:
275: //--------------------------------------------------------------------------
276: //---
277: //--- Private methods
278: //---
279: //--------------------------------------------------------------------------
280:
281: private static String mapLocalEntity(List entities, String name) {
282: for (Object e : entities) {
283: Element entity = (Element) e;
284:
285: if (entity.getChildText("name").equals(name))
286: return entity.getChildText("id");
287: }
288:
289: return null;
290: }
291: }
292:
293: //=============================================================================
|