001: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
002: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
003: //=== and United Nations Environment Programme (UNEP)
004: //===
005: //=== This program is free software; you can redistribute it and/or modify
006: //=== it under the terms of the GNU General Public License as published by
007: //=== the Free Software Foundation; either version 2 of the License, or (at
008: //=== your option) any later version.
009: //===
010: //=== This program is distributed in the hope that it will be useful, but
011: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
012: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: //=== General Public License for more details.
014: //===
015: //=== You should have received a copy of the GNU General Public License
016: //=== along with this program; if not, write to the Free Software
017: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
018: //===
019: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
020: //=== Rome - Italy. email: geonetwork@osgeo.org
021: //==============================================================================
022:
023: package org.fao.geonet.kernel.search;
024:
025: import java.sql.SQLException;
026: import java.util.ArrayList;
027: import java.util.List;
028: import jeeves.resources.dbms.Dbms;
029: import jeeves.server.ServiceConfig;
030: import jeeves.server.context.ServiceContext;
031: import jeeves.utils.Util;
032: import org.fao.geonet.GeonetContext;
033: import org.fao.geonet.constants.Geonet;
034: import org.fao.geonet.kernel.setting.SettingManager;
035: import org.fao.geonet.util.ISODate;
036: import org.jdom.Element;
037:
038: //==============================================================================
039:
040: class UnusedSearcher extends MetaSearcher {
041: private ArrayList alResult;
042: private Element elSummary;
043:
044: //--------------------------------------------------------------------------
045: //---
046: //--- Constructor
047: //---
048: //--------------------------------------------------------------------------
049:
050: public UnusedSearcher() {
051: }
052:
053: //--------------------------------------------------------------------------
054: //---
055: //--- MetaSearcher Interface
056: //---
057: //--------------------------------------------------------------------------
058:
059: public void search(ServiceContext context, Element request,
060: ServiceConfig config) throws Exception {
061: GeonetContext gc = (GeonetContext) context
062: .getHandlerContext(Geonet.CONTEXT_NAME);
063: SettingManager sm = gc.getSettingManager();
064:
065: String siteId = sm.getValue("system/site/siteId");
066:
067: alResult = new ArrayList();
068:
069: //--- get maximun delta in minutes
070:
071: int maxDiff = Integer.parseInt(Util.getParam(request,
072: "maxDiff", "5"));
073:
074: context.info("UnusedSearcher : using maxDiff=" + maxDiff);
075:
076: //--- proper search
077:
078: String query = "SELECT DISTINCT id, createDate, changeDate "
079: + "FROM Metadata "
080: + "WHERE isTemplate='n' AND isHarvested='n' AND source='"
081: + siteId + "'";
082:
083: Dbms dbms = (Dbms) context.getResourceManager().open(
084: Geonet.Res.MAIN_DB);
085: List list = dbms.select(query).getChildren();
086:
087: for (int i = 0; i < list.size(); i++) {
088: Element rec = (Element) list.get(i);
089:
090: String id = rec.getChildText("id");
091:
092: ISODate createDate = new ISODate(rec
093: .getChildText("createdate"));
094: ISODate changeDate = new ISODate(rec
095: .getChildText("changedate"));
096:
097: if (changeDate.sub(createDate) / 60 < maxDiff)
098: if (!hasInternetGroup(dbms, id))
099: alResult.add(id);
100: }
101:
102: //--- build summary
103:
104: makeSummary();
105:
106: initSearchRange(context);
107: }
108:
109: //--------------------------------------------------------------------------
110:
111: public Element present(ServiceContext srvContext, Element request,
112: ServiceConfig config) throws Exception {
113: updateSearchRange(request);
114:
115: GeonetContext gc = (GeonetContext) srvContext
116: .getHandlerContext(Geonet.CONTEXT_NAME);
117:
118: //--- build response
119:
120: Element response = new Element("response");
121: response.setAttribute("from", getFrom() + "");
122: response.setAttribute("to", getTo() + "");
123:
124: response.addContent((Element) elSummary.clone());
125:
126: if (getTo() > 0) {
127: for (int i = getFrom() - 1; i < getTo(); i++) {
128: String id = (String) alResult.get(i);
129: Element md = gc.getDataManager().getMetadata(
130: srvContext, id, false);
131:
132: response.addContent(md);
133: }
134: }
135:
136: return response;
137: }
138:
139: //--------------------------------------------------------------------------
140:
141: public int getSize() {
142: return alResult.size();
143: }
144:
145: //--------------------------------------------------------------------------
146:
147: public Element getSummary() throws Exception {
148: Element response = new Element("response");
149: response.addContent((Element) elSummary.clone());
150:
151: return response;
152: }
153:
154: //--------------------------------------------------------------------------
155:
156: public void close() {
157: }
158:
159: //--------------------------------------------------------------------------
160: //---
161: //--- Private methods
162: //---
163: //--------------------------------------------------------------------------
164:
165: private boolean hasInternetGroup(Dbms dbms, String id)
166: throws SQLException {
167: String query = "SELECT COUNT(*) AS result FROM OperationAllowed WHERE groupId=1 AND metadataId="
168: + id;
169:
170: List list = dbms.select(query).getChildren();
171:
172: Element record = (Element) list.get(0);
173:
174: int result = Integer.parseInt(record.getChildText("result"));
175:
176: return (result > 0);
177: }
178:
179: //--------------------------------------------------------------------------
180:
181: private void makeSummary() throws Exception {
182: elSummary = new Element("summary");
183:
184: elSummary.setAttribute("count", getSize() + "");
185: elSummary.setAttribute("type", "local");
186:
187: Element elKeywords = new Element("keywords");
188: elSummary.addContent(elKeywords);
189:
190: Element elCategories = new Element("categories");
191: elSummary.addContent(elCategories);
192: }
193: }
194:
195: //==============================================================================
|