View Javadoc

1   package org.musicontroller.dao;
2   
3   import java.util.Calendar;
4   import java.util.Date;
5   import java.util.GregorianCalendar;
6   
7   import org.varienaja.util.DateTools;
8   
9   /**
10   * A PlaylistKey is a helperclass to extract information from real
11   * id's of Playlists. 
12   * 
13   * PlaylistID's have the following meaning:
14   *  - Positive long's point to a record in the database.
15   * 
16   *  - Negative long's have the following form yyyymmddkkkk
17   * where kkkk is the kind of the special item to load
18   * dd is the day of the month (1..28,29,30 or 31)
19   * mm is The month (1...12)
20   * yyyy is The year (0...9999)
21   * 
22   * If yyyy and mm are 0, then there is no date-part present.
23   * 
24   * @author Varienaja
25   * @version $Id: PlaylistKey.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
26   */
27  public class PlaylistKey {
28  
29  	private Long _id;
30  	private Integer _kind;
31  	private boolean _isSpecial;
32  	private int _dd;
33  	private int _mm;
34  	private int _yyyy;
35  	
36  	public PlaylistKey(long id) {
37  		_isSpecial = false;
38  		_id = null;
39  		_kind = null;
40  		decrypt(id);
41  	}
42  	
43  	/**
44  	 * Constructs a PlaylistKey from a kind and a Date. The resulting
45  	 * PlaylistKey 
46  	 * @param kind The kind (-9999...0)
47  	 * @param date a Date
48  	 */
49  	public PlaylistKey(int kind, Date date) {
50  		_isSpecial = true;
51  		_kind = kind;
52  		
53  		GregorianCalendar g=new GregorianCalendar();
54  		g.setTime(date);
55  		_yyyy = g.get(Calendar.YEAR);
56  		_mm = g.get(Calendar.MONTH)+1;
57  		_dd = g.get(Calendar.DAY_OF_MONTH);
58  	}
59  	
60  	/**
61  	 * Decrypts an id into the internal values _kind, _mm, _yyyy.
62  	 * @param id
63  	 */
64  	private void decrypt(long id) {
65  		_id = id;
66  		if (id<0) {
67  			_isSpecial = true;
68  			//...yyyymmxxxx
69  			_kind = Long.valueOf(id % 10000).intValue();
70  			if (id<-9999) {
71  				_dd = (int) (-id / 10000) % 100;
72  				_mm = (int) (-id / 1000000) % 100;
73  				_yyyy = (int) (-id / 100000000) % 10000;
74  			}
75  		}
76  	}
77  	
78  	/**
79  	 * @return whether this PlaylistKey points to an
80  	 * ordinary recordid (not special), or to some special id.
81  	 */
82  	public boolean isSpecial() {
83  		return _isSpecial;
84  	}
85  	
86  	/**
87  	 * @return The id of this PlaylistKey. Special id's are negative.
88  	 */
89  	public long getId() {
90  		if (_isSpecial) {
91  			return calcId();
92  		} else {
93  			return _id;
94  		}
95  	}
96  	
97  	/**
98  	 * @return The kkkk-part from the id. If this PlaylistKey is not special,
99  	 * garbage is returned.
100 	 */
101 	public int getKind() {
102 		return _kind;
103 	}
104 	
105 	/**
106 	 * @return The date-part from the id. If this PlaylistKey is not special,
107 	 * null is returned.
108 	 */
109 	public Date getFilter() {
110 		if (_isSpecial && _yyyy!=0) {
111 			return DateTools.getDate(_yyyy, _mm-1, _dd);
112 		} else {
113 			return null;
114 		}
115 	}
116 	
117 	/**
118 	 * Adds one month to the date-part of this PlaylistKey.
119 	 */
120 	public void addMonth() {
121 		if (_isSpecial) {
122 			_mm++;
123 			if (_mm>12) {
124 				_yyyy++;
125 				_mm=1;
126 			}
127 		}
128 	}
129 	
130 	/**
131 	 * Subtracts one month to the date-part of this PlaylistKey
132 	 */
133 	public void substractMonth() {
134 		if (_isSpecial) {
135 			_mm--;
136 			if (_mm<1) {
137 				_yyyy--;
138 				_mm=12;
139 			}
140 		}
141 	}
142 	
143 	/**
144 	 * @return The id matching the values of _kind, _dd, _mm and _yyyy.
145 	 */
146 	private long calcId() {
147 		return _kind - 10000l*_dd - 1000000l*_mm - 100000000l*_yyyy;
148 	}
149 	
150 	
151 }