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.lib;
025:
026: import java.sql.SQLException;
027: import java.util.ArrayList;
028: import java.util.HashMap;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.Set;
032: import jeeves.resources.dbms.Dbms;
033: import org.jdom.Element;
034:
035: //=============================================================================
036:
037: public class LocalLib {
038: //-----------------------------------------------------------------------------
039: //---
040: //--- API methods
041: //---
042: //-----------------------------------------------------------------------------
043:
044: public Map<String, String> getLanguages(Dbms dbms)
045: throws SQLException {
046: HashMap<String, String> hm = new HashMap<String, String>();
047:
048: for (Object obj : dbms.select("SELECT * FROM Languages")
049: .getChildren()) {
050: Element lang = (Element) obj;
051: hm.put(lang.getChildText("id"), lang.getChildText("name"));
052: }
053:
054: return hm;
055: }
056:
057: //-----------------------------------------------------------------------------
058:
059: public void insert(Dbms dbms, String baseTable, int id, String name)
060: throws SQLException {
061: Set<String> langs = getLanguages(dbms).keySet();
062:
063: String query = "INSERT INTO " + baseTable
064: + "Des(idDes, langId, label) VALUES (?,?,?)";
065:
066: for (String langId : langs)
067: dbms.execute(query, id, langId, name);
068: }
069:
070: //-----------------------------------------------------------------------------
071:
072: public void insert(Dbms dbms, String baseTable, int id,
073: Map<String, String> locNames, String defName)
074: throws SQLException {
075: Set<String> langs = getLanguages(dbms).keySet();
076:
077: String query = "INSERT INTO " + baseTable
078: + "Des(idDes, langId, label) VALUES (?,?,?)";
079:
080: for (String langId : langs) {
081: String name = (locNames == null) ? null : locNames
082: .get(langId);
083:
084: //--- check if the local language does not exist in locNames
085: //--- this will help to align languages with remote sites
086:
087: if (name == null)
088: name = defName;
089:
090: dbms.execute(query, id, langId, name);
091: }
092: }
093:
094: //-----------------------------------------------------------------------------
095:
096: public void update(Dbms dbms, String baseTable, int id,
097: Element label) throws SQLException {
098: List labels = label.getChildren();
099:
100: for (Object lt : labels) {
101: Element locText = (Element) lt;
102:
103: String langId = locText.getName();
104: String value = locText.getText();
105:
106: update(dbms, baseTable, id, langId, value);
107: }
108: }
109:
110: //-----------------------------------------------------------------------------
111:
112: public void update(Dbms dbms, String baseTable, int id,
113: String langId, String label) throws SQLException {
114: String query = "UPDATE " + baseTable
115: + "Des SET label=? WHERE idDes=? AND langId=?";
116:
117: dbms.execute(query, label, id, langId);
118: }
119:
120: //-----------------------------------------------------------------------------
121:
122: public Element retrieve(Dbms dbms, String table)
123: throws SQLException {
124: return retrieve(dbms, table, null, null, null);
125: }
126:
127: //-----------------------------------------------------------------------------
128:
129: public Element retrieve(Dbms dbms, String table, String where)
130: throws SQLException {
131: return retrieve(dbms, table, null, where, null);
132: }
133:
134: //-----------------------------------------------------------------------------
135:
136: public Element retrieve(Dbms dbms, String table, String where,
137: String orderBy) throws SQLException {
138: return retrieve(dbms, table, null, where, orderBy);
139: }
140:
141: //-----------------------------------------------------------------------------
142:
143: public Element retrieveById(Dbms dbms, String table, String id)
144: throws SQLException {
145: return retrieve(dbms, table, id, null, null);
146: }
147:
148: //-----------------------------------------------------------------------------
149: //---
150: //--- Private methods
151: //---
152: //-----------------------------------------------------------------------------
153:
154: private Element retrieve(Dbms dbms, String table, String id,
155: String where, String orderBy) throws SQLException {
156: String query1 = "SELECT * FROM " + table;
157: String query2 = "SELECT * FROM " + table + "Des";
158:
159: if (id == null) {
160: if (where != null)
161: query1 += " WHERE " + where;
162: } else {
163: query1 += " WHERE id=" + id;
164: query2 += " WHERE idDes=" + id;
165: }
166:
167: if (orderBy != null)
168: query1 += " ORDER BY " + orderBy;
169:
170: Element result = dbms.select(query1);
171:
172: List base = result.getChildren();
173: List des = dbms.select(query2).getChildren();
174:
175: //--- preprocess data for faster access
176:
177: Map<String, List<String>> langData = new HashMap<String, List<String>>();
178:
179: for (Object o : des) {
180: Element loc = (Element) o;
181:
182: String iddes = loc.getChildText("iddes");
183: String lang = loc.getChildText("langid");
184: String label = loc.getChildText("label");
185:
186: List<String> list = langData.get(iddes);
187:
188: if (list == null) {
189: list = new ArrayList<String>();
190: langData.put(iddes, list);
191: }
192:
193: list.add(lang);
194: list.add(label);
195: }
196:
197: //--- fill results
198:
199: for (Object o : base) {
200: Element record = (Element) o;
201: Element labels = new Element("label");
202:
203: record.addContent(labels);
204:
205: id = record.getChildText("id");
206:
207: List<String> list = langData.get(id);
208:
209: for (int j = 0; j < list.size(); j += 2)
210: labels.addContent(new Element(list.get(j)).setText(list
211: .get(j + 1)));
212: }
213:
214: return result.setName(table.toLowerCase());
215: }
216: }
217:
218: //=============================================================================
|