001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-08-24 22:47:39 +0000 (Mon, 24 Aug 2015) $' 
007 * '$Revision: 33633 $'
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.io.IOException;
033import java.util.Iterator;
034import java.util.Set;
035import java.util.TreeSet;
036
037import org.kepler.configuration.ConfigurationManager;
038import org.kepler.configuration.ConfigurationProperty;
039
040import edu.sdsc.grid.io.MetaDataCondition;
041import edu.sdsc.grid.io.MetaDataRecordList;
042import edu.sdsc.grid.io.MetaDataSelect;
043import edu.sdsc.grid.io.MetaDataSet;
044import edu.sdsc.grid.io.MetaDataTable;
045import edu.sdsc.grid.io.local.LocalFile;
046import edu.sdsc.grid.io.srb.SRBFile;
047import edu.sdsc.grid.io.srb.SRBFileSystem;
048import edu.sdsc.grid.io.srb.SRBMetaDataSet;
049import ptolemy.actor.NoTokenException;
050import ptolemy.actor.TypedAtomicActor;
051import ptolemy.actor.TypedIOPort;
052import ptolemy.actor.parameters.PortParameter;
053import ptolemy.data.ArrayToken;
054import ptolemy.data.IntToken;
055import ptolemy.data.ObjectToken;
056import ptolemy.data.StringToken;
057import ptolemy.data.Token;
058import ptolemy.data.type.ArrayType;
059import ptolemy.data.type.BaseType;
060import ptolemy.kernel.CompositeEntity;
061import ptolemy.kernel.util.Attribute;
062import ptolemy.kernel.util.IllegalActionException;
063import ptolemy.kernel.util.NameDuplicationException;
064
065//////////////////////////////////////////////////////////////////////////
066//// SRBCreateQueryInterface
067/**
068 * <p>
069 * Create an html interface for querying the SRB.
070 * 
071 * The following actor expects as input a reference to the SRB file system. This
072 * reference connection is obtained via the SRBConnect Actor in Kepler. <i>See
073 * SRBConnect and its documentation.</i>
074 * </p>
075 * <p>
076 * The file reference system is created with a unique SRB user account and with
077 * this connection reference as input the SCreateQueryInterface actor is able to
078 * gain access to the SRB file space. Once an alive SRB file connection system
079 * has been established the actor gets the attributes and number of conditions
080 * as inputs and creates an HTML template for all the conditions.
081 * </p>
082 * <p>
083 * <B>Actor Input:</B> Accepts a reference to the SRB files system, attributes
084 * and number of conditions.
085 * </p>
086 * <p>
087 * <B>Actor Output:</B> Outputs an HTML document with the appropriate conditions
088 * in HTML template form. The HTML document's file content can be further viewed
089 * by the BrowserUI actor. The BrowserUI outputs the XML output form of the
090 * above HTML document. This XML output can be further given to the
091 * SRBCreateQueryConditions actor to create an array of string conditions.
092 * </p>
093 * 
094 * @author Efrat Jaeger
095 * @version $Id: SRBCreateQueryInterface.java 13429 2007-02-01 20:18:02Z berkley
096 *          $
097 * @category.name srb
098 * @category.name put
099 */
100
101public class SRBCreateQueryInterface extends TypedAtomicActor {
102
103        /**
104         * Construct a constant source with the given container and name. Create the
105         * <i>value</i> parameter, initialize its value to the default value of an
106         * IntToken with value 1.
107         * 
108         * @param container
109         *            The container.
110         * @param name
111         *            The name of this actor.
112         * @exception IllegalActionException
113         *                If the entity cannot be contained by the proposed
114         *                container.
115         * @exception NameDuplicationException
116         *                If the container already has an actor with this name.
117         */
118        public SRBCreateQueryInterface(CompositeEntity container, String name)
119                        throws NameDuplicationException, IllegalActionException {
120                super(container, name);
121    
122    ConfigurationManager confMan = ConfigurationManager.getInstance();
123    //get the specific configuration we want
124    ConfigurationProperty commonProperty = confMan.getProperty(ConfigurationManager.getModule("common"));
125    ConfigurationProperty serversProperty = commonProperty.getProperty("servers.server");
126    ConfigurationProperty geonProperty = serversProperty.findProperties("name", "geon").get(0);
127    serverPath = geonProperty.getProperty("url").getValue();
128
129                SRBFileSystem = new TypedIOPort(this, "SRBFileSystem", true, false);
130                SRBFileSystem.setTypeEquals(BaseType.GENERAL);
131                new Attribute(SRBFileSystem, "_showName");
132
133                attributes = new PortParameter(this, "attributes");
134                attributes.setTypeEquals(new ArrayType(BaseType.STRING));
135                new Attribute(attributes, "_showName");
136
137                numberOfConditions = new PortParameter(this, "numberOfConditions");
138                numberOfConditions.setTypeEquals(BaseType.INT);
139                new Attribute(numberOfConditions, "_showName");
140
141                html = new TypedIOPort(this, "html", false, true);
142                html.setTypeEquals(BaseType.STRING);
143                new Attribute(html, "_showName");
144
145                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
146                                + "width=\"60\" height=\"30\" " + "style=\"fill:white\"/>\n"
147                                + "<text x=\"4\" y=\"20\""
148                                + "style=\"font-size:16; fill:blue; font-family:SansSerif\">"
149                                + "[SRB]</text>\n" + "<text x=\"45\" y=\"22\""
150                                + "style=\"font-size:20; fill:blue; font-family:SansSerif\">"
151                                + "$</text>\n" + "</svg>\n");
152        }
153
154        /**
155         * A pointer to the SRB file system.
156         */
157        public TypedIOPort SRBFileSystem;
158
159        /**
160         * HTML string.
161         */
162        public TypedIOPort html;
163
164        /**
165         * A list of attribute names for querying.
166         */
167        public PortParameter attributes;
168
169        /**
170         * The number of query conditions.
171         */
172        public PortParameter numberOfConditions;
173
174        // /////////////////////////////////////////////////////////////////
175        // // public methods ////
176        /**
177         * Get the physical location of SRB logical file paths.
178         * 
179         * @exception IllegalActionException
180         *                If it is thrown if the SRB file cannot be accessed or the
181         *                current directory cannot be broadcasted.
182         */
183        public void fire() throws IllegalActionException {
184
185                SRBFile srbFile;
186                LocalFile localFile;
187                String localFilePath;
188                String _exitCode = "";
189
190                StringBuffer sb = new StringBuffer();
191                sb.append("<HTML><HEAD> <BASE TARGET = \"sub29809\"> </HEAD>" + "\n");
192                sb
193                                .append("<body background=\"https://srb.npaci.edu/srb3.jpg\"><H2>Query MetaData</H2>"
194                                                + "\n");
195                sb.append("<FORM METHOD=\"post\" ACTION=\"" + serverPath
196                                + "pt2/jsp/pts.jsp\"  >" + "\n");
197                // sb.append("<input type=hidden name=\"function\" value=\"browsequeryvalues\">");
198                sb.append("<STRONG><FONT COLOR=#FF0000>File Metadata</STRONG></FONT>");
199                sb.append("<table><tr><th align=center>MetaData Name</th>" + "\n");
200                sb
201                                .append("<th></th><th align = center>MetaData Value</th></tr><tr></tr>"
202                                                + "\n");
203
204                try {
205                        // make sure there is an alive connection.
206                        try {
207                                srbFileSystem.getHost();
208                        } catch (Exception ex) { // connection was closed.
209                                srbFileSystem = null;
210                                ObjectToken SRBConOT = null;
211                                try { // try to get a new connection in case the previous one
212                                                // has terminated.
213                                        SRBConOT = (ObjectToken) SRBFileSystem.get(0);
214                                } catch (NoTokenException ntex) {
215                                }
216                                if (SRBConOT != null) {
217                                        srbFileSystem = (SRBFileSystem) SRBConOT.getValue();
218                                }
219                        }
220                        if (srbFileSystem == null) {
221                                throw new IllegalActionException(this,
222                                                "No SRB connection available in actor "
223                                                                + this.getName() + ".");
224                        }
225
226                        int _numCond = NUMCOND;
227                        numberOfConditions.update();
228                        if (!numberOfConditions.getExpression().trim().equals("")) {
229                                _numCond = ((IntToken) numberOfConditions.getToken())
230                                                .intValue();
231                        }
232
233                        String[] atts = null;
234                        attributes.update();
235                        if (!attributes.getExpression().trim().equals("")) {
236                                Token[] attTokens = ((ArrayToken) attributes.getToken())
237                                                .arrayValue();
238                                atts = new String[attTokens.length];
239                                for (int i = 0; i < attTokens.length; i++) {
240                                        atts[i] = ((StringToken) attTokens[i]).stringValue();
241                                }
242                        } else {
243                                atts = _getAttributes(SRBMetaDataSet.DEFINABLE_METADATA_FOR_FILES);
244                                if (atts == null) {
245                                        srbFileSystem = SRBUtil.closeConnection(srbFileSystem);
246                                        throw new IllegalActionException(this,
247                                                        "Failed to get query attributes in actor "
248                                                                        + this.getName() + ".");
249                                }
250                        }
251                        StringBuffer opsb = new StringBuffer();
252                        opsb
253                                        .append("<OPTION SELECTED> = <OPTION value=\"not=\"> &lt;&gt; ");
254                        opsb
255                                        .append("<OPTION value=\"gt\"> &gt; <OPTION value=\"num_gt\"> num &gt; <OPTION value=\"lt\"> &lt; <OPTION value=\"num_lt\"> num &lt; <OPTION value=\"ge\"> &gt;= ");
256                        opsb
257                                        .append("<OPTION value=\"num_ge\"> num &gt;= <OPTION value=\"le\"> &lt;= <OPTION value=\"num_le\"> num &lt;= <OPTION> between <OPTION value=\"num_between\"> num between ");
258                        opsb
259                                        .append("<OPTION value=\"not_between\"> not between <OPTION value=\"num_not_between\"> num not between <OPTION> like <OPTION value=\"not_like\"> ");
260                        opsb
261                                        .append("not like <OPTION value=\"sounds_like\"> sounds like <OPTION value=\"sounds_not_like\"> sounds not like <OPTION> in <OPTION value=\"not_in\"> ");
262                        opsb
263                                        .append("not in <OPTION> contains <OPTION value=\"not_contains\"> not contains </SELECT></td>"
264                                                        + "\n");
265
266                        // create templates for all conditions (and two more..).
267                        for (int i = 0; i < _numCond; i++) {
268                                sb.append("<tr><td><SELECT SIZE=1 NAME=d_att" + i
269                                                + "><OPTION SELECTED> ");
270                                for (int j = 0; j < atts.length; j++) {
271                                        sb.append("<OPTION> " + atts[j]);
272                                }
273                                sb.append("</SELECT></td>");
274                                sb.append("<td><SELECT SIZE=1 NAME=\"d_op" + i + "\">"
275                                                + opsb.toString());
276                                sb.append("<td><INPUT NAME=\"d_newmdval" + i
277                                                + "\", VALUE= \"\", SIZE=20,12></td></tr>" + "\n");
278                        }
279                        sb
280                                        .append("<tr><td  align=right><input type=hidden name=\"c1\" value=\"74\">Annotation</td>");
281                        sb.append("<td><SELECT SIZE=1 NAME=\"AnnotationOp\">"
282                                        + opsb.toString());
283                        sb
284                                        .append("<td><INPUT NAME=\"Annotation\", VALUE= \"\", SIZE=20,12></td></tr>"
285                                                        + "\n");
286
287                        sb
288                                        .append("<tr><td  align=right><input type=hidden name=\"c1\" value=\"72\">Annotator</td>");
289                        sb
290                                        .append("<td align=center><input type=hidden name=\"AnnotatorOp\" value=\"=\"><B>=</B></td>");
291                        sb
292                                        .append("<td><INPUT NAME=\"Annotator\", VALUE= \"kepler_dev@sdsc\", SIZE=20,12></td></tr>"
293                                                        + "\n");
294
295                        sb
296                                        .append("<tr><td  align=right><input type=hidden name=\"c1\" value=\"35\">Owner</td>");
297                        sb
298                                        .append("<td align=center><input type=hidden name=\"OwnerOp\" value=\"=\"><B>=</B></td>");
299                        sb
300                                        .append("<td><INPUT NAME=\"Owner\", VALUE= \"kepler_dev@sdsc\", SIZE=20,12></td></tr>"
301                                                        + "\n");
302
303                        sb
304                                        .append("<tr><td  align=right><input type=hidden name=\"c1\" value=\"2\">Data Name</td>");
305                        sb.append("<td><SELECT SIZE=1 NAME=\"dataNameOp\">"
306                                        + opsb.toString());
307                        sb
308                                        .append("<td><INPUT NAME=\"dataName\", VALUE= \"\", SIZE=20,12></td></tr>"
309                                                        + "\n");
310
311                        sb
312                                        .append("<tr><td  align=right><input type=hidden name=\"c1\" value=\"15\">Collection Name</td>");
313                        sb.append("<td><SELECT SIZE=1 NAME=\"collNameOp\">"
314                                        + opsb.toString());
315                        sb
316                                        .append("<td><INPUT NAME=\"collName\", VALUE= \"\", SIZE=20,12></td></tr>"
317                                                        + "\n");
318
319                        sb
320                                        .append("<tr><td  align=right><input type=hidden name=\"c1\" value=\"4\">Data Type</td>");
321                        sb
322                                        .append("<td align=center><input type=hidden name=\"dataTypeOp\" value=\"=\"><B>=</B></td>");
323                        sb
324                                        .append("<td><SELECT SIZE=1 NAME=dataType><OPTION SELECTED> <OPTION> AIX DLL<OPTION> AIX Executable<OPTION> AVI");
325                        sb
326                                        .append("<OPTION> C code<OPTION> C include file<OPTION> Cray DLL<OPTION> Cray Executable<OPTION> DICOM header");
327                        sb
328                                        .append("<OPTION> DICOM image<OPTION> DLL<OPTION> DVI format<OPTION> Document<OPTION> Excel Spread Sheet");
329                        sb
330                                        .append("<OPTION> Executable<OPTION> FITS image<OPTION> LaTeX format<OPTION> MPEG<OPTION> MPEG 3 Movie");
331                        sb
332                                        .append("<OPTION> MPEG Movie<OPTION> MSWord Document<OPTION> Mac DLL<OPTION> Mac Executable");
333                        sb
334                                        .append("<OPTION> Mac OSX Executable<OPTION> Movie<OPTION> NSF Award Abstracts<OPTION> NT DLL");
335                        sb
336                                        .append("<OPTION> NT Executable<OPTION> PDF Document<OPTION> Postscript format<OPTION> Power Point Slide");
337                        sb
338                                        .append("<OPTION> Quicktime Movie<OPTION> SGI DLL<OPTION> SGI Executable<OPTION> SGML File<OPTION> SQL script");
339                        sb
340                                        .append("<OPTION> Slide<OPTION> Solaris DLL<OPTION> Solaris Executable<OPTION> Spread Sheet");
341                        sb
342                                        .append("<OPTION> Troff format<OPTION> URL<OPTION> Wave Audio<OPTION> Word format<OPTION> ascii compressed Huffman");
343                        sb
344                                        .append("<OPTION> ascii compressed Lempel-Ziv<OPTION> ascii text<OPTION> audio streams<OPTION> binary file");
345                        sb
346                                        .append("<OPTION> compressed PDB file<OPTION> compressed file<OPTION> compressed mmCIF file");
347                        sb
348                                        .append("<OPTION> compressed tar file<OPTION> data file<OPTION> database<OPTION> database shadow object");
349                        sb
350                                        .append("<OPTION> datascope data<OPTION> deleted<OPTION> directory shadow object<OPTION> ebcdic compressed Huffman");
351                        sb
352                                        .append("<OPTION> ebcdic compressed Lempel-Ziv<OPTION> ebcdic text<OPTION> email<OPTION> fig image");
353                        sb
354                                        .append("<OPTION> fortran code<OPTION> generic<OPTION> gif image<OPTION> home<OPTION> html<OPTION> image");
355                        sb
356                                        .append("<OPTION> java code<OPTION> jpeg image<OPTION> level1<OPTION> level2<OPTION> level3<OPTION> level4");
357                        sb
358                                        .append("<OPTION> library code<OPTION> link code<OPTION> object code<OPTION> orb data<OPTION> pbm image");
359                        sb
360                                        .append("<OPTION> perl script<OPTION> print-format<OPTION> printout<OPTION> program code<OPTION> realAudio");
361                        sb
362                                        .append("<OPTION> realVideo<OPTION> shadow object<OPTION> streams<OPTION> tar file<OPTION> tcl script<OPTION> text");
363                        sb
364                                        .append("<OPTION> tiff image<OPTION> uuencoded tiff<OPTION> video streams<OPTION> xml</SELECT></td></tr>"
365                                                        + "\n");
366                        sb.append("</table><br/>");
367
368                        if (attributes.getExpression().trim().equals("")) {
369                                atts = _getAttributes(SRBMetaDataSet.DEFINABLE_METADATA_FOR_DIRECTORIES);
370                                System.out.println("atts for dirs");
371                        }
372
373                        sb
374                                        .append("<STRONG><FONT COLOR=#FF0000>Collection Metadata</STRONG></FONT>");
375                        sb.append("<table><tr><th align=center>MetaData Name</th>" + "\n");
376                        sb
377                                        .append("<th></th><th align = center>MetaData Value</th></tr><tr></tr>"
378                                                        + "\n");
379
380                        for (int i = 0; i < _numCond; i++) {
381                                sb.append("<tr><td><SELECT SIZE=1 NAME=c_att" + i
382                                                + "><OPTION SELECTED> ");
383                                for (int j = 0; j < atts.length; j++) {
384                                        sb.append("<OPTION> " + atts[j]);
385                                }
386                                sb.append("</SELECT></td>");
387                                sb.append("<td><SELECT SIZE=1 NAME=\"c_op" + i + "\">"
388                                                + opsb.toString());
389                                sb.append("<td><INPUT NAME=\"c_newmdval" + i
390                                                + "\", VALUE= \"\", SIZE=20,12></td></tr>" + "\n");
391                        }
392                        sb.append("</table><br/>");
393
394                        sb.append("<INPUT TYPE=\"submit\" VALUE=\"Find\">" + "\n");
395                        // sb.append("<INPUT TYPE=\"reset\" VALUE=\"Clear\">"); // ADD!!!
396                        sb.append("</FORM> \n </body></html>");
397
398                        System.out.println(sb.toString());
399                        html.broadcast(new StringToken(sb.toString()));
400                } catch (Exception ex) {
401                        srbFile = null;
402                        srbFileSystem = SRBUtil.closeConnection(srbFileSystem);
403                        ex.printStackTrace();
404                        throw new IllegalActionException(this, "Exception in actor "
405                                        + this.getName() + ": " + ex.getMessage() + ".");
406                }
407        }
408
409        /**
410         * Initialize the srb file system to null.
411         */
412        public void initialize() throws IllegalActionException {
413                super.initialize();
414                srbFileSystem = null;
415        }
416
417        /**
418         * Disconnect from SRB.
419         */
420        public void wrapup() {
421                srbFileSystem = SRBUtil.closeConnection(srbFileSystem);
422        }
423
424        // /////////////////////////////////////////////////////////////////
425        // // private methods ////
426
427        /**
428         * Querying the SRB metadata from the head to get all attributes.
429         * 
430         * @param type
431         *       */
432        private String[] _getAttributes(String type) {
433                /*
434                 * String hd = srbFileSystem.getHomeDirectory(); SRBFile homeDir = new
435                 * SRBFile(srbFileSystem, hd);
436                 * 
437                 * if (homeDir.isDirectory()) {
438                 * System.out.println("homeDir is a directory"); }
439                 * 
440                 * SRBFile srbFile = new SRBFile(srbFileSystem, ".");
441                 */
442                String[] attsArr = null;
443                try {
444                        // All user files (recursive).
445                        MetaDataCondition conditions[] = { MetaDataSet.newCondition(
446                                        SRBMetaDataSet.USER_GROUP_NAME, MetaDataCondition.LIKE,
447                                        srbFileSystem.getUserName()), };
448
449                        // The return fields.
450                        String[] selectFieldNames = { type };
451                        MetaDataSelect selects[] = MetaDataSet
452                                        .newSelection(selectFieldNames);
453
454                        // querying mcat
455                        MetaDataRecordList[] rl = srbFileSystem.query(conditions, selects);
456                        Set atts = new TreeSet();
457                        for (int i = 0; i < rl.length; i++) {
458                                // getting the metadata field index, if exists.
459                                int ind = rl[i].getFieldIndex(type);
460                                if (ind != -1) {
461                                        int dirInd = rl[i]
462                                                        .getFieldIndex(SRBMetaDataSet.DIRECTORY_NAME);
463                                        if (dirInd != -1) {
464                                                String dirName = rl[i].getStringValue(dirInd);
465                                                // ignore files that have been deleted.
466                                                int trashInd = dirName.toLowerCase().indexOf("/trash/");
467                                                if (trashInd == -1) {
468                                                        MetaDataTable mdt = rl[i].getTableValue(ind);
469                                                        int rc = mdt.getRowCount();
470                                                        for (int j = 0; j < rc; j++) {
471                                                                System.out.println(mdt.getStringValue(j, 0));
472                                                                atts.add(mdt.getStringValue(j, 0));
473                                                        }
474                                                        System.out.println("here");
475                                                }
476                                        }
477                                }
478                        }
479                        attsArr = new String[atts.size()];
480                        Iterator it = atts.iterator();
481                        int i = 0;
482                        while (it.hasNext()) {
483                                attsArr[i++] = (String) it.next();
484                        }
485                } catch (IOException ex) {
486                        ex.printStackTrace();
487                }
488
489                return attsArr;
490        }
491
492        // /////////////////////////////////////////////////////////////////
493        // // private members ////
494
495        /**
496         * A reference to the SRB file system
497         */
498        private SRBFileSystem srbFileSystem = null;
499
500        /**
501         * A static number of condition
502         */
503        private static final int NUMCOND = 5;
504
505        /**
506         * Path to the geon server url in the config file.
507         */
508        private static final String SERVERPATH = "//servers/server[@name=\"geon\"]/url";
509
510        /**
511         * URL to backend server
512         */
513        private String serverPath;
514
515}