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.harvest.harvester;
025:
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.UUID;
029: import jeeves.exceptions.BadInputEx;
030: import jeeves.exceptions.BadParameterEx;
031: import jeeves.exceptions.MissingParameterEx;
032: import jeeves.utils.Util;
033: import org.fao.geonet.kernel.DataManager;
034: import org.fao.geonet.kernel.harvest.harvester.Privileges;
035: import org.fao.geonet.lib.Lib;
036: import org.jdom.Element;
037:
038: //=============================================================================
039:
040: public abstract class AbstractParams {
041: //---------------------------------------------------------------------------
042: //---
043: //--- Constructor
044: //---
045: //---------------------------------------------------------------------------
046:
047: public AbstractParams(DataManager dm) {
048: this .dm = dm;
049: }
050:
051: //---------------------------------------------------------------------------
052: //---
053: //--- API methods
054: //---
055: //---------------------------------------------------------------------------
056:
057: public void create(Element node) throws BadInputEx {
058: Element site = node.getChild("site");
059: Element opt = node.getChild("options");
060: Element account = (site == null) ? null : site
061: .getChild("account");
062:
063: name = Util.getParam(site, "name", "");
064: uuid = Util
065: .getParam(site, "uuid", UUID.randomUUID().toString());
066:
067: useAccount = Util.getParam(account, "use", false);
068: username = Util.getParam(account, "username", "");
069: password = Util.getParam(account, "password", "");
070:
071: every = Util.getParam(opt, "every", 90);
072: oneRunOnly = Util.getParam(opt, "oneRunOnly", false);
073:
074: checkEvery(every);
075:
076: addPrivileges(node.getChild("privileges"));
077: addCategories(node.getChild("categories"));
078: }
079:
080: //---------------------------------------------------------------------------
081:
082: public void update(Element node) throws BadInputEx {
083: Element site = node.getChild("site");
084: Element opt = node.getChild("options");
085: Element account = (site == null) ? null : site
086: .getChild("account");
087: Element privil = node.getChild("privileges");
088: Element categ = node.getChild("categories");
089:
090: name = Util.getParam(site, "name", name);
091:
092: useAccount = Util.getParam(account, "use", useAccount);
093: username = Util.getParam(account, "username", username);
094: password = Util.getParam(account, "password", password);
095:
096: every = Util.getParam(opt, "every", every);
097: oneRunOnly = Util.getParam(opt, "oneRunOnly", oneRunOnly);
098:
099: checkEvery(every);
100:
101: if (privil != null)
102: addPrivileges(privil);
103:
104: if (categ != null)
105: addCategories(categ);
106: }
107:
108: //---------------------------------------------------------------------------
109:
110: public Iterable<Privileges> getPrivileges() {
111: return alPrivileges;
112: }
113:
114: public Iterable<String> getCategories() {
115: return alCategories;
116: }
117:
118: //---------------------------------------------------------------------------
119: //---
120: //--- Protected methods
121: //---
122: //---------------------------------------------------------------------------
123:
124: protected void copyTo(AbstractParams copy) {
125: copy.name = name;
126: copy.uuid = uuid;
127:
128: copy.useAccount = useAccount;
129: copy.username = username;
130: copy.password = password;
131:
132: copy.every = every;
133: copy.oneRunOnly = oneRunOnly;
134:
135: for (Privileges p : alPrivileges)
136: copy.alPrivileges.add(p.copy());
137:
138: for (String s : alCategories)
139: copy.alCategories.add(s);
140: }
141:
142: //---------------------------------------------------------------------------
143:
144: protected void checkEvery(int every) throws BadParameterEx {
145: if (every < 1 || every > MAX_EVERY)
146: throw new BadParameterEx("every", every);
147: }
148:
149: //---------------------------------------------------------------------------
150:
151: protected void checkPort(int port) throws BadParameterEx {
152: if (port < 1 || port > 65535)
153: throw new BadParameterEx("port", port);
154: }
155:
156: //---------------------------------------------------------------------------
157: //---
158: //--- Privileges and categories API methods
159: //---
160: //---------------------------------------------------------------------------
161:
162: /** Fills a list with Privileges that reflect the input 'privileges' element.
163: * The 'privileges' element has this format:
164: *
165: * <privileges>
166: * <group id="...">
167: * <operation name="...">
168: * ...
169: * </group>
170: * ...
171: * </privileges>
172: *
173: * Operation names are: view, download, edit, etc... User defined operations are
174: * taken into account.
175: */
176:
177: private void addPrivileges(Element privil) throws BadInputEx {
178: alPrivileges.clear();
179:
180: if (privil == null)
181: return;
182:
183: Iterator groupList = privil.getChildren("group").iterator();
184:
185: while (groupList.hasNext()) {
186: Element group = (Element) groupList.next();
187: String groupID = group.getAttributeValue("id");
188:
189: if (groupID == null)
190: throw new MissingParameterEx("attribute:id", group);
191:
192: Privileges p = new Privileges(groupID);
193:
194: Iterator operList = group.getChildren("operation")
195: .iterator();
196:
197: while (operList.hasNext()) {
198: Element oper = (Element) operList.next();
199: int op = getOperationId(oper);
200:
201: p.add(op);
202: }
203:
204: alPrivileges.add(p);
205: }
206: }
207:
208: //---------------------------------------------------------------------------
209:
210: private int getOperationId(Element oper) throws BadInputEx {
211: String operName = oper.getAttributeValue("name");
212:
213: if (operName == null)
214: throw new MissingParameterEx("attribute:name", oper);
215:
216: int operID = dm.getAccessManager().getPrivilegeId(operName);
217:
218: if (operID == -1)
219: throw new BadParameterEx("attribute:name", operName);
220:
221: if (operID == 2 || operID == 4)
222: throw new BadParameterEx("attribute:name", operName);
223:
224: return operID;
225: }
226:
227: //---------------------------------------------------------------------------
228: /** Fills a list with category identifiers that reflect the input 'categories' element.
229: * The 'categories' element has this format:
230: *
231: * <categories>
232: * <category id="..."/>
233: * ...
234: * </categories>
235: */
236:
237: private void addCategories(Element categ) throws BadInputEx {
238: alCategories.clear();
239:
240: if (categ == null)
241: return;
242:
243: Iterator categList = categ.getChildren("category").iterator();
244:
245: while (categList.hasNext()) {
246: Element categElem = (Element) categList.next();
247: String categId = categElem.getAttributeValue("id");
248:
249: if (categId == null)
250: throw new MissingParameterEx("attribute:id", categElem);
251:
252: if (!Lib.type.isInteger(categId))
253: throw new BadParameterEx("attribute:id", categElem);
254:
255: alCategories.add(categId);
256: }
257: }
258:
259: //---------------------------------------------------------------------------
260: //---
261: //--- Variables
262: //---
263: //---------------------------------------------------------------------------
264:
265: public String name;
266: public String uuid;
267:
268: public boolean useAccount;
269: public String username;
270: public String password;
271:
272: public int every;
273: public boolean oneRunOnly;
274:
275: //---------------------------------------------------------------------------
276:
277: protected DataManager dm;
278:
279: private ArrayList<Privileges> alPrivileges = new ArrayList<Privileges>();
280: private ArrayList<String> alCategories = new ArrayList<String>();
281:
282: //---------------------------------------------------------------------------
283:
284: private static final int MAX_EVERY = 1000000;
285: }
286:
287: //=============================================================================
|