001/*
002 $Id: OMSSAParameterSetStorage.java 3207 2009-04-09 06:48:11Z gregory $
003
004 Copyright (C) 2008 Olle Mansson
005
006 This file is part of Proteios.
007 Available at http://www.proteios.org/
008
009 Proteios is free software; you can redistribute it and/or modify it
010 under the terms of the GNU General Public License as published by
011 the Free Software Foundation; either version 2 of the License, or
012 (at your option) any later version.
013
014 Proteios is distributed in the hope that it will be useful, but
015 WITHOUT ANY WARRANTY; without even the implied warranty of
016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
017 General Public License for more details.
018
019 You should have received a copy of the GNU General Public License
020 along with this program; if not, write to the Free Software
021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
022 02111-1307, USA.
023 */
024package org.proteios.core;
025
026import org.proteios.core.data.OMSSAParameterSetStorageData;
027import org.proteios.io.OMSSAParameterFileUtil;
028import org.proteios.io.OMSSAParameterSet;
029
030//import se.lu.thep.waf.ActionException;
031
032import java.io.OutputStream;
033import java.util.Set;
034
035/**
036 * This class represents OMSSAParameterSetStorage.
037 * 
038 * @author olle
039 * @version 2.0
040 * @proteios.modified $Date: 2009-04-09 08:48:11 +0200 (Thu, 09 Apr 2009) $
041 */
042public class OMSSAParameterSetStorage
043                extends AnnotatedItem<OMSSAParameterSetStorageData>
044{
045        OMSSAParameterSetStorage(OMSSAParameterSetStorageData omssaParameterSetStorageData)
046        {
047                super(omssaParameterSetStorageData);
048        }
049
050        /**
051         * The type of item represented by this class.
052         * 
053         * @see Item#PROTEIOS_OMSSAPARAMETERSETSTORAGE
054         * @see #getType()
055         */
056        public static final Item TYPE = Item.PROTEIOS_OMSSAPARAMETERSETSTORAGE;
057
058
059        /**
060         * Get a query that returns OMSSAParameterSetStorage set items.
061         * 
062         * @return An {@link ItemQuery} object.
063         */
064        public static ItemQuery<OMSSAParameterSetStorage> getQuery()
065        {
066                return new ItemQuery<OMSSAParameterSetStorage>(OMSSAParameterSetStorage.class);
067        }
068
069
070        /**
071         * Get the OMSSA parameter file item coupled to this OMSSA parameter set
072         * 
073         * @return File The OMSSA parameter file item coupled to this OMSSA parameter set
074         */
075        public File getParameterFile()
076        {
077                return getDbControl().getItem(File.class, getData().getParameterFile());
078        }
079
080
081        /**
082         * Set the OMSSA parameter file item
083         * 
084         * @param parameterFile File The OMSSA parameter file item coupled to this OMSSA parameter set
085         */
086        public void setParameterFile(File parameterFile)
087        {
088                getData().setParameterFile(parameterFile.getData());
089        }
090
091        
092        /**
093         * Fetch OMSSAParameterSet from existing parameter file item.
094         * 
095         * @return OMSSAParameterSet The OMSSA parameter set stored in the parameter file item.
096         * @throws BaseException If no parameter file item exists.
097         */
098        public OMSSAParameterSet fetchOMSSAParameterSet()
099        {
100                // Get parameter file item coupled to this OMSSA parameter set storage
101                File inputFile = getParameterFile();
102                if (inputFile == null)
103                {
104                        throw new BaseException(
105                                "OMSSA parameter set cannot be fetched as not parameter file item is coupled to OMSSAParameterSetStorage item " + this);
106                }
107                // Create OMSSA parameter file utility object to use
108                OMSSAParameterFileUtil omssaParameterFileUtil = new OMSSAParameterFileUtil(inputFile);
109                // Get OMSSA parameter set from OMSSA parameters XML file
110                OMSSAParameterSet omssaParameterSet = omssaParameterFileUtil.fetchOMSSAParameterSet();
111                return omssaParameterSet;
112        }
113
114
115        /**
116         * Save OMSSAParameterSet in existing parameter file item.
117         * 
118         * @param omssaParameterSet OMSSAParameterSet The OMSSA parameter set to store.
119         * @throws BaseException If no parameter file item exists, or data cannot be saved in it.
120         */
121        public void save(OMSSAParameterSet omssaParameterSet)
122        {
123                // Check input values
124                if (omssaParameterSet == null)
125                {
126                        return;
127                }
128                // Get parameter file item coupled to this OMSSA parameter set storage
129                File outputFile = getParameterFile();
130                if (outputFile == null)
131                {
132                        throw new BaseException(
133                                "OMSSA parameter set cannot be saved as not parameter file item is coupled to OMSSAParameterSetStorage item " + this);
134                }
135                // Get output stream from file item
136                OutputStream oStream = null;
137                try
138                {
139                        oStream = outputFile.getUploadStream(false);
140                }
141                catch (Exception e)
142                {
143                        throw new BaseException(
144                                "Exception when trying to get upload stream for file " + outputFile + ": " + e);
145                }
146                // Create OMSSA parameter file utility object to use
147                OMSSAParameterFileUtil omssaParameterFileUtil = new OMSSAParameterFileUtil();
148                omssaParameterFileUtil.setOMSSAParameterSet(omssaParameterSet);
149                omssaParameterFileUtil.setXMLOutputStream(oStream);
150                // Create an XML stream writer for the XML data and write it to file item
151                omssaParameterFileUtil.exportOMSSAParameterSet(omssaParameterSet);
152                // Make sure that upload stream is closed
153                try
154                {
155                        if (oStream != null)
156                        {
157                                oStream.flush();
158                                // Close upload stream.
159                                oStream.close();
160                        }
161                }
162                catch (Exception e)
163                {
164                        throw new BaseException(
165                                "Exception when trying to close upload stream for file " + outputFile + ": " + e);
166                }
167        }
168
169
170        // -------------------------------------------
171        /*
172         * From the Identifiable interface
173         * -------------------------------------------
174         */
175        public Item getType()
176        {
177                return TYPE;
178        }
179
180
181        // -------------------------------------------
182        /**
183         * Always null.
184         */
185        public Set<Annotatable> getAnnotatableParents()
186                        throws BaseException
187        {
188                return null;
189        }
190
191
192        // -------------------------------------------
193        /*
194         * From the BasicItem class -------------------------------------------
195         */
196        /**
197         * Check that:
198         * <ul>
199         * <li>no item has been created from this omssaparametersetstorage
200         * </ul>
201         */
202        @Override
203        public boolean isUsed()
204                        throws BaseException
205        {
206                return false;
207        }
208}