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.srb;
031
032import java.util.HashMap;
033import java.util.Map;
034import java.util.Vector;
035
036import ptolemy.actor.TypedAtomicActor;
037import ptolemy.actor.TypedIOPort;
038import ptolemy.data.ArrayToken;
039import ptolemy.data.StringToken;
040import ptolemy.data.Token;
041import ptolemy.data.type.ArrayType;
042import ptolemy.data.type.BaseType;
043import ptolemy.kernel.CompositeEntity;
044import ptolemy.kernel.util.Attribute;
045import ptolemy.kernel.util.IllegalActionException;
046import ptolemy.kernel.util.NameDuplicationException;
047
048//////////////////////////////////////////////////////////////////////////
049//// SRBCreateQueryConditions
050/**
051 * <p>
052 * Creates an array of query conditions from user selections. Thereby
053 * translating user selcted conditions into an array of strings. Creates
054 * conditions for querying the SRB metadata from a user xml string conditions,
055 * returned by the BrowserUI actor.
056 * </p>
057 * <p>
058 * The SRBCreateQueryInterface outputs an HTML document with the appropriate
059 * conditions in HTML template form. The HTML document's file content can be
060 * further viewed by the BrowserUI actor. The BrowserUI outputs the XML output
061 * form of the above HTML document. This XML output can be further given to the
062 * SRBCreateQueryConditions actor to create an array of string conditions.
063 * </p>
064 * 
065 * @author Efrat Jaeger
066 * @version $Id: SRBCreateQueryConditions.java 13429 2007-02-01 20:18:02Z
067 *          berkley $
068 * @category.name srb
069 * @category.name put
070 */
071
072public class SRBCreateQueryConditions extends TypedAtomicActor {
073
074        /**
075         * Construct a constant source with the given container and name. Create the
076         * <i>value</i> parameter, initialize its value to the default value of an
077         * IntToken with value 1.
078         * 
079         * @param container
080         *            The container.
081         * @param name
082         *            The name of this actor.
083         * @exception IllegalActionException
084         *                If the entity cannot be contained by the proposed
085         *                container.
086         * @exception NameDuplicationException
087         *                If the container already has an actor with this name.
088         */
089        public SRBCreateQueryConditions(CompositeEntity container, String name)
090                        throws NameDuplicationException, IllegalActionException {
091                super(container, name);
092
093                xmlConditions = new TypedIOPort(this, "xmlConditions", true, false);
094                xmlConditions.setTypeEquals(BaseType.STRING);
095                new Attribute(xmlConditions, "_showName");
096
097                conditions = new TypedIOPort(this, "conditions", false, true);
098                conditions.setTypeEquals(new ArrayType(BaseType.STRING));
099                new Attribute(conditions, "_showName");
100
101                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
102                                + "width=\"60\" height=\"30\" " + "style=\"fill:white\"/>\n"
103                                + "<text x=\"4\" y=\"20\""
104                                + "style=\"font-size:16; fill:blue; font-family:SansSerif\">"
105                                + "[SRB]</text>\n" + "<text x=\"45\" y=\"22\""
106                                + "style=\"font-size:20; fill:blue; font-family:SansSerif\">"
107                                + "$</text>\n" + "</svg>\n");
108        }
109
110        /**
111         * The xml conditions obtained from user selection.
112         */
113        public TypedIOPort xmlConditions;
114
115        /**
116         * An array of conditions strings.
117         */
118        public TypedIOPort conditions;
119
120        // /////////////////////////////////////////////////////////////////
121        // // public methods ////
122        /**
123         * Translate user selcted conditions into an array of strings.
124         * 
125         * @exception IllegalActionException
126         *                If it is thrown if the SRB file cannot be accessed or the
127         *                current directory cannot be broadcasted.
128         */
129        public void fire() throws IllegalActionException {
130
131                try {
132                        String xmlStr = ((StringToken) xmlConditions.get(0)).stringValue();
133                        Map nameVal = new HashMap();
134                        int d_numAtts = 0, c_numAtts = 0;
135                        while (!xmlStr.trim().equals("</xmp>")) {
136
137                                int indStart = xmlStr.toLowerCase().indexOf("<name>");
138                                int indEnd = xmlStr.toLowerCase().indexOf("</name>");
139                                String name = xmlStr.substring(indStart + 6, indEnd);
140                                xmlStr = xmlStr.substring(indEnd + 7);
141                                indStart = xmlStr.toLowerCase().indexOf("<value>");
142                                indEnd = xmlStr.toLowerCase().indexOf("</value>");
143                                String value = xmlStr.substring(indStart + 7, indEnd);
144                                xmlStr = xmlStr.substring(indEnd + 8);
145                                if (name.startsWith("d_att"))
146                                        d_numAtts++;
147                                else if (name.startsWith("c_att"))
148                                        c_numAtts++;
149                                nameVal.put(name, value);
150                        }
151                        Vector condsVec = new Vector();
152                        for (int i = 0; i < d_numAtts; i++) {
153                                String att = (String) nameVal.get("d_att" + i);
154                                String op = (String) nameVal.get("d_op" + i);
155                                String val = (String) nameVal.get("d_newmdval" + i);
156                                if (!val.equals("")) {
157                                        if (op.equals("")) // op is empty - assume equals.
158                                                op = "=";
159                                        String condition = att + " " + op + " " + val;
160                                        condsVec.add(new StringToken(condition));
161                                }
162                        }
163
164                        condsVec.add(new StringToken("|")); // separating between dataset
165                                                                                                // and collection metadata.
166
167                        for (int i = 0; i < c_numAtts; i++) {
168                                String att = (String) nameVal.get("c_att" + i);
169                                String op = (String) nameVal.get("c_op" + i);
170                                String val = (String) nameVal.get("c_newmdval" + i);
171                                if (!val.equals("")) {
172                                        if (op.equals("")) // op is empty - assume equals.
173                                                op = "=";
174                                        String condition = att + " " + op + " " + val;
175                                        condsVec.add(new StringToken(condition));
176                                }
177                        }
178
179                        condsVec.add(new StringToken("|")); // separating between user
180                                                                                                // defined metadata and
181                                                                                                // predefined.
182
183                        String mcatVal = (String) nameVal.get("Annotation");
184                        if (!mcatVal.equals("")) {
185                                String mcatOp = (String) nameVal.get("AnnotationOp");
186                                String condition = "Annotation " + mcatOp + " " + mcatVal;
187                                condsVec.add(new StringToken(condition));
188                        }
189                        mcatVal = (String) nameVal.get("Annotator");
190                        if (!mcatVal.equals("")) {
191                                String mcatOp = (String) nameVal.get("AnnotatorOp");
192                                String condition = "Annotator " + mcatOp + " " + mcatVal;
193                                condsVec.add(new StringToken(condition));
194                        }
195                        mcatVal = (String) nameVal.get("Owner");
196                        if (!mcatVal.equals("")) {
197                                String mcatOp = (String) nameVal.get("OwnerOp");
198                                String condition = "Owner " + mcatOp + " " + mcatVal;
199                                condsVec.add(new StringToken(condition));
200                        }
201                        mcatVal = (String) nameVal.get("dataName");
202                        if (!mcatVal.equals("")) {
203                                String mcatOp = (String) nameVal.get("dataNameOp");
204                                String condition = "dataName " + mcatOp + " " + mcatVal;
205                                condsVec.add(new StringToken(condition));
206                        }
207                        mcatVal = (String) nameVal.get("collName");
208                        if (!mcatVal.equals("")) {
209                                String mcatOp = (String) nameVal.get("collNameOp");
210                                String condition = "collName " + mcatOp + " " + mcatVal;
211                                condsVec.add(new StringToken(condition));
212                        }
213                        mcatVal = (String) nameVal.get("dataType");
214                        if (!mcatVal.equals("")) {
215                                String mcatOp = (String) nameVal.get("dataTypeOp");
216                                String condition = "dataType " + mcatOp + " " + mcatVal;
217                                condsVec.add(new StringToken(condition));
218                        }
219
220                        Token[] condTokens = new Token[condsVec.size()];
221                        condsVec.toArray(condTokens);
222                        conditions.broadcast(new ArrayToken(condTokens));
223                } catch (Exception ex) {
224                        ex.printStackTrace();
225                        throw new IllegalActionException(this,
226                                        "Failed to create query condition: " + ex.getMessage()
227                                                        + ".");
228                }
229
230        }
231}