KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MAssignmentSlot


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.util.*;
17 import java.sql.*;
18 import java.awt.*;
19
20 import org.compiere.util.*;
21
22 /**
23  * Assignment Slot.
24  * Display Information about Assignment Slot
25  *
26  * @author Jorg Janke
27  * @version $Id: MAssignmentSlot.java,v 1.11 2002/08/12 01:55:12 danb Exp $
28  */

29 public class MAssignmentSlot implements Comparator
30 {
31     /**
32      * Comparator Constructor
33      */

34     public MAssignmentSlot ()
35     {
36         this (null, null, null, null, STATUS_TimeSlot);
37     } // MAssignmentSlot
38

39     /**
40      * Timeslot Constructor
41      * @param startTime start time
42      * @param endTime end time
43      */

44     public MAssignmentSlot (Timestamp startTime, Timestamp endTime)
45     {
46         this (startTime, endTime, null, null, STATUS_TimeSlot);
47         setDisplay(DISPLAY_TIME_FROM);
48     } // MAssignmentSlot
49

50     /**
51      * Timeslot Constructor
52      * @param startTime start time
53      * @param endTime end time
54      */

55     public MAssignmentSlot (long startTime, long endTime)
56     {
57         this (new Timestamp(startTime), new Timestamp(endTime), null, null, STATUS_TimeSlot);
58         setDisplay(DISPLAY_TIME_FROM);
59     } // MAssignmentSlot
60

61     /**
62      * Non Assignment Constructor
63      * @param startTime start time
64      * @param endTime end time
65      * @param name name
66      * @param description description
67      * @param status status
68      */

69     public MAssignmentSlot (Timestamp startTime, Timestamp endTime,
70         String JavaDoc name, String JavaDoc description, int status)
71     {
72         setStartTime(startTime);
73         setEndTime(endTime);
74         setName(name);
75         setDescription(description);
76         setStatus(status);
77         //
78
// Log.trace(Log.l6_Database, toString());
79
} // MAssignmentSlot
80

81     /**
82      * Assignment Constructor
83      * @param assignment MAssignment
84      */

85     public MAssignmentSlot (MAssignment assignment)
86     {
87         setStatus(assignment.isConfirmed() ? STATUS_Confirmed : STATUS_NotConfirmed);
88         setMAssignment(assignment);
89     // Log.trace(Log.l6_Database, toString());
90
} // MAssignmentSlot
91

92
93     /** Not Available Code */
94     public static final int STATUS_NotAvailable = 0;
95     /** Not Available Code */
96     public static final int STATUS_UnAvailable = 11;
97     /** Not Available Code */
98     public static final int STATUS_NonBusinessDay = 12;
99     /** Not Available Code */
100     public static final int STATUS_NotInSlotDay = 21;
101     /** Not Available Code */
102     public static final int STATUS_NotInSlotTime = 22;
103     /** Assignment Code */
104     public static final int STATUS_NotConfirmed = 101;
105     /** Assignment Code */
106     public static final int STATUS_Confirmed = 102;
107
108     /** Assignment Code */
109     public static final int STATUS_TimeSlot = 100000;
110
111     /** Start Time */
112     private Timestamp m_startTime;
113     /** End Time */
114     private Timestamp m_endTime;
115     /** Name */
116     private String JavaDoc m_name;
117     /** Description */
118     private String JavaDoc m_description;
119     /** Status */
120     private int m_status = STATUS_NotAvailable;
121     /** Y position */
122     private int m_yStart = 0;
123     private int m_yEnd = 0;
124     private int m_xPos = 0;
125     private int m_xMax = 1;
126
127     /** The assignment */
128     private MAssignment m_mAssignment;
129
130     /** Language used for formatting */
131     private Language m_language = Language.getLanguage();
132
133     /** toString displays everything */
134     public static final int DISPLAY_ALL = 0;
135
136     /** toString displays formatted time from */
137     public static final int DISPLAY_TIME_FROM = 1;
138     /** toString displays formatted time from-to */
139     public static final int DISPLAY_TIME_FROM_TO = 1;
140     /** toString displays formatted day time from-to */
141     public static final int DISPLAY_DATETIME_FROM_TO = 1;
142     /** toString displays name */
143     public static final int DISPLAY_NAME = 1;
144     /** toString displays name and optional description */
145     public static final int DISPLAY_NAME_DESCRIPTION = 1;
146     /** toString displays formatted all info */
147     public static final int DISPLAY_FULL = 1;
148
149     /** DisplayMode */
150     private int m_displayMode = DISPLAY_FULL;
151
152     /*************************************************************************/
153
154     /**
155      * Set Status
156      * @param status STATUS_..
157      */

158     public void setStatus (int status)
159     {
160         m_status = status;
161     } // setStatus
162

163     /**
164      * Get Status
165      * @return STATUS_..
166      */

167     public int getStatus()
168     {
169         return m_status;
170     } // getStatus
171

172     /**
173      * Is the Slot an Assignment?
174      * @return true if slot is an assignment
175      */

176     public boolean isAssignment()
177     {
178         return (m_status == STATUS_NotConfirmed || m_status == STATUS_Confirmed);
179     } // isAssignment
180

181     /**
182      * Get Color for Status
183      * @param background true if background - or foreground
184      * @return Color
185      */

186     public Color getColor (boolean background)
187     {
188         // Not found, Inactive, not available
189
if (m_status == STATUS_NotAvailable)
190             return background ? Color.gray : Color.magenta;
191
192         // Holiday
193
else if (m_status == STATUS_UnAvailable)
194             return background ? Color.gray : Color.pink;
195
196         // Vacation
197
else if (m_status == STATUS_NonBusinessDay)
198             return background ? Color.lightGray : Color.red;
199
200         // Out of normal hours
201
else if (m_status == STATUS_NotInSlotDay || m_status == STATUS_NotInSlotTime)
202             return background ? Color.lightGray : Color.black;
203
204         // Assigned
205
else if (m_status == STATUS_NotConfirmed)
206             return background ? Color.blue : Color.white;
207
208         // Confirmed
209
else if (m_status == STATUS_Confirmed)
210             return background ? Color.blue : Color.black;
211
212         // Unknown
213
return background ? Color.black : Color.white;
214     } // getColor
215

216     /*************************************************************************/
217
218     /**
219      * Get Start time
220      * @return start time
221      */

222     public Timestamp getStartTime()
223     {
224         return m_startTime;
225     }
226
227     /**
228      * Set Start time
229      * @param startTime start time, if null use current time
230      */

231     public void setStartTime (Timestamp startTime)
232     {
233         if (startTime == null)
234             m_startTime = new Timestamp(System.currentTimeMillis());
235         else
236             m_startTime = startTime;
237     } // setStartTime
238

239     /**
240      * Get End time
241      * @return end time
242      */

243     public Timestamp getEndTime()
244     {
245         return m_endTime;
246     }
247
248     /**
249      * Set End time
250      * @param endTime end time, if null use start time
251      */

252     public void setEndTime (Timestamp endTime)
253     {
254         if (endTime == null)
255             m_endTime = m_startTime;
256         else
257             m_endTime = endTime;
258     }
259
260     /*************************************************************************/
261
262     /**
263      * Set Assignment
264      * @param assignment MAssignment
265      */

266     public void setMAssignment (MAssignment assignment)
267     {
268         if (assignment == null)
269             return;
270         if (!isAssignment())
271             throw new IllegalArgumentException JavaDoc("Assignment Slot not an Assignment");
272         //
273
m_mAssignment = assignment;
274         setStartTime(m_mAssignment.getAssignDateFrom());
275         setEndTime(m_mAssignment.getAssignDateTo());
276         setName(m_mAssignment.getName());
277         setDescription(m_mAssignment.getDescription());
278         setStatus(m_mAssignment.isConfirmed() ? STATUS_Confirmed : STATUS_NotConfirmed);
279     } // setMAssignment
280

281     /**
282      * Get Assugnment
283      * @return assignment
284      */

285     public MAssignment getMAssignment()
286     {
287         return m_mAssignment;
288     } // getAssignment
289

290     /**
291      * Set Name
292      * @param name name
293      */

294     public void setName (String JavaDoc name)
295     {
296         if (name == null)
297             m_name = "";
298         else
299             m_name = name;
300     } // setName
301

302     /**
303      * Get Name
304      * @return name
305      */

306     public String JavaDoc getName()
307     {
308         return m_name;
309     } // getName
310

311     /**
312      * Set Description
313      * @param description description
314      */

315     public void setDescription (String JavaDoc description)
316     {
317         if (description == null)
318             m_description = "";
319         else
320             m_description = description;
321     } // setDescription
322

323     /**
324      * Get Description
325      * @return description
326      */

327     public String JavaDoc getDescription()
328     {
329         return m_description;
330     } // getDescription
331

332     /*************************************************************************/
333
334     /**
335      * Set Y position
336      * @param yStart zero based Y start index
337      * @param yEnd zero based Y end index
338      */

339     public void setY (int yStart, int yEnd)
340     {
341         m_yStart = yStart;
342         m_yEnd = yEnd;
343     } // setY
344

345     /**
346      * Get Y start position
347      * @return zero based Y start index
348      */

349     public int getYStart ()
350     {
351         return m_yStart;
352     } // getYStart
353

354     /**
355      * Get Y end position
356      * @return zero based Y end index
357      */

358     public int getYEnd ()
359     {
360         return m_yEnd;
361     } // setYEnd
362

363     /**
364      * Set X position
365      * @param xPos zero based X position index
366      * @param xMax number of parallel columns
367      */

368     public void setX (int xPos, int xMax)
369     {
370         m_xPos = xPos;
371         if (xMax > m_xMax)
372             m_xMax = xMax;
373     } // setX
374

375     /**
376      * Get X position
377      * @return zero based X position index
378      */

379     public int getXPos()
380     {
381         return m_xPos;
382     } // setXPos
383

384     /**
385      * Get X columns
386      * @return number of parallel columns
387      */

388     public int getXMax()
389     {
390         return m_xMax;
391     } // setXMax
392

393     /*************************************************************************/
394
395     /**
396      * Set Language
397      * @param language language
398      */

399     public void setLanguage (Language language)
400     {
401         m_language = language;
402     } // setLanguage
403

404     /**
405      * Set Display Mode of toString()
406      * @param displayMode DISPLAY_
407      */

408     public void setDisplay (int displayMode)
409     {
410         m_displayMode = displayMode;
411     } // setDisplay
412

413
414     /**
415      * String representation
416      * @return info
417      */

418     public String JavaDoc toString()
419     {
420         if (m_displayMode == DISPLAY_TIME_FROM)
421             return getInfoTimeFrom();
422         else if (m_displayMode == DISPLAY_TIME_FROM_TO)
423             return getInfoTimeFromTo();
424         else if (m_displayMode == DISPLAY_DATETIME_FROM_TO)
425             return getInfoDateTimeFromTo();
426         else if (m_displayMode == DISPLAY_NAME)
427             return m_name;
428         else if (m_displayMode == DISPLAY_NAME_DESCRIPTION)
429             return getInfoNameDescription();
430         else if (m_displayMode == DISPLAY_FULL)
431             return getInfo();
432
433         // DISPLAY_ALL
434
StringBuffer JavaDoc sb = new StringBuffer JavaDoc("MAssignmentSlot[");
435         sb.append(m_startTime).append("-").append(m_endTime)
436             .append("-Status=").append(m_status).append(",Name=")
437             .append(m_name).append(",").append(m_description).append("]");
438         return sb.toString();
439     } // toString
440

441     /**
442      * Get Info with Time From
443      * @return info 00:00
444      */

445     public String JavaDoc getInfoTimeFrom()
446     {
447         return m_language.getTimeFormat().format(m_startTime);
448     } // getInfoTimeFrom
449

450     /**
451      * Get Info with Time From-To
452      * @return info 00:00 - 01:00
453      */

454     public String JavaDoc getInfoTimeFromTo()
455     {
456         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
457         sb.append(m_language.getTimeFormat().format(m_startTime))
458             .append(" - ")
459             .append(m_language.getTimeFormat().format(m_endTime));
460         return sb.toString();
461     } // getInfoTimeFromTo
462

463     /**
464      * Get Info with Date & Time From-To
465      * @return info 12/12/01 00:00 - 01:00 or 12/12/01 00:00 - 12/13/01 01:00
466      */

467     public String JavaDoc getInfoDateTimeFromTo()
468     {
469         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
470         sb.append(m_language.getDateTimeFormat().format(m_startTime))
471             .append(" - ");
472         if (TimeUtil.isSameDay(m_startTime, m_endTime))
473             sb.append(m_language.getTimeFormat().format(m_endTime));
474         else
475             m_language.getDateTimeFormat().format(m_endTime);
476         return sb.toString();
477     }
478
479     /**
480      * Get Info with Name and optional Description
481      * @return Name (Description)
482      */

483     public String JavaDoc getInfoNameDescription()
484     {
485         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(m_name);
486         if (m_description.length() > 0)
487             sb.append(" (").append(m_description).append(")");
488         return sb.toString();
489     } // getInfoNameDescription
490

491     /**
492      * Get Info with Date, Time From-To Name Description
493      * @return 12/12/01 00:00 - 01:00: Name (Description)
494      */

495     public String JavaDoc getInfo()
496     {
497         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(getInfoDateTimeFromTo());
498         sb.append(": ").append(m_name);
499         if (m_description.length() > 0)
500             sb.append(" (").append(m_description).append(")");
501         return sb.toString();
502     } // getInfo
503

504     /*************************************************************************/
505
506     /**
507      * Returns true if time is between start and end Time.
508      * Date part is ignored.
509      * <pre>
510      * Example:
511      * - Slots: 0:00-9:00 - 9:00-10:00 - 10:00-11:00 - ...
512      * - inSlot (9:00, false) -> 1 // start time
513      * - inSlot (10:00, true) -> 1 // end time
514      * </pre>
515      * @param time time of the day
516      * @param endTime if true, the end time is included
517      * @return true if within slot
518      */

519     public boolean inSlot (Timestamp time, boolean endTime)
520     {
521         // Compare --
522
GregorianCalendar cal = new GregorianCalendar();
523         cal.setTime(time);
524         cal.set(Calendar.YEAR, 1970);
525         cal.set(Calendar.DAY_OF_YEAR, 1);
526         // handle -00:00 (end time)
527
if (endTime && cal.get(Calendar.HOUR_OF_DAY) == 0 && cal.get(Calendar.MINUTE) == 0)
528         {
529             cal.set(Calendar.HOUR_OF_DAY, 23);
530             cal.set(Calendar.MINUTE, 59);
531         }
532         Time compare = new Time (cal.getTimeInMillis());
533         // Start Time --
534
cal.setTime(m_startTime);
535         cal.set(Calendar.YEAR, 1970);
536         cal.set(Calendar.DAY_OF_YEAR, 1);
537         Time start = new Time (cal.getTimeInMillis());
538         // End time --
539
cal.setTime(m_endTime);
540         cal.set(Calendar.YEAR, 1970);
541         cal.set(Calendar.DAY_OF_YEAR, 1);
542         if (cal.get(Calendar.HOUR_OF_DAY) == 0 && cal.get(Calendar.MINUTE) == 0)
543         {
544             cal.set(Calendar.HOUR_OF_DAY, 23);
545             cal.set(Calendar.MINUTE, 59);
546         }
547         Time end = new Time (cal.getTimeInMillis());
548
549         // before start x |---|
550
if (compare.before(start))
551         {
552         // System.out.println("InSlot-false Compare=" + compare + " before start " + start);
553
return false;
554         }
555         // after end |---| x
556
if (compare.after(end))
557         {
558         // System.out.println("InSlot-false Compare=" + compare + " after end " + end);
559
return false;
560         }
561
562         // start x---|
563
if (!endTime && compare.equals(start))
564         {
565         // System.out.println("InSlot-true Compare=" + compare + " = Start=" + start);
566
return true;
567         }
568
569         //
570
// end |---x
571
if (endTime && compare.equals(end))
572         {
573         // System.out.println("InSlot-true Compare=" + compare + " = End=" + end);
574
return true;
575         }
576         // between start/end |-x-|
577
if (compare.before(end))
578         {
579         // System.out.println("InSlot-true Compare=" + compare + " before end " + end);
580
return true;
581         }
582         return false;
583     } // inSlot
584

585     /*************************************************************************/
586
587     /**
588      * Compares its two arguments for order. Returns a negative integer,
589      * zero, or a positive integer as the first argument is less than, equal
590      * to, or greater than the second.
591      *
592      * @param o1 the first object to be compared.
593      * @param o2 the second object to be compared.
594      * @return a negative integer, zero, or a positive integer as the
595      * first argument is less than, equal to, or greater than the
596      * second.
597      * @throws ClassCastException if the arguments' types prevent them from
598      * being compared by this Comparator.
599      */

600     public int compare(Object JavaDoc o1, Object JavaDoc o2)
601     {
602         if (!(o1 instanceof MAssignmentSlot && o2 instanceof MAssignmentSlot))
603             throw new ClassCastException JavaDoc ("MAssignmentSlot.compare arguments not MAssignmentSlot");
604
605         MAssignmentSlot s1 = (MAssignmentSlot)o1;
606         MAssignmentSlot s2 = (MAssignmentSlot)o2;
607
608         // Start Date
609
int result = s1.getStartTime().compareTo(s2.getStartTime());
610         if (result != 0)
611             return result;
612         // Status
613
result = s2.getStatus() - s1.getStatus();
614         if (result != 0)
615             return result;
616         // End Date
617
result = s1.getEndTime().compareTo(s2.getEndTime());
618         if (result != 0)
619             return result;
620         // Name
621
result = s1.getName().compareTo(s2.getName());
622         if (result != 0)
623             return result;
624         // Description
625
return s1.getDescription().compareTo(s2.getDescription());
626     } // compare
627

628     /**
629      * Indicates whether some other object is &quot;equal to&quot; this
630      * Comparator.
631      * @param obj the reference object with which to compare.
632      * @return <code>true</code> only if the specified object is also
633      * a comparator and it imposes the same ordering as this
634      * comparator.
635      * @see java.lang.Object#equals(java.lang.Object)
636      * @see java.lang.Object#hashCode()
637      */

638     public boolean equals(Object JavaDoc obj)
639     {
640         if (obj instanceof MAssignmentSlot)
641         {
642             MAssignmentSlot cmp = (MAssignmentSlot)obj;
643             if (m_startTime.equals(cmp.getStartTime())
644                 && m_endTime.equals(cmp.getEndTime())
645                 && m_status == cmp.getStatus()
646                 && m_name.equals(cmp.getName())
647                 && m_description.equals(cmp.getDescription()))
648                 return true;
649         }
650         return false;
651     } // equals
652

653     /**
654      * HashCode of MAssignmentSlot
655      * @return has code
656      */

657     public int hashCode()
658     {
659         return m_startTime.hashCode() + m_endTime.hashCode() + m_status
660             + m_name.hashCode() + m_description.hashCode();
661     } // hashCode
662

663 } // MAssignmentSlot
664
Popular Tags