001/*
002 * Copyright (c) 2015 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-12-22 00:34:45 +0000 (Tue, 22 Dec 2015) $' 
007 * '$Revision: 34363 $'
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 */
029package org.kepler.gis.actor.io;
030
031import java.io.File;
032
033import org.kepler.gis.actor.CRSActor;
034import org.kepler.gis.data.VectorToken;
035
036import ptolemy.actor.TypedIOPort;
037import ptolemy.actor.parameters.PortParameter;
038import ptolemy.data.StringToken;
039import ptolemy.data.expr.StringParameter;
040import ptolemy.data.type.BaseType;
041import ptolemy.kernel.CompositeEntity;
042import ptolemy.kernel.util.IllegalActionException;
043import ptolemy.kernel.util.NameDuplicationException;
044import ptolemy.kernel.util.SingletonAttribute;
045
046/** Base class actor for writing GIS data sets.
047 * 
048 *  @author Daniel Crawl
049 *  @version $Id: GISWriter.java 34363 2015-12-22 00:34:45Z crawl $
050 */
051public abstract class GISWriter extends CRSActor {
052
053    /** Create a new GISWriter in a container with the specified name. */
054    public GISWriter(CompositeEntity container, String name) throws IllegalActionException, NameDuplicationException {
055        super(container, name);
056
057        filename = new PortParameter(this, "filename");
058        filename.setTypeEquals(BaseType.STRING);
059        filename.getPort().setTypeEquals(BaseType.STRING);
060        filename.setStringMode(true);
061        new SingletonAttribute(filename.getPort(), "_showName");
062         
063        data = new TypedIOPort(this, "data", true, false);
064        data.setTypeEquals(VectorToken.VECTOR);
065        new SingletonAttribute(data, "_showName");
066
067        type = new StringParameter(this, "type");
068
069        done = new TypedIOPort(this, "done", false, true);
070        done.setTypeEquals(BaseType.STRING);    
071    }
072
073    
074    @Override
075    public void fire() throws IllegalActionException {
076        super.fire();
077        
078        filename.update();
079        _fileNameStr = ((StringToken)filename.getToken()).stringValue();
080        _outputFile = new File(_fileNameStr);
081        
082        
083        if(_outputFile.exists() && !_outputFile.canWrite()) {
084            throw new IllegalActionException(this, "Output file exists and not writeable.");
085            //outputFile.delete();
086        }
087
088        _typeStr = type.stringValue();
089        if(_typeStr != null) {
090            _typeStr = _typeStr.trim();
091        }
092
093    }
094    
095    /** The output format type. */
096    public StringParameter type;
097    
098    /** The name of the output file. */
099    public PortParameter filename;
100    
101    /** The input data set to write. */
102    public TypedIOPort data;
103        
104    /** The name of the output file is sent to this port when the
105     *  data has been written.
106     */
107    public TypedIOPort done;
108
109    ///////////////////////////////////////////////////////////////////
110    ////                      protected fields                   //////
111    
112    /** The output file name. */
113    protected String _fileNameStr;
114    
115    /** The output file. */
116    protected File _outputFile;
117    
118    /** The output type. */
119    protected String _typeStr;
120}