001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-08-09 22:47:01 +0000 (Thu, 09 Aug 2012) $' 
007 * '$Revision: 30395 $'
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.kepler.moml;
031
032import java.util.LinkedList;
033import java.util.List;
034import java.util.StringTokenizer;
035import java.util.Vector;
036
037import org.kepler.objectmanager.lsid.KeplerLSID;
038
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041import ptolemy.kernel.util.NamedObj;
042import ptolemy.kernel.util.Settable;
043import ptolemy.kernel.util.StringAttribute;
044
045/**
046 * This StringAttribute holds a string of KeplerLSIDs separated by colons.
047 * Do not use the setExpression method, instead use the addReferral method.
048 * 
049 * @author Aaron Schultz
050 * 
051 */
052public class NamedObjIdReferralList extends StringAttribute {
053        
054        public static final String NAME = "derivedFrom";
055
056        public NamedObjIdReferralList() {
057                super();
058        setVisibility(Settable.NONE);
059        }
060
061        public NamedObjIdReferralList(NamedObj container, String name)
062                        throws IllegalActionException, NameDuplicationException {
063                super(container, name);
064        setVisibility(Settable.NONE);
065        }
066
067    /** Add an LSID to the referral list. */
068        public void addReferral(KeplerLSID lsid) throws IllegalActionException {
069                String value = getExpression();
070                if (value.equals("")) {
071                        value += lsid.toString();
072                } else {
073                        value += ":" + lsid.toString();
074                }
075                setExpression(value);
076        }
077
078        /**
079         * This method returns a list of KeplerLSID objects. The first element in
080         * the list is the most recent KeplerLSID.
081         * 
082         *       * @throws Exception
083         */
084        public List<KeplerLSID> getReferrals() throws Exception {
085                String value = getExpression();
086                StringTokenizer st = new StringTokenizer(value, KeplerLSID.separator);
087                int cnt = st.countTokens();
088                if (cnt == 0) {
089                        return (List) new Vector(0);
090                }
091                if (cnt % 6 != 0) {
092                        throw new Exception(
093                                        NAME
094                                                        + " list must contain properly formatted KeplerLSID strings.");
095                }
096                int lsidCnt = cnt / 6;
097                Vector<KeplerLSID> referrals = new Vector<KeplerLSID>(lsidCnt);
098                for (int i = 0; i < lsidCnt; i++) {
099                        String lsidStr = st.nextToken() + KeplerLSID.separatorChar 
100                                        + st.nextToken() + KeplerLSID.separatorChar
101                                        + st.nextToken() + KeplerLSID.separatorChar
102                                        + st.nextToken() + KeplerLSID.separatorChar
103                                        + st.nextToken() + KeplerLSID.separatorChar
104                                        + st.nextToken() + KeplerLSID.separatorChar;
105                        KeplerLSID lsid = new KeplerLSID(lsidStr);
106                        referrals.add(0, lsid); // add to beginning of Vector
107                }
108                return (List<KeplerLSID>) referrals;
109
110        }
111        
112        public boolean hasReferral(KeplerLSID lsid) throws Exception {
113                List<KeplerLSID> lsids = getReferrals();
114                for (KeplerLSID thisLsid : lsids) {
115                        if (thisLsid.equals(lsid)) {
116                                return true;
117                        }
118                }
119                return false;
120        }
121
122    /** Remove an LSID from the referral list. */
123    public void removeReferral(KeplerLSID removeLSID) throws Exception {
124
125        // get the list of current referrals
126        List<KeplerLSID> currentLSIDs = getReferrals();
127
128        // remove from the current list
129        List<KeplerLSID> keepLSIDs = new LinkedList<KeplerLSID>();
130        for(KeplerLSID checkLSID : currentLSIDs)
131        {
132            if(!checkLSID.equals(removeLSID))
133            {
134                keepLSIDs.add(checkLSID);
135            }
136        }
137
138        // clear the list and add all the ones we want to keep
139        setExpression("");
140        for(KeplerLSID addLSID : keepLSIDs)
141        {
142            addReferral(addLSID);
143        }
144    }
145}