001/*
002 $Id: SpectrumSearch.java 3207 2009-04-09 06:48:11Z gregory $
003
004 Copyright (C) 2006 Fredrik Levander
005 Copyright (C) 2007 Fredrik Levander, Gregory Vincic, Olle Mansson
006
007 This file is part of Proteios.
008 Available at http://www.proteios.org/
009
010 Proteios is free software; you can redistribute it and/or modify it
011 under the terms of the GNU General Public License as published by
012 the Free Software Foundation; either version 2 of the License, or
013 (at your option) any later version.
014
015 Proteios is distributed in the hope that it will be useful, but
016 WITHOUT ANY WARRANTY; without even the implied warranty of
017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
018 General Public License for more details.
019
020 You should have received a copy of the GNU General Public License
021 along with this program; if not, write to the Free Software
022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
023 02111-1307, USA.
024 */
025package org.proteios.core;
026
027import org.proteios.core.data.DigestParameterData;
028import org.proteios.core.data.SearchDatabaseData;
029import org.proteios.core.data.SearchModificationData;
030import org.proteios.core.data.SearchResultData;
031import org.proteios.core.data.SpectrumSearchData;
032import java.util.Date;
033import java.util.HashSet;
034import java.util.Iterator;
035import java.util.Set;
036import java.util.SortedSet;
037import java.util.TreeSet;
038
039/**
040 * This class represent spectrumsearch.
041 * 
042 * @author Fredrik
043 * @version 2.0
044 * @proteios.modified $Date: 2009-04-09 08:48:11 +0200 (Thu, 09 Apr 2009) $
045 */
046public class SpectrumSearch
047                extends AnnotatedItem<SpectrumSearchData>
048{
049        SpectrumSearch(SpectrumSearchData spectrumSearchData)
050        {
051                super(spectrumSearchData);
052        }
053
054        /**
055         * The type of item represented by this class.
056         * 
057         * @see Item#PROTEIOS_SPECTRUMSEARCH
058         * @see #getType()
059         */
060        public static final Item TYPE = Item.PROTEIOS_SPECTRUMSEARCH;
061
062
063        /**
064         * Get a query that returns spectrumsearchset items.
065         * 
066         * @return An {@link ItemQuery} object.
067         */
068        public static ItemQuery<SpectrumSearch> getQuery()
069        {
070                return new ItemQuery<SpectrumSearch>(SpectrumSearch.class);
071        }
072
073
074        // -------------------------------------------
075        /*
076         * From the Identifiable interface
077         * -------------------------------------------
078         */
079        public Item getType()
080        {
081                return TYPE;
082        }
083
084
085        // -------------------------------------------
086        /**
087         * Always null.
088         */
089        public Set<Annotatable> getAnnotatableParents()
090                        throws BaseException
091        {
092                return null;
093        }
094
095
096        // -------------------------------------------
097        /*
098         * From the BasicItem class -------------------------------------------
099         */
100        /**
101         * Check that:
102         * <ul>
103         * <li>no item has been created from this peaklistset
104         * </ul>
105         */
106        @Override
107        public boolean isUsed()
108                        throws BaseException
109        {
110                return false;
111        }
112
113
114        /**
115         * Get the {@link PeakListSet} this SpectrumSearch is associated with.
116         * 
117         * @return The <code>PeakListSet</code> item or null if not known
118         * @throws BaseException If there is another error
119         */
120        public PeakListSet getPeakListSet()
121                        throws BaseException
122        {
123                return getDbControl().getItem(PeakListSet.class,
124                        getData().getPeakListSet());
125        }
126
127
128        /**
129         * Set the {@link PeakListSet} this SpectrumSearch is associated with.
130         * 
131         * @param peakListSet The new <code>PeakListSet</code> item
132         * @throws BaseException If there is another error
133         */
134        public void setPeakListSet(PeakListSet peakListSet)
135                        throws BaseException
136        {
137                getData().setPeakListSet(peakListSet.getData());
138        }
139
140
141        /**
142         * Get the earch engine {@link Software} this SpectrumSearch is associated
143         * with.
144         * 
145         * @return The <code>Software</code> item or null if not known
146         * @throws BaseException If there is another error
147         */
148        public Software getSearchEngine()
149                        throws BaseException
150        {
151                return getDbControl().getItem(Software.class,
152                        getData().getSearchEngine());
153        }
154
155
156        /**
157         * Set the search engine {@link Software} this SpectrumSearch is associated
158         * with.
159         * 
160         * @param software The new <code>Software</code> item
161         * @throws BaseException If there is another error
162         */
163        public void setSearchEngine(Software software)
164                        throws BaseException
165        {
166                getData().setSearchEngine(software.getData());
167        }
168
169
170        /**
171         * Add a DigestParameter
172         * 
173         * @param dp The DigestParameter to add to the Set
174         */
175        public void addDigestParameter(DigestParameter dp)
176        {
177                dp.setSpectrumSearch(this);
178                getData().getDigestParameters().add(dp.getData());
179        }
180
181
182        /**
183         * @return Returns the digestParameters.
184         */
185        public Set<DigestParameter> getDigestParameters()
186                        throws ItemNotFoundException, BaseException
187        {
188                Set<DigestParameter> digestParameters = new HashSet<DigestParameter>();
189                try
190                {
191                        Iterator<DigestParameterData> it = getData().getDigestParameters()
192                                .iterator();
193                        while (it.hasNext())
194                        {
195                                digestParameters.add(getDbControl().getItem(
196                                        DigestParameter.class, it.next()));
197                        }
198                }
199                catch (org.hibernate.ObjectNotFoundException e)
200                {
201                        throw new ItemNotFoundException(HibernateUtil.getShortEntityName(e
202                                .getEntityName()) + "[id=" + e.getIdentifier() + "]");
203                }
204                return digestParameters;
205        }
206
207
208        /**
209         * Set the {@link DigestParameter} digestParameters set.
210         * 
211         * @param digestParameters The new <code>Set<DigestParameter></code>
212         *        item.
213         * @throws InvalidDataException If dataProcessingStep is null
214         * @throws BaseException If there is another error
215         */
216        public void setDigestParameters(Set<DigestParameter> digestParameters)
217        {
218                Set<DigestParameterData> dpData = new HashSet<DigestParameterData>();
219                Iterator<DigestParameter> it = digestParameters.iterator();
220                while (it.hasNext())
221                {
222                        dpData.add(it.next().getData());
223                }
224                getData().setDigestParameters(dpData);
225        }
226
227
228        /**
229         * @return Returns the searchDatabases.
230         */
231        public Set<SearchDatabase> getSearchDatabases()
232                        throws ItemNotFoundException, BaseException
233        {
234                Set<SearchDatabase> searchDatabases = new HashSet<SearchDatabase>();
235                try
236                {
237                        Iterator<SearchDatabaseData> it = getData().getSearchDatabases()
238                                .iterator();
239                        while (it.hasNext())
240                        {
241                                searchDatabases.add(getDbControl().getItem(
242                                        SearchDatabase.class, it.next()));
243                        }
244                }
245                catch (org.hibernate.ObjectNotFoundException e)
246                {
247                        throw new ItemNotFoundException(HibernateUtil.getShortEntityName(e
248                                .getEntityName()) + "[id=" + e.getIdentifier() + "]");
249                }
250                return searchDatabases;
251        }
252
253
254        /**
255         * Set the {@link SearchDatabase} searchDatabases set.
256         * 
257         * @param searchDatabases The new <code>Set<SearchDatabase></code> item.
258         * @throws InvalidDataException If dataProcessingStep is null
259         * @throws BaseException If there is another error
260         */
261        public void setSearchDatabases(Set<SearchDatabase> searchDatabases)
262        {
263                Set<SearchDatabaseData> dpData = new HashSet<SearchDatabaseData>();
264                Iterator<SearchDatabase> it = searchDatabases.iterator();
265                while (it.hasNext())
266                {
267                        dpData.add(it.next().getData());
268                }
269                getData().setSearchDatabases(dpData);
270        }
271
272
273        /**
274         * Add a SearchResult and set the association back to this SpectrumSearch
275         * 
276         * @param sr The Search Result to add to the SortedSet
277         * @throws InvalidDataException if the search result could not be added
278         *         (probable reason=duplicate indexes)
279         */
280        public void addSearchResult(SearchResult sr)
281                        throws InvalidDataException
282        {
283                sr.setSpectrumSearch(this);
284                if (!getData().getSearchResults().add(sr.getData()))
285                {
286                        throw new InvalidDataException("Couldn't add SearchResult");
287                }
288        }
289
290
291        /**
292         * Add a SearchDatabase
293         * 
294         * @param db The Search Database to add to the Set
295         */
296        public void addSearchDatabase(SearchDatabase db)
297        {
298                db.setSpectrumSearch(this);
299                getData().getSearchDatabases().add(db.getData());
300        }
301
302
303        /**
304         * @return Returns the searchResults.
305         */
306        public SortedSet<SearchResult> getSearchResults()
307                        throws ItemNotFoundException, BaseException
308        {
309                SortedSet<SearchResult> searchResults = new TreeSet<SearchResult>();
310                try
311                {
312                        Iterator<SearchResultData> it = getData().getSearchResults()
313                                .iterator();
314                        while (it.hasNext())
315                        {
316                                searchResults.add(getDbControl().getItem(SearchResult.class,
317                                        it.next()));
318                        }
319                }
320                catch (org.hibernate.ObjectNotFoundException e)
321                {
322                        throw new ItemNotFoundException(HibernateUtil.getShortEntityName(e
323                                .getEntityName()) + "[id=" + e.getIdentifier() + "]");
324                }
325                return searchResults;
326        }
327
328
329        /**
330         * Set the {@link SearchResult} searchResults set.
331         * 
332         * @param searchResults The new <code>Set<SearchResult></code> item.
333         * @throws InvalidDataException If searchResults is null
334         * @throws BaseException If there is another error
335         */
336        public void setSearchResults(SortedSet<SearchResult> searchResults)
337        {
338                SortedSet<SearchResultData> dpData = new TreeSet<SearchResultData>();
339                Iterator<SearchResult> it = searchResults.iterator();
340                while (it.hasNext())
341                {
342                        dpData.add(it.next().getData());
343                }
344                getData().setSearchResults(dpData);
345        }
346
347
348        /**
349         * @return Returns the analysis date.
350         */
351        public Date getAnalysisTime()
352        {
353                return getData().getAnalysisTime();
354        }
355
356
357        /**
358         * @param date The analysis time.
359         */
360        public void setAnalysisTime(Date date)
361        {
362                getData().setAnalysisTime(date);
363        }
364
365
366        /**
367         * Get the result file that this spectrumsearch was generated from
368         * 
369         * @return the File
370         */
371        public File getResultFile()
372        {
373                return getDbControl().getItem(File.class, getData().getResultFile());
374        }
375
376
377        /**
378         * Set the result file
379         * 
380         * @param resultFile The results File this spectrumSearch was generated from
381         */
382        public void setResultFile(File resultFile)
383        {
384                getData().setResultFile(resultFile.getData());
385        }
386
387        /**
388         * The maximum length of the inputSpectrumFileName string that can be stored
389         * in the database.
390         * 
391         * @see #setInputSpectrumFileName(String)
392         */
393        public static final int MAX_INPUTSPECTRUMFILENAME_LENGTH = SpectrumSearchData.MAX_INPUTSPECTRUMFILENAME_LENGTH;
394
395
396        /**
397         * Get the input filename of this <code>SpectrumSearch</code>.
398         * 
399         * @return the filename
400         */
401        public String getInputSpectrumFileName()
402        {
403                return getData().getInputSpectrumFileName();
404        }
405
406
407        /**
408         * Set the inputSpectrumFileName for this <code>SpectrumSearch</code>
409         * item. The value may be null but must not be longer than the value
410         * specified by the {@link #MAX_INPUTSPECTRUMFILENAME_LENGTH} constant.
411         * 
412         * @param inputSpectrumFileName The new inputSpectrumFileName for this item
413         * @throws InvalidDataException If the inputSpectrumFileName is longer than
414         *         {@link #MAX_INPUTSPECTRUMFILENAME_LENGTH}
415         */
416        public void setInputSpectrumFileName(String inputSpectrumFileName)
417                        throws InvalidDataException
418        {
419                getData().setInputSpectrumFileName(
420                        StringUtil.setNullableString(inputSpectrumFileName,
421                                "inputSpectrumFileName", MAX_INPUTSPECTRUMFILENAME_LENGTH));
422        }
423
424
425        /**
426         * Add a SearchModification
427         * 
428         * @param dp The SearchModification to add to the Set
429         */
430        public void addSearchModification(SearchModification dp)
431        {
432                dp.setSpectrumSearch(this);
433                getData().getSearchModifications().add(dp.getData());
434        }
435
436
437        /**
438         * @return Returns the searchModifications.
439         */
440        public Set<SearchModification> getSearchModifications()
441                        throws ItemNotFoundException, BaseException
442        {
443                Set<SearchModification> searchModifications = new HashSet<SearchModification>();
444                try
445                {
446                        Iterator<SearchModificationData> it = getData()
447                                .getSearchModifications().iterator();
448                        while (it.hasNext())
449                        {
450                                searchModifications.add(getDbControl().getItem(
451                                        SearchModification.class, it.next()));
452                        }
453                }
454                catch (org.hibernate.ObjectNotFoundException e)
455                {
456                        throw new ItemNotFoundException(HibernateUtil.getShortEntityName(e
457                                .getEntityName()) + "[id=" + e.getIdentifier() + "]");
458                }
459                return searchModifications;
460        }
461
462
463        /**
464         * Set the {@link SearchModification} searchModifications set.
465         * 
466         * @param searchModifications The new <code>Set<SearchModification></code>
467         *        item.
468         * @throws InvalidDataException If dataProcessingStep is null
469         * @throws BaseException If there is another error
470         */
471        public void setSearchModifications(
472                        Set<SearchModification> searchModifications)
473        {
474                Set<SearchModificationData> dpData = new HashSet<SearchModificationData>();
475                Iterator<SearchModification> it = searchModifications.iterator();
476                while (it.hasNext())
477                {
478                        dpData.add(it.next().getData());
479                }
480                getData().setSearchModifications(dpData);
481        }
482}