001/*
002 $Id: Hardware.java 3207 2009-04-09 06:48:11Z gregory $
003
004 Copyright (C) 2006 Gregory Vincic, Olle Mansson
005 Copyright (C) 2007 Gregory Vincic
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.HardwareData;
028
029/**
030 * This class is used to represent individual hardware items and information
031 * about them. In the current version of Proteios the only type of hardware we
032 * keep information about is scanners.
033 * 
034 * @author enell
035 * @version 2.0
036 * @see HardwareType HardwareType
037 * @base.modified $Date: 2009-04-09 08:48:11 +0200 (Thu, 09 Apr 2009) $
038 */
039public class Hardware
040                extends CommonItem<HardwareData>
041{
042        /**
043         * The type of item represented by this class.
044         * 
045         * @see Item#HARDWARE
046         * @see #getType()
047         */
048        public static final Item TYPE = Item.HARDWARE;
049        /**
050         * The maximum length of the version variable that can be stored in the
051         * database. Check the length against this value before calling the
052         * {@link #setVersionString(String)} method to avoid exceptions.
053         */
054        public static final int MAX_VERSIONSTRING_LENGTH = HardwareData.MAX_VERSIONSTRING_LENGTH;
055
056
057        /**
058         * Get a query configured to retrieve hardware.
059         * 
060         * @return An {@link ItemQuery} object
061         */
062        public static ItemQuery<Hardware> getQuery()
063                        throws BaseException
064        {
065                return new ItemQuery<Hardware>(Hardware.class);
066        }
067
068
069        /**
070         * Creates a new hardware item.
071         * 
072         * @param data the data
073         */
074        Hardware(HardwareData data)
075        {
076                super(data);
077        }
078
079
080        /*
081         * From the Identifiable interface
082         * -------------------------------------------
083         */
084        public Item getType()
085        {
086                return TYPE;
087        }
088
089
090        // -------------------------------------------
091        /*
092         * From the BasicItem class -------------------------------------------
093         */
094        /**
095         * Check if:
096         * <ul>
097         * <li>a Scan is linked to this hardware
098         * </ul>
099         * 
100         * @return TRUE if this hardware is used.
101         * @throws BaseException if there is any error.
102         */
103        @Override
104        public boolean isUsed()
105                        throws BaseException
106        {
107                boolean used = false;
108                return used;
109        }
110
111
112        // -------------------------------------------
113        /**
114         * Get the associated {@link HardwareType} item. All <code>Hardware</code>
115         * items must have a type.
116         * 
117         * @return The <code>HardwareType</code> item
118         * @throws PermissionDeniedException If the logged in user doesn't have
119         *         {@link Permission#READ } permission to the item
120         * @throws BaseException If there is any error
121         */
122        public HardwareType getHardwareType()
123                        throws PermissionDeniedException, BaseException
124        {
125                return getDbControl().getItem(HardwareType.class,
126                        getData().getHardwareType());
127        }
128
129
130        /**
131         * Set the {@link HardwareType} of this <code>Hardware</code> item. This
132         * parameter mustn't be <code>null</code>.
133         * 
134         * @param hardwareType The new <code>HardwareType</code>
135         * @throws PermissionDeniedException If the logged in user doesn't have
136         *         {@link Permission#WRITE} permission for the hardware or
137         *         {@link Permission#USE} permission for the hardware type
138         * @throws InvalidUseOfNullException If the hardware type is null
139         */
140        public void setHardwareType(HardwareType hardwareType)
141                        throws PermissionDeniedException, InvalidUseOfNullException
142        {
143                checkPermission(Permission.WRITE);
144                if (hardwareType == null)
145                        throw new InvalidUseOfNullException("hardwareType");
146                hardwareType.checkPermission(Permission.USE);
147                getData().setHardwareType(hardwareType.getData());
148        }
149
150
151        /**
152         * Get the versionstring of this <code>Hardware</code> item.
153         * 
154         * @return A string with the version of this item, or null if not known
155         */
156        public String getVersionString()
157        {
158                return getData().getVersionString();
159        }
160
161
162        /**
163         * Set the versionstring for this <code>Hardware</code> item. The value
164         * must not be longer than the value specified by the
165         * {@link #MAX_VERSIONSTRING_LENGTH} constant.
166         * 
167         * @param versionString The new version for this item, or null if not known
168         * @throws StringTooLongException If versionString is longer then
169         *         {@link #MAX_VERSIONSTRING_LENGTH}
170         * @throws PermissionDeniedException If the logged in user don't have write
171         *         permission on this item
172         */
173        public void setVersionString(String versionString)
174                        throws StringTooLongException, PermissionDeniedException
175        {
176                checkPermission(Permission.WRITE);
177                getData().setVersionString(
178                        StringUtil.setNullableString(versionString, "versionString",
179                                MAX_VERSIONSTRING_LENGTH));
180        }
181}