001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.ecoinformatics.seek.ecogrid;
031
032import java.awt.Point;
033import java.awt.event.ActionEvent;
034import java.util.Vector;
035
036import javax.swing.AbstractAction;
037
038import org.apache.commons.logging.Log;
039import org.apache.commons.logging.LogFactory;
040import org.ecoinformatics.ecogrid.client.RegistryServiceClient;
041import org.ecoinformatics.ecogrid.registry.stub.RegistryEntryType;
042import org.kepler.configuration.ConfigurationManager;
043import org.kepler.configuration.ConfigurationProperty;
044
045import ptolemy.util.MessageHandler;
046
047/**
048 * Class will search the registry
049 * 
050 * @author Jing Tao
051 * 
052 */
053
054public class SearchRegistryAction extends AbstractAction {
055        private RegistrySearchDialog searchDialog = null;
056        private EcogridPreferencesTab parent = null;
057        private String registryEndPoint = null;
058        private RegistryServiceClient client = null;
059
060        private static final String ENDPOINTPATH = "//ecogridService/registry/endPoint";
061        private static final String PERCENTAGE = "%";
062        private String sessionId = "foo"; // this value can be replaced by something
063                                                                                // else
064
065        protected final static Log log;
066        static {
067                log = LogFactory
068                                .getLog("org.ecoinformatics.seek.ecogrid.SearchRegistryAction");
069        }
070
071        /**
072         * Constructor
073         * 
074         * @param name
075         *            String
076         * @param searchDialog
077         *            JDialog
078         * @param parent
079         *            ServicesDisplayFrame
080         */
081        public SearchRegistryAction(String name, RegistrySearchDialog searchDialog,
082                        EcogridPreferencesTab parent, Point location) {
083                super(name);
084                this.searchDialog = searchDialog;
085                this.parent = parent;
086    ConfigurationProperty ecogridProperty = ConfigurationManager.getInstance()
087      .getProperty(ConfigurationManager.getModule("ecogrid"));
088    ConfigurationProperty endpointProperty = ecogridProperty.getProperty("registry.endPoint");
089    registryEndPoint = endpointProperty.getValue();
090
091        }// SearchRegistryAction
092
093        /**
094         * Method do search
095         * 
096         * @param e
097         *            ActionEvent
098         */
099        public void actionPerformed(ActionEvent e) {
100                String xpath = searchDialog.getXPath();
101                String value = searchDialog.getSearchValue();
102                try {
103                        client = new RegistryServiceClient(registryEndPoint);
104                } catch (Exception ee) {
105                        log.debug("The error to generate registry search client ", ee);
106                }
107                if (client != null && xpath != null && !xpath.trim().equals("")) {
108                        Vector searchResult = searchRegistry(xpath, value);
109                        Vector onlyQueryServices = EcoGridServicesController
110                                        .filterQueryServicesList(searchResult);
111                        Vector selectedServicesLists = SelectableEcoGridService
112                                        .transferServiceVectToDefaultSelectedServiceVect(onlyQueryServices);
113                        // destroy the search dialog
114                        searchDialog.setVisible(false);
115                        searchDialog.dispose();
116                        searchDialog = null;
117                        // create a search result frame base on search result
118                        EcoGridServicesController controller = parent
119                                        .getEcoGridServicesController();
120                        //SearchRegistryResultFrame resultFrame = new SearchRegistryResultFrame(
121                        //              "Search Result", parent);
122
123                }// if
124
125        }// actionPerformed
126
127        /*
128         * This methd will search registry
129         */
130        private Vector searchRegistry(String optionLabel, String value) {
131                Vector newServiceList = new Vector();
132
133                // This is for all service
134                if (optionLabel != null
135                                && optionLabel.equals(RegistrySearchDialog.ALLSERVICES)) {
136                        log.debug("The option label is " + optionLabel);
137                        RegistryEntryType[] serviceList = null;
138                        try {
139                                serviceList = client.list(sessionId);
140                                newServiceList = EcoGridServicesController
141                                                .registryEntries2EcogridServices(serviceList, true // selectable
142                                                );
143                        } catch (Exception e) {
144                                log.debug("couldn't search the ecogrid registry because ", e);
145                        }
146
147                }// if
148                else if (optionLabel != null && value != null) {
149                        log.debug("The option label is " + optionLabel + " and value is "
150                                        + value);
151                        String xpath = searchDialog.getXPath(optionLabel);
152                        value = decorateSearchValue(value);
153                        log.debug("The xpath value is:" + xpath + " for option label:"
154                                        + optionLabel + " sessionId:" + sessionId);
155                        if (xpath != null) {
156                                RegistryEntryType[] serviceList = null;
157                                try {
158                                        serviceList = client.query(sessionId, xpath, value);
159                                        newServiceList = EcoGridServicesController
160                                                        .registryEntries2EcogridServices(serviceList, true // selectable
161                                                        );
162                                } catch (Exception e) {
163                                        log.debug("couldn't search the ecogrid registry because ",
164                                                        e);
165                                }
166                        }// if
167
168                }// elseif
169
170                return newServiceList;
171        }// searchResigry
172
173        /**
174         * Query the registry and persist to config file
175         */
176        public static void queryRegistryRewriteConfig() {
177
178                RegistryEntryType[] registryEntries = null;
179                Vector ecogridServicesList = new Vector();
180                String sessionId = "fakesessionid";
181    
182    ConfigurationProperty ecogridProperty = ConfigurationManager.getInstance()
183      .getProperty(ConfigurationManager.getModule("ecogrid"));
184    ConfigurationProperty registryEndPointProp = ecogridProperty.getProperty("registry.endPoint");
185    String registryEndPoint = registryEndPointProp.getValue();
186
187                RegistryServiceClient client = null;
188
189                try {
190                        client = new RegistryServiceClient(registryEndPoint);
191                } catch (Exception ee) {
192                        String msg = "Problem creating registry search service client using endpoint: \n"
193                                        + registryEndPoint;
194                        // MessageHandler.error(msg, ee);
195                        log.warn(msg);
196                        return;
197                }
198                try {
199                        // get all the entries
200                        registryEntries = client.list(sessionId);
201
202                } catch (Exception e) {
203                        String msg = "Problem looking up registry entries using endpoint: \n"
204                                        + registryEndPoint;
205                        // MessageHandler.error(msg, e);
206                        log.warn(msg);
207                        return;
208                }
209
210                try {
211                        // convert them to EcogridServices, optionally selectable
212                        ecogridServicesList = EcoGridServicesController
213                                        .registryEntries2EcogridServices(registryEntries, true // selectable
214                                        );
215                        // set the new services from registry as the services in the
216                        // controller
217                        // EcoGridServicesController.getInstance().setServicesList(ecogridServicesList);
218                        // merge the new list with the existing
219                        EcoGridServicesController.getInstance().mergeServicesList(
220                                        ecogridServicesList);
221                } catch (Exception e) {
222                        String msg = "Problem merging new services with existing services";
223                        MessageHandler.error(msg, e);
224                        log.error(msg);
225                        return;
226                }
227
228                // write them to the config
229                try {
230                        //EcoGridServicesController.getInstance().writeServicesToConfig();
231                        EcoGridServicesController.getInstance().writeServices();
232                } catch (Exception e) {
233                        String msg = "Problem writing new services to config file";
234                        MessageHandler.error(msg, e);
235                        log.error(msg);
236                        return;
237                }
238        }
239
240        /*
241         * Method to add % symbol to value
242         */
243        private String decorateSearchValue(String value) {
244                String newValue = PERCENTAGE + value + PERCENTAGE;
245                return newValue;
246        }
247
248        /*
249         * This method is hard code to go some serivce list without searching
250         */
251        private Vector hardCodeReturnVector() {
252                Vector newServiceList = new Vector();
253                try {
254                        DocumentType type1 = new DocumentType(
255                                        "eml://ecoinformatics.org/eml-2.0.0",
256                                        "Ecological Metadata Language 2.0.0");
257                        DocumentType type2 = new DocumentType(
258                                        "http://digir.net/schema/conceptual/darwin/full/2001/1.0",
259                                        "Darwin Core 1.0");
260                        DocumentType[] docTypes = new DocumentType[2];
261                        docTypes[0] = type1;
262                        docTypes[1] = type2;
263                        EcoGridService service1 = new EcoGridService();
264                        service1.setServiceName("KNB EcoGrid");
265                        service1.setServiceType("EcoGridQueryInterface");
266                        service1
267                                        .setEndPoint("http://ecogrid.ecoinformatics.org/ogsa/services/org/ecoinformatics/ecogrid/EcoGridQueryInterfaceLevelOneService");
268                        service1.setDocumentTypeList(docTypes);
269
270                        DocumentType type3 = new DocumentType(
271                                        "eml://ecoinformatics.org/eml-2.0.0",
272                                        "Ecological Metadata Language 2.0.0");
273                        DocumentType type4 = new DocumentType(
274                                        "http://digir.net/schema/conceptual/darwin/full/2001/1.0",
275                                        "Darwin Core 1.0");
276                        DocumentType[] docTypes1 = new DocumentType[2];
277                        docTypes1[0] = type3;
278                        docTypes1[1] = type4;
279                        EcoGridService service2 = new EcoGridService();
280                        service2.setServiceName("KU Digir EcoGrid QueryInterface");
281                        service2.setServiceType("EcoGridQueryInterface");
282                        // service2.setEndPoint("http://129.237.201.166:8080/ogsa/services/org/ecoinformatics/ecogrid/EcoGridQueryInterfaceLevelOneServiceFactory");
283                        service2
284                                        .setEndPoint("http://129.237.127.19:8080/ogsa/services/org/ecoinformatics/ecogrid/EcoGridQueryInterfaceLevelOneService");
285                        service2.setDocumentTypeList(docTypes1);
286
287                        DocumentType type5 = new DocumentType(
288                                        "eml://ecoinformatics.org/eml-2.0.0",
289                                        "Ecological Metadata Language 2.0.0");
290                        DocumentType type6 = new DocumentType(
291                                        "http://digir.net/schema/conceptual/darwin/full/2001/1.0",
292                                        "Darwin Core 1.0");
293                        DocumentType[] docTypes2 = new DocumentType[2];
294                        docTypes2[0] = type5;
295                        docTypes2[1] = type6;
296                        EcoGridService service3 = new EcoGridService();
297                        service3.setServiceName("Pine EcoGrid QueryInterface");
298                        service3.setServiceType("EcoGridQueryInterface");
299                        service3
300                                        .setEndPoint("http://pine.nceas.ucsb.edu:8090/ogsa/services/org/ecoinformatics/ecogrid/EcoGridQueryInterfaceLevelOneService");
301                        service3.setDocumentTypeList(docTypes2);
302
303                        newServiceList.add(service1);
304                        newServiceList.add(service2);
305                        newServiceList.add(service3);
306
307                } catch (Exception e) {
308
309                }
310                return newServiceList;
311        }
312
313}