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.sdm.spa;
031
032import java.io.StringReader;
033
034import javax.xml.parsers.DocumentBuilder;
035import javax.xml.parsers.DocumentBuilderFactory;
036
037import org.w3c.dom.Document;
038import org.w3c.dom.Element;
039import org.xml.sax.InputSource;
040
041import ptolemy.actor.TypedAtomicActor;
042import ptolemy.actor.TypedIOPort;
043import ptolemy.data.StringToken;
044import ptolemy.data.type.BaseType;
045import ptolemy.kernel.CompositeEntity;
046import ptolemy.kernel.util.IllegalActionException;
047import ptolemy.kernel.util.NameDuplicationException;
048
049//////////////////////////////////////////////////////////////////////////
050//// CreateExpressionFromQuery
051/**
052 * @author Ilkay Altintas, Ashraf Memonn
053 * @version $Id: CreateExpressionFromQuery.java 12324 2006-04-04 17:23:50Z
054 *          altintas $
055 */
056
057public class CreateExpressionFromQuery extends TypedAtomicActor {
058
059        /**
060         * Construct a CreateExpressionFromQuery actor with the given container and
061         * name.
062         * 
063         * @param container
064         *            The container.
065         * @param name
066         *            The name of this actor.
067         * @exception IllegalActionException
068         *                If the actor cannot be contained by the proposed
069         *                container.
070         * @exception NameDuplicationException
071         *                If the container already has an actor with this name.
072         */
073        public CreateExpressionFromQuery(CompositeEntity container, String name)
074                        throws NameDuplicationException, IllegalActionException {
075
076                super(container, name);
077
078                queryPlan = new TypedIOPort(this, "queryPlan", true, false);
079                queryPlan.setTypeEquals(BaseType.STRING);
080
081                queryExpr1 = new TypedIOPort(this, "queryExpr1", false, true);
082                queryExpr1.setTypeEquals(BaseType.STRING);
083                queryExpr2 = new TypedIOPort(this, "queryExpr2", false, true);
084                queryExpr2.setTypeEquals(BaseType.STRING);
085                queryExpr3 = new TypedIOPort(this, "queryExpr3", false, true);
086                queryExpr3.setTypeEquals(BaseType.STRING);
087                queryExpr4 = new TypedIOPort(this, "queryExpr4", false, true);
088                queryExpr4.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 queryPlan;
099        public TypedIOPort queryExpr1;
100        public TypedIOPort queryExpr2;
101        public TypedIOPort queryExpr3;
102        public TypedIOPort queryExpr4;
103
104        /**
105         * 
106         * @exception IllegalActionException
107         *                If there is no director.
108         */
109        public void fire() throws IllegalActionException {
110                super.fire();
111                StringToken inputToken = (StringToken) queryPlan.get(0);
112                String layerLegends = inputToken.stringValue();
113                // System.out.println(layerLegends);
114
115                // /ASHRAF'S CODE
116                try {
117                        org.w3c.dom.NodeList nl;
118
119                        DocumentBuilderFactory dbf;
120                        DocumentBuilder db;
121                        Document document;
122                        dbf = DocumentBuilderFactory.newInstance();
123                        db = dbf.newDocumentBuilder();
124                        document = db
125                                        .parse(new InputSource(new StringReader(layerLegends)));
126
127                        org.w3c.dom.Element element = document.getDocumentElement();
128                        String queryStrings[] = new String[4];
129                        org.w3c.dom.NodeList datasets = element
130                                        .getElementsByTagName("Dataset");
131                        if (datasets != null) {
132                                for (int i = 0; i < datasets.getLength(); i++) {
133                                        StringBuffer queryString = new StringBuffer();
134                                        String field = ((Element) datasets.item(i)).getAttribute(
135                                                        "column").trim();
136                                        String layer = ((Element) datasets.item(i)).getAttribute(
137                                                        "id").trim();
138                                        nl = ((Element) datasets.item(i))
139                                                        .getElementsByTagName("Term");
140                                        String value = "";
141                                        if (nl != null)
142                                                value = nl.item(0).getFirstChild().getNodeValue()
143                                                                .trim();
144                                        queryString.append(field + "='" + value + "' ");
145                                        for (int j = 1; j < nl.getLength(); j++) {
146                                                queryString.append(" OR ");
147                                                value = nl.item(j).getFirstChild().getNodeValue()
148                                                                .trim();
149                                                queryString.append(field + "='" + value + "' ");
150                                        }
151                                        queryStrings[i] = queryString.toString();
152                                }
153                                String utahString = queryStrings[0];
154                                String arizonaString = queryStrings[1];
155                                String nevadaString = queryStrings[2];
156                                String idahoString = queryStrings[3];
157                                // /END OF ASHRAF's CODE
158
159                                queryExpr1.broadcast(new StringToken(utahString));
160                                queryExpr2.broadcast(new StringToken(arizonaString));
161                                queryExpr3.broadcast(new StringToken(nevadaString));
162                                queryExpr4.broadcast(new StringToken(idahoString));
163
164                        }
165                } catch (Exception ex) {
166                        System.out.println(ex);
167                }
168        } // end of fire
169
170        /**
171         * Post fire the actor. Return false to indicate that the process has
172         * finished. If it returns true, the process will continue indefinitely.
173         */
174        public boolean postfire() {
175                return false;
176        } // end of postfire
177
178        /**
179         * Pre fire the actor. Calls the super class's prefire in case something is
180         * set there.
181         */
182        public boolean prefire() throws IllegalActionException {
183                return super.prefire();
184        } // end of prefire
185
186} // end of actor