View Javadoc

1   /********************************************************************************
2    * $Id: Job.java 137 2008-11-07 17:06:50Z sjardine $
3    * 
4    * Copyright 2001-2003 Innovation Software Group, LLC - http://www.innovationsw.com
5    * Copyright 2001-2003 Joe Phillips <jaiger@innovationsw.com>
6    * Copyright 2008 Steven Jardine, MJN Services, Inc. <steve@mjnservices.com>
7    * 
8    * All rights reserved. This program and the accompanying materials are made
9    * available under the terms of the GNU Lesser Public License v2.1 which 
10   * accompanies this distribution, and is available at
11   * 	http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
12   *
13   * For more information on the HylaFAX Fax Server please see
14   * 	HylaFAX  - http://www.hylafax.org or 
15   * 	Hylafax+ - http://hylafax.sourceforge.net
16   * 
17   * Contributors:
18   * 	Joe Phillips - Initial API and implementation
19   * 	Steven Jardine - Code formatting, rework of license header, javadoc 
20   ******************************************************************************/
21  package gnu.hylafax.job;
22  
23  import gnu.hylafax.Client;
24  import gnu.hylafax.ClientProtocol;
25  import gnu.hylafax.job.TimeParser.ParseException;
26  import gnu.inet.ftp.ServerResponseException;
27  
28  import java.awt.Dimension;
29  import java.io.IOException;
30  import java.text.SimpleDateFormat;
31  import java.util.Date;
32  import java.util.Locale;
33  import java.util.TimeZone;
34  
35  /***
36   * This class is a light, unsynchronized implementation of gnu.hylafax.Job.
37   * 
38   * @version $Revision: 137 $
39   * @author Joe Phillips <jaiger@innovationsw.com>
40   * @author Steven Jardine <steve@mjnservices.com>
41   */
42  public class Job implements gnu.hylafax.Job {
43  
44      public static String CHOP_DEFAULT = "default";
45  
46      public static String NOTIFY_ALL = ClientProtocol.NOTIFY_ALL;
47  
48      public static String NOTIFY_DONE = ClientProtocol.NOTIFY_DONE;
49  
50      public static String NOTIFY_NONE = ClientProtocol.NOTIFY_NONE;
51  
52      public static String NOTIFY_REQUEUE = ClientProtocol.NOTIFY_REQUEUE;
53  
54      public static int PRIORITY_BULK = 207;
55  
56      public static int PRIORITY_HIGH = 63;
57  
58      public static int PRIORITY_NORMAL = 127;
59  
60      public static int RESOLUTION_LOW = 98;
61  
62      public static int RESOLUTION_MEDIUM = 196;
63  
64      protected Client client;
65  
66      private long Id;
67  
68      /***
69       * Creates a new job. All job parameters are inherited from the default job.
70       * 
71       * @param c
72       *            the client to use to create the new job.
73       * @throws ServerResponseException
74       * @throws IOException
75       */
76      public Job(Client c) throws ServerResponseException, IOException {
77  	this(c, true);
78      }
79  
80      /***
81       * Creates a new job.
82       * 
83       * @param c
84       *            the client to use to create the new job.
85       * @param inheritDefault
86       *            inherit job parameters from the default job on the hylafax
87       *            server.
88       * @throws ServerResponseException
89       * @throws IOException
90       */
91      public Job(Client c, boolean inheritDefault)
92  	    throws ServerResponseException, IOException {
93  	synchronized (c) {
94  	    client = c;
95  	    client.jnew(inheritDefault);
96  	    Id = client.job();
97  	}
98      }
99  
100     /***
101      * Gets a job from the hylafax server.
102      * 
103      * @param c
104      *            the client to use to create the new job.
105      * @param id
106      *            the job id to retrieve from the hylafax server.
107      * @throws ServerResponseException
108      * @throws IOException
109      */
110     public Job(Client c, long id) throws ServerResponseException, IOException {
111 	synchronized (c) {
112 	    client = c;
113 	    client.job(id);
114 	    Id = client.job();
115 	}
116     }
117 
118     public void addDocument(String value) throws ServerResponseException,
119 	    IOException {
120 	setProperty("DOCUMENT", value);
121     }
122 
123     public int getChopThreshold() throws ServerResponseException, IOException {
124 	return Integer.parseInt(getProperty("CHOPTHRESHOLD"));
125     }
126 
127     public String getDialstring() throws ServerResponseException, IOException {
128 	return getProperty("DIALSTRING");
129     }
130 
131     public String getDocumentName() throws ServerResponseException, IOException {
132 	return getProperty("DOCUMENT");
133     }
134 
135     /*
136      * (non-Javadoc)
137      * 
138      * @see gnu.hylafax.Job#getFromUser()
139      */
140     public String getFromUser() throws ServerResponseException, IOException {
141 	return getProperty("FROMUSER");
142     }
143 
144     /***
145      * get the job-id of this Job instance.
146      * 
147      * @return job id
148      */
149     public long getId() {
150 	return Id;
151     }
152 
153     /***
154      * Get JobInfo attribute. JobInfo is an identifying string associated with
155      * the job.
156      */
157     public String getJobInfo() throws ServerResponseException, IOException {
158 	return getProperty("JOBINFO");
159     }
160 
161     public String getKilltime() throws ServerResponseException, IOException {
162 	return getProperty("LASTTIME");
163     }
164 
165     public int getMaximumDials() throws ServerResponseException, IOException {
166 	return Integer.parseInt(getProperty("MAXDIALS"));
167     }
168 
169     public int getMaximumTries() throws ServerResponseException, IOException {
170 	return Integer.parseInt(getProperty("MAXTRIES"));
171     }
172 
173     public String getNotifyAddress() throws ServerResponseException,
174 	    IOException {
175 	return getProperty("NOTIFYADDR");
176     }
177 
178     public String getNotifyType() throws ServerResponseException, IOException {
179 	return getProperty("NOTIFY");
180     }
181 
182     public String getPageChop() throws ServerResponseException, IOException {
183 	return getProperty("PAGECHOP");
184     }
185 
186     public Dimension getPageDimension() throws ServerResponseException,
187 	    IOException {
188 	return new Dimension(getPageWidth(), getPageLength());
189     }
190 
191     public int getPageLength() throws ServerResponseException, IOException {
192 	return Integer.parseInt(getProperty("PAGELENGTH"));
193     }
194 
195     public int getPageWidth() throws ServerResponseException, IOException {
196 	return Integer.parseInt(getProperty("PAGEWIDTH"));
197     }
198 
199     public int getPriority() throws ServerResponseException, IOException {
200 	return Integer.parseInt(getProperty("SCHEDPRI"));
201     }
202 
203     /***
204      * Get the value for an arbitrary property for this job. Developers using
205      * this method should be familiar with the HylaFAX client protocol in order
206      * to provide the correct key values and how to interpret the values
207      * returned.
208      * 
209      * @exception ServerResponseException
210      *                the server responded with an error. This is likely due to
211      *                a protocol error.
212      * @exception IOException
213      *                an i/o error occured
214      * @return a String value for the given property key
215      */
216     public String getProperty(String key) throws ServerResponseException,
217 	    IOException {
218 	String tmp = client.jparm(key);
219 	return tmp;
220     }
221 
222     public String getRetrytime() throws ServerResponseException, IOException {
223 	return getProperty("RETRYTIME");
224     }
225 
226     /***
227      * Get TagLine format attribute. The TagLine
228      */
229     public String getTagline() throws ServerResponseException, IOException {
230 	return getProperty("TAGLINE");
231     }
232 
233     /***
234      * Get the UseTagLine attribute. The TagLine
235      */
236     public boolean getUseTagline() throws ServerResponseException, IOException {
237 	return ("YES".equalsIgnoreCase(getProperty("USETAGLINE")) ? true
238 		: false);
239     }
240 
241     public int getVerticalResolution() throws ServerResponseException,
242 	    IOException {
243 	return Integer.parseInt(getProperty("VRES"));
244     }
245 
246     public void setChopThreshold(int value) throws ServerResponseException,
247 	    IOException {
248 	setProperty("CHOPTHRESHOLD", value);
249     }
250 
251     public void setDialstring(String value) throws ServerResponseException,
252 	    IOException {
253 	setProperty("DIALSTRING", value);
254     }
255 
256     public void setFromUser(String value) throws ServerResponseException,
257 	    IOException {
258 	setProperty("FROMUSER", value);
259     }
260 
261     /***
262      * Set the JobInfo attribute. This is an identifying string associated with
263      * each job.
264      */
265     public void setJobInfo(String value) throws ServerResponseException,
266 	    IOException {
267 	setProperty("JOBINFO", value);
268     }
269 
270     public void setKilltime(String value) throws ServerResponseException,
271 	    IOException {
272 	String time;
273 	try {
274 	    time = new TimeParser().getKillTime(value);
275 	} catch (ParseException e) {
276 	    time = value;
277 	}
278 	setProperty("LASTTIME", time);
279     }
280 
281     public void setMaximumDials(int value) throws ServerResponseException,
282 	    IOException {
283 	setProperty("MAXDIALS", value);
284     }
285 
286     public void setMaximumTries(int value) throws ServerResponseException,
287 	    IOException {
288 	setProperty("MAXTRIES", value);
289     }
290 
291     public void setNotifyAddress(String value) throws ServerResponseException,
292 	    IOException {
293 	setProperty("NOTIFYADDR", value);
294     }
295 
296     /***
297      * set the notification type. For possible values, see the NOTIFY_* members
298      * of this class.
299      * 
300      * @param value
301      *            the new notification type
302      * @exception ServerResponseException
303      *                the server responded with an error. This is likely a
304      *                protocol violation.
305      * @exception IOException
306      *                an IO error occurred while communicating with the server
307      */
308     public void setNotifyType(String value) throws ServerResponseException,
309 	    IOException {
310 	setProperty("NOTIFY", value);
311     }
312 
313     public void setPageChop(String value) throws ServerResponseException,
314 	    IOException {
315 	setProperty("PAGECHOP", value);
316     }
317 
318     public void setPageDimension(Dimension value)
319 	    throws ServerResponseException, IOException {
320 	setPageWidth((int) value.getWidth());
321 	setPageLength((int) value.getHeight());
322     }
323 
324     public void setPageLength(int length) throws ServerResponseException,
325 	    IOException {
326 	setProperty("PAGELENGTH", length);
327     }
328 
329     public void setPageWidth(int width) throws ServerResponseException,
330 	    IOException {
331 	setProperty("PAGEWIDTH", width);
332     }
333 
334     public void setPriority(int value) throws ServerResponseException,
335 	    IOException {
336 	setProperty("SCHEDPRI", value);
337     }
338 
339     /***
340      * Set any arbitrary property on this job to an integer value. In order to
341      * use this method, developers should be familiar with the HylaFAX client
342      * protocol.
343      * 
344      * @exception ServerResponseException
345      *                the server responded with an error code. This is likely a
346      *                protocol violation.
347      * @exception IOException
348      *                an i/o error occured
349      */
350     public void setProperty(String property, int value)
351 	    throws ServerResponseException, IOException {
352 	setProperty(property, (new Integer(value)).toString());
353     }
354 
355     /***
356      * Set any arbitrary property on this job. In order to use this method,
357      * developers should be familiar with the HylaFAX client protocol.
358      * 
359      * @exception ServerResponseException
360      *                the server responded with an error code. This is likely a
361      *                protocol violation.
362      * @exception IOException
363      *                an i/o error occured
364      */
365     public void setProperty(String parameter, String value)
366 	    throws ServerResponseException, IOException {
367 	client.jparm(parameter, value);
368     }
369 
370     public void setRetrytime(String value) throws ServerResponseException,
371 	    IOException {
372 	setProperty("RETRYTIME", value);
373     }
374 
375     /*
376      * (non-Javadoc)
377      * 
378      * @see gnu.hylafax.Job#setSendTime(java.util.Date)
379      */
380     public void setSendTime(Date sendTime) throws ServerResponseException,
381 	    IOException {
382 	// Format the date using the GMT timezone and Hylafax date format for
383 	// SENDTIME.
384 	SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
385 	df.setTimeZone(TimeZone.getTimeZone("GMT"));
386 	setProperty("SENDTIME", df.format(sendTime));
387     }
388 
389     /*
390      * (non-Javadoc)
391      * 
392      * @see gnu.hylafax.Job#setSendTime(java.lang.String)
393      */
394     public void setSendTime(String sendTime) throws ServerResponseException,
395 	    IOException {
396 	setSendTime(sendTime, Locale.getDefault(), TimeZone.getDefault());
397     }
398 
399     public void setSendTime(String sendTime, Locale locale, TimeZone timeZone)
400 	    throws ServerResponseException, IOException {
401 	String time;
402 	try {
403 	    time = new TimeParser(locale, timeZone).getSendTime(sendTime);
404 	} catch (ParseException e) {
405 	    time = sendTime;
406 	}
407 	setProperty("SENDTIME", time);
408     }
409 
410     /***
411      * Set the TagLine format attribute. This property specifies the format of
412      * the tagline rendered at the top of each page of the transmitted FAX.
413      * Tagline format strings are documented in config(5F). If you use this, you
414      * will probably want to use setUseTagline()
415      */
416     public void setTagline(String value) throws ServerResponseException,
417 	    IOException {
418 	setProperty("TAGLINE", value);
419     }
420 
421     public void setUseTagline(boolean value) throws ServerResponseException,
422 	    IOException {
423 	setProperty("USETAGLINE", (value ? "YES" : "NO"));
424     }
425 
426     public void setVerticalResolution(int value)
427 	    throws ServerResponseException, IOException {
428 	setProperty("VRES", value);
429     }
430 
431 }