001/*
002 $Id: SeparationMethod.java 3207 2009-04-09 06:48:11Z gregory $
003
004 Copyright (C) 2007 Gregory Vincic, 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.BioMaterialEventData;
027import org.proteios.core.data.SeparationMethodData;
028
029import java.util.HashSet;
030import java.util.Iterator;
031import java.util.List;
032import java.util.Set;
033
034/**
035 * This class is the root class for separation methods. The class describes the
036 * object that is the result of the method. It is the base class for for the two
037 * types of separationMethods: {@link GelElectrophoresis} and {@link IPG}.
038 * 
039 * @author Olle
040 * @version 2.0
041 * @proteios.modified $Date: 2006-09-19 12:33:12Z $
042 */
043public class SeparationMethod<D extends SeparationMethodData>
044                extends SharedItem<D>
045{
046        SeparationMethod(D separationMethodData)
047        {
048                super(separationMethodData);
049        }
050
051        /**
052         * The type of item represented by this class.
053         * 
054         * @see Item#PROTEIOS_SEPARATIONMETHOD
055         * @see #getType()
056         */
057        public static final Item TYPE = Item.PROTEIOS_SEPARATIONMETHOD;
058
059
060        @SuppressWarnings("unchecked")
061        public static List<Integer> getIdsForSeparationMethodsWithSeaprationEvent(
062                        DbControl dc)
063        {
064                org.hibernate.Query query = HibernateUtil.getPredefinedQuery(dc
065                        .getHibernateSession(),
066                        "GET_IDS_FOR_SEPARATION_METHODS_WITH_SEPARATION_EVENTS");
067                return query.list();
068        }
069
070        /**
071         * The maximum length of the external id that can be stored in the database.
072         * 
073         * @see #setExternalId(String)
074         */
075        public static final int MAX_EXTERNAL_ID_LENGTH = SeparationMethodData.MAX_EXTERNAL_ID_LENGTH;
076
077
078        /**
079         * Get a <code>SeparationMethod</code> item when you know the external ID.
080         * 
081         * @param dc The <code>DbControl</code> which will be used for permission
082         *        checking and database access.
083         * @param externalId The external id of the item to load
084         * @return The <code>SeparationMethod</code> item
085         * @throws ItemNotFoundException If an item with the specified ID is not
086         *         found
087         * @throws PermissionDeniedException If the logged in user doesn't have
088         *         {@link Permission#READ READ} permission for the item
089         * @throws BaseException If there is another error
090         */
091        public static SeparationMethod<?> getByExternalId(DbControl dc,
092                        String externalId)
093                        throws ItemNotFoundException, PermissionDeniedException,
094                        BaseException
095        {
096                org.hibernate.Query query = HibernateUtil.getPredefinedQuery(dc
097                        .getHibernateSession(), "GET_SEPARATIONMETHOD_FOR_EXTERNAL_ID");
098                /*
099                 * SELECT sm FROM SeparationMethodData sm WHERE sm.externalId =
100                 * :externalId
101                 */
102                query.setString("externalId", externalId);
103                SeparationMethod<?> sm = dc.getItem(SeparationMethod.class, HibernateUtil
104                        .loadData(SeparationMethodData.class, query));
105                if (sm == null)
106                        throw new ItemNotFoundException(
107                                "SeparationMethod[externalId=" + externalId + "]");
108                return sm;
109        }
110
111
112        /*
113         * From the Identifiable interface
114         * -------------------------------------------
115         */
116        public Item getType()
117        {
118                return TYPE;
119        }
120
121
122        // -------------------------------------------
123        /*
124         * From the BasicItem class -------------------------------------------
125         */
126        /**
127         * Always return FALSE. A separation method can be referenced from help,
128         * sessions and settings but those references are automatically deleted if
129         * the separation method is deleted and aren't included in this check.
130         */
131        @Override
132        public boolean isUsed()
133                        throws BaseException
134        {
135                return false;
136        }
137
138
139        // -------------------------------------------
140        /**
141         * Get the external id of the separation method. This value can be used to
142         * link with information in external databases. It is not used by the
143         * Proteios core and it doesn't have to be unique.
144         * 
145         * @return The external id for the separation method.
146         */
147        public String getExternalId()
148        {
149                return getData().getExternalId();
150        }
151
152
153        /**
154         * Set the external id for this <code>SeparationMethod</code> item. The
155         * value cannot be null and must not be longer than the value specified by
156         * the {@link #MAX_EXTERNAL_ID_LENGTH} constant.
157         * 
158         * @param externalId The new value for the external id
159         * @throws PermissionDeniedException If the logged in user doesn't have
160         *         write permission
161         * @throws InvalidDataException If the new value is null or longer than
162         *         {@link #MAX_EXTERNAL_ID_LENGTH}
163         */
164        public void setExternalId(String externalId)
165                        throws PermissionDeniedException, InvalidDataException
166        {
167                checkPermission(Permission.WRITE);
168                getData().setExternalId(
169                        StringUtil.setNotNullString(externalId, "externalId",
170                                MAX_EXTERNAL_ID_LENGTH));
171        }
172
173
174        /**
175         * @return Returns the separationEvents.
176         */
177        @SuppressWarnings("unchecked")
178        public Set<SeparationEvent> getSeparationEvents()
179                        throws ItemNotFoundException, BaseException
180        {
181                Set<SeparationEvent> separationEvents = new HashSet<SeparationEvent>();
182                try
183                {
184                        Iterator<BioMaterialEventData> it = getData()
185                                .getBioMaterialEvents().iterator();
186                        while (it.hasNext())
187                        {
188                                BioMaterialEvent bme = getDbControl().getItem(
189                                        BioMaterialEvent.class, it.next());
190                                if (bme instanceof SeparationEvent)
191                                        separationEvents.add((SeparationEvent) bme);
192                        }
193                }
194                catch (org.hibernate.ObjectNotFoundException e)
195                {
196                        throw new ItemNotFoundException(HibernateUtil.getShortEntityName(e
197                                .getEntityName()) + "[id=" + e.getIdentifier() + "]");
198                }
199                return separationEvents;
200        }
201
202
203
204}