001/*
002 * Copyright (c) 2000-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.sdm.spa;
031
032import java.io.StringReader;
033import java.util.Date;
034
035import javax.xml.parsers.DocumentBuilder;
036import javax.xml.parsers.DocumentBuilderFactory;
037
038import org.w3c.dom.Document;
039import org.w3c.dom.Element;
040import org.w3c.dom.NodeList;
041import org.xml.sax.InputSource;
042
043import ptolemy.actor.TypedAtomicActor;
044import ptolemy.actor.TypedIOPort;
045import ptolemy.data.StringToken;
046import ptolemy.data.type.BaseType;
047import ptolemy.kernel.CompositeEntity;
048import ptolemy.kernel.util.IllegalActionException;
049import ptolemy.kernel.util.NameDuplicationException;
050
051//////////////////////////////////////////////////////////////////////////
052//// CreateExpressionFromQuery
053/**
054 * @author Ilkay Altintas, Ashraf Memonn
055 * @version $Id: CreateRequestFromResult.java 11161 2005-11-01 20:39:16Z ruland
056 *          $
057 */
058
059public class CreateRequestFromResult extends TypedAtomicActor {
060
061        /**
062         * Construct a CreateExpressionFromQuery actor with the given container and
063         * name.
064         * 
065         * @param container
066         *            The container.
067         * @param name
068         *            The name of this actor.
069         * @exception IllegalActionException
070         *                If the actor cannot be contained by the proposed
071         *                container.
072         * @exception NameDuplicationException
073         *                If the container already has an actor with this name.
074         */
075        public CreateRequestFromResult(CompositeEntity container, String name)
076                        throws NameDuplicationException, IllegalActionException {
077
078                super(container, name);
079
080                resultArizona = new TypedIOPort(this, "resultArizona", true, false);
081                resultArizona.setTypeEquals(BaseType.STRING);
082                resultIdaho = new TypedIOPort(this, "resultIdaho", true, false);
083                resultIdaho.setTypeEquals(BaseType.STRING);
084                envelope = new TypedIOPort(this, "envelope", true, false);
085                envelope.setTypeEquals(BaseType.STRING);
086
087                request = new TypedIOPort(this, "request", false, true);
088                request.setTypeEquals(BaseType.STRING);
089
090                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
091                                + "width=\"80\" height=\"40\" " + "style=\"fill:white\"/>\n"
092                                + "</svg>\n");
093        }
094
095        // /////////////////////////////////////////////////////////////////
096        // // ports and parameters ////
097
098        public TypedIOPort resultArizona;
099        public TypedIOPort resultIdaho;
100        public TypedIOPort envelope;
101        public TypedIOPort request;
102
103        private String constructRenderRequest(String layerName, String output) {
104                String str = new String();
105                Element element = stringToElement(output);
106                String type = getStringValueForXMLTag(element, "type");
107                String url = getStringValueForXMLTag(element, "url");
108                return ("<layer><name>" + layerName + "</name><type>" + type
109                                + "</type><url>" + url + "</url></layer>");
110        }
111
112        public Element stringToElement(String arg) {
113                try {
114                        DocumentBuilderFactory dbf;
115                        DocumentBuilder db;
116                        Document document;
117                        dbf = DocumentBuilderFactory.newInstance();
118                        db = dbf.newDocumentBuilder();
119                        document = db.parse(new InputSource(new StringReader(arg)));
120                        return document.getDocumentElement();
121                } catch (Exception e) {
122                        System.out.println(e.getMessage());
123                        return null;
124                }
125        }
126
127        public String getStringValueForXMLTag(Element xmlElement, String key) {
128                NodeList nl = xmlElement.getElementsByTagName(key);
129                if (nl.getLength() > 0) {
130                        return (nl.item(0).getFirstChild().getNodeValue().trim());
131                }
132                return "";
133        }
134
135        /**
136         * 
137         * @exception IllegalActionException
138         *                If there is no director.
139         */
140        public void fire() throws IllegalActionException {
141                super.fire();
142
143                StringToken inputToken = (StringToken) resultArizona.get(0);
144                String arizonaStr = inputToken.stringValue();
145                System.out.println("arizona--->" + arizonaStr);
146                inputToken = (StringToken) resultIdaho.get(0);
147                String idahoStr = inputToken.stringValue();
148                System.out.println("idaho--->" + idahoStr);
149                inputToken = (StringToken) envelope.get(0);
150                String envStr = inputToken.stringValue();
151                System.out.println("envelope" + envStr);
152                String requestStr = "";
153
154                // ASHRAF'S CODE
155                String dynamicService = new String(Long.toString(new Date().getTime()));
156                requestStr = "<request name=\"RenderMap\"><client>Browser</client><servicename>D"
157                                + dynamicService
158                                + "</servicename><envelope>"
159                                + envStr
160                                + "</envelope><layers>"
161                                + constructRenderRequest("Arizona", arizonaStr)
162                                + constructRenderRequest("Idaho", idahoStr)
163                                + "<layer><name>States</name><type>LocalURL"
164                                + "</type><url>c:\\geontemp\\states_polygon_area.shp</url></layer></layers></request>";
165                System.out.println("request--->" + requestStr);
166                // ASHRAF's CODE ENDS
167
168                request.broadcast(new StringToken(requestStr));
169
170        } // end of fire
171
172        /**
173         * Post fire the actor. Return false to indicate that the process has
174         * finished. If it returns true, the process will continue indefinitely.
175         */
176        public boolean postfire() {
177                return false;
178        } // end of postfire
179
180        /**
181         * Pre fire the actor. Calls the super class's prefire in case something is
182         * set there.
183         */
184        public boolean prefire() throws IllegalActionException {
185                return super.prefire();
186        } // end of prefire
187
188} // end of WebService