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.io.IOException;
028: import java.io.InputStream;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.UUID;
032: import jeeves.resources.dbms.Dbms;
033: import org.fao.geonet.kernel.mef.MEFLib;
034: import org.fao.geonet.kernel.mef.MEFVisitor;
035: import org.fao.geonet.util.ISODate;
036: import org.jdom.Element;
037:
038: //=============================================================================
039:
040: public class MefLib {
041: //---------------------------------------------------------------------------
042: //---
043: //--- API methods
044: //---
045: //---------------------------------------------------------------------------
046:
047: public void doImport(final Dbms dbms, final int id, File mefFile)
048: throws Exception {
049: final Element md[] = { null };
050:
051: //--- import metadata from MEF file
052:
053: Lib.log.info("Adding MEF file : " + mefFile.getAbsolutePath());
054:
055: MEFLib.visit(mefFile, new MEFVisitor() {
056: public void handleMetadata(Element mdata) throws Exception {
057: md[0] = mdata;
058: }
059:
060: //--------------------------------------------------------------------
061:
062: public void handleInfo(Element info) throws Exception {
063: addMetadata(dbms, id, md[0], info);
064: }
065:
066: //--------------------------------------------------------------------
067:
068: public void handlePublicFile(String file,
069: String changeDate, InputStream is)
070: throws IOException {
071: saveFile(id, file, changeDate, is, "public");
072: }
073:
074: //--------------------------------------------------------------------
075:
076: public void handlePrivateFile(String file,
077: String changeDate, InputStream is)
078: throws IOException {
079: saveFile(id, file, changeDate, is, "private");
080: }
081: });
082: }
083:
084: //--------------------------------------------------------------------------
085: //---
086: //--- Private methods
087: //---
088: //---------------------------------------------------------------------------
089:
090: private void addMetadata(Dbms dbms, int id, Element md, Element info)
091: throws Exception {
092: int owner = 1;
093:
094: String groupOwner = null;
095:
096: Element general = info.getChild("general");
097:
098: String uuid = general.getChildText("uuid");
099: String createDate = general.getChildText("createDate");
100: String changeDate = general.getChildText("changeDate");
101: String source = general.getChildText("siteId");
102: String schema = general.getChildText("schema");
103: String isTemplate = general.getChildText("isTemplate").equals(
104: "true") ? "y" : "n";
105:
106: boolean dcore = schema.equals("dublin-core");
107: boolean fgdc = schema.equals("fgdc-std");
108: boolean iso115 = schema.equals("iso19115");
109: boolean iso139 = schema.equals("iso19139");
110:
111: if (!dcore && !fgdc && !iso115 && !iso139)
112: throw new Exception("Unknown schema format : " + schema);
113:
114: if (uuid == null) {
115: uuid = UUID.randomUUID().toString();
116: source = Lib.site.getSiteId(dbms);
117:
118: //--- set uuid inside metadata
119: md = Lib.metadata.setUUID(schema, uuid, md);
120: }
121:
122: Lib.log.debug(" - Adding XML with uuid : " + uuid);
123:
124: Lib.metadata.insertMetadata(dbms, schema, md, id, source,
125: createDate, changeDate, uuid, owner, groupOwner,
126: isTemplate, null);
127:
128: addCategories(dbms, id, info.getChild("categories"));
129: addPrivileges(dbms, id, info.getChild("privileges"));
130:
131: //TODO: index metadata here
132: //index is performed at startup
133: //indexMetadata(dbms, id);
134: }
135:
136: //--------------------------------------------------------------------------
137:
138: private void saveFile(int id, String file, String changeDate,
139: InputStream is, String access) throws IOException {
140: Lib.log.debug(" - Adding '" + access + "' file with name : "
141: + file);
142:
143: File outDir = new File(Lib.metadata.getDir(id, access));
144: File outFile = new File(outDir, file);
145:
146: Lib.log.debug(" - Destination folder is : "
147: + outDir.getAbsolutePath());
148: outDir.mkdirs();
149:
150: Lib.io.save(outFile, is);
151: outFile
152: .setLastModified(new ISODate(changeDate).getSeconds() * 1000);
153: }
154:
155: //--------------------------------------------------------------------------
156: //--- Categories
157: //--------------------------------------------------------------------------
158:
159: private void addCategories(Dbms dbms, int id, Element categ)
160: throws Exception {
161: List locCats = dbms.select("SELECT id, name FROM Categories")
162: .getChildren();
163: List list = categ.getChildren("category");
164:
165: for (Iterator j = list.iterator(); j.hasNext();) {
166: String catName = ((Element) j.next())
167: .getAttributeValue("name");
168: String catId = mapLocalEntity(locCats, catName);
169:
170: if (catId == null)
171: Lib.log.debug(" - Skipping inesistent category : "
172: + catName);
173: else {
174: //--- metadata category exists locally
175:
176: Lib.log.debug(" - Setting category : " + catName);
177:
178: String query = "INSERT INTO MetadataCateg(metadataId, categoryId) VALUES(?,?)";
179:
180: dbms.execute(query, id, new Integer(catId));
181: }
182: }
183: }
184:
185: //--------------------------------------------------------------------------
186: //--- Privileges
187: //--------------------------------------------------------------------------
188:
189: private void addPrivileges(Dbms dbms, int id, Element privil)
190: throws Exception {
191: List locGrps = dbms.select("SELECT id,name FROM Groups")
192: .getChildren();
193: List list = privil.getChildren("group");
194:
195: for (Object g : list) {
196: Element group = (Element) g;
197: String grpName = group.getAttributeValue("name");
198: String grpId = mapLocalEntity(locGrps, grpName);
199:
200: if (grpId == null)
201: Lib.log.debug(" - Skipping inesistent group : "
202: + grpName);
203: else {
204: //--- metadata group exists locally
205:
206: Lib.log.debug(" - Setting privileges for group : "
207: + grpName);
208: addOperations(dbms, group, id, Integer.parseInt(grpId));
209: }
210: }
211: }
212:
213: //--------------------------------------------------------------------------
214:
215: private void addOperations(Dbms dbms, Element group, int id,
216: int grpId) throws Exception {
217: List opers = group.getChildren("operation");
218:
219: for (int j = 0; j < opers.size(); j++) {
220: Element oper = (Element) opers.get(j);
221: String opName = oper.getAttributeValue("name");
222:
223: int opId = getPrivilegeId(opName);
224:
225: if (opId == -1)
226: Lib.log.debug(" Skipping --> " + opName);
227: else {
228: //--- operation exists locally
229:
230: Lib.log.debug(" Adding --> " + opName);
231:
232: String query = "INSERT INTO OperationAllowed(metadataId, groupId, operationId) VALUES(?,?,?)";
233:
234: dbms.execute(query, id, grpId, opId);
235: }
236: }
237: }
238:
239: //--------------------------------------------------------------------------
240:
241: private int getPrivilegeId(String descr) {
242: if (descr.equals("view"))
243: return 0;
244: if (descr.equals("download"))
245: return 1;
246: if (descr.equals("notify"))
247: return 3;
248: if (descr.equals("dynamic"))
249: return 5;
250: if (descr.equals("featured"))
251: return 6;
252:
253: return -1;
254: }
255:
256: //--------------------------------------------------------------------------
257: //--- General private methods
258: //--------------------------------------------------------------------------
259:
260: private String mapLocalEntity(List entities, String name) {
261: for (Object e : entities) {
262: Element entity = (Element) e;
263:
264: if (entity.getChildText("name").equals(name))
265: return entity.getChildText("id");
266: }
267:
268: return null;
269: }
270: }
271:
272: //=============================================================================
|