PageRenderTime 32ms CodeModel.GetById 15ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/db/objects/Schedule.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 401 lines | 246 code | 39 blank | 116 comment | 23 complexity | 435a1b19318adc5236ac37a0fad7d9d5 MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *
  4 *      YaBS is free software: you can redistribute it and/or modify
  5 *      it under the terms of the GNU General Public License as published by
  6 *      the Free Software Foundation, either version 3 of the License, or
  7 *      (at your option) any later version.
  8 *
  9 *      YaBS is distributed in the hope that it will be useful,
 10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *      GNU General Public License for more details.
 13 *
 14 *      You should have received a copy of the GNU General Public License
 15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17package mpv5.db.objects;
 18
 19import java.util.ArrayList;
 20import java.util.Calendar;
 21import java.util.Date;
 22import javax.swing.JComponent;
 23import mpv5.db.common.Context;
 24import mpv5.db.common.DatabaseObject;
 25import mpv5.db.common.NodataFoundException;
 26import mpv5.db.common.QueryCriteria;
 27import mpv5.db.common.QueryHandler;
 28import mpv5.db.objects.Item;
 29import mpv5.logging.Log;
 30import mpv5.usermanagement.MPSecurityManager;
 31import mpv5.utils.date.DateConverter;
 32import mpv5.utils.date.vTimeframe;
 33import mpv5.utils.images.MPIcon;
 34
 35/**
 36 *
 37 *  
 38 */
 39public class Schedule extends DatabaseObject {
 40    private static final long serialVersionUID = 1L;
 41
 42    private int usersids = 4343;
 43    private int itemsids;
 44    private int contactsids;
 45    private Date nextdate = new Date();
 46    private Date stopdate = new Date();
 47    private Date startdate = new Date();
 48    private int intervalmonth = 1;
 49    private int eventtype = 0;
 50    private Item item;
 51    private boolean isdone;
 52    private Contact contact;
 53
 54    public Schedule() {
 55       setContext(Context.getSchedule());
 56    }
 57
 58    /**
 59     *
 60     * @return A vTimeframe object with this schedules day set
 61     */
 62    public vTimeframe getDate() {
 63        Calendar c = Calendar.getInstance();
 64        c.setTime(__getNextdate());
 65        c.add(Calendar.DAY_OF_MONTH, 1);
 66        vTimeframe t = new vTimeframe(__getNextdate(), new Date(c.getTimeInMillis() - 1));
 67        return t;
 68    }
 69
 70    /**
 71     *
 72     * @return The item assigned to this schedule object
 73     * @throws mpv5.db.common.NodataFoundException
 74     */
 75    public Item getItem() throws NodataFoundException {
 76        if (item == null && __getItemsids() >= 0) {
 77            item = (Item) DatabaseObject.getObject(Context.getItem(), __getItemsids());
 78        }
 79        return item;
 80    }
 81    
 82        /**
 83     *
 84     * @return The item assigned to this schedule object
 85     * @throws mpv5.db.common.NodataFoundException
 86     */
 87    public Contact getContact() throws NodataFoundException {
 88        if (contact == null && __getContactsids() >= 0 ) {
 89            contact = (Contact) DatabaseObject.getObject(Context.getContact(), __getContactsids());
 90        }
 91        return contact;
 92    }
 93
 94    /**
 95     *
 96     * @param date
 97     * @return
 98     */
 99    public static ArrayList<Schedule> getEvents(vTimeframe date) {
100        Object[][] data = QueryHandler.instanceOf().clone(Context.getSchedule()).select("ids", null, "nextdate", date);
101        ArrayList<Schedule> l = new ArrayList<Schedule>();
102        for (int i = 0; i < data.length; i++) {
103            Object[] objects = data[i];
104            try {
105                Schedule s = (Schedule) DatabaseObject.getObject(Context.getSchedule(), Integer.valueOf(objects[0].toString()));
106                l.add(s);
107            } catch (NodataFoundException ex) {
108                Log.Debug(ex);
109            }
110        }
111        return l;
112    }
113
114    /**
115     *
116     * @param date
117     * @return
118     */
119    public static ArrayList<Schedule> getEvents2(vTimeframe date) {
120        ArrayList<Schedule> l = new ArrayList<Schedule>();
121        QueryCriteria criteria = new QueryCriteria("intervalmonth", 0);
122        try {
123            Object[][] data = QueryHandler.instanceOf().clone(Context.getSchedule()).select("IDS", criteria, date, "nextdate");
124            for (int i = 0; i < data.length; i++) {
125                Object[] objects = data[i];
126                Schedule s = (Schedule) DatabaseObject.getObject(Context.getSchedule(), Integer.valueOf(objects[0].toString()));
127                l.add(s);
128            }
129            return l;
130        } catch (NodataFoundException ex) {
131            Log.Debug(Schedule.class, ex.getMessage());
132            return l;
133        }
134    }
135    
136    /**
137     *
138     * @param dataOwner
139     * @return
140     */
141    public static ArrayList<Schedule> getEvents(Contact dataOwner) {
142        ArrayList<Schedule> l = new ArrayList<Schedule>();
143        if (dataOwner != null) {
144            String query =
145                    "SELECT ids FROM " + Context.getSchedule().getDbIdentity() +
146                    " WHERE itemsids IN (SELECT ids FROM " +
147                    Context.getItem().getDbIdentity() +
148                    " WHERE contactsids = " + dataOwner.__getIDS() + ")";
149            Object[][] data = QueryHandler.instanceOf().clone(Context.getSchedule()).freeSelectQuery(query, MPSecurityManager.VIEW, null).getData();
150
151            for (int i = 0; i < data.length; i++) {
152                Object[] objects = data[i];
153                try {
154                    Schedule s = (Schedule) DatabaseObject.getObject(Context.getSchedule(), Integer.valueOf(objects[0].toString()));
155                    l.add(s);
156                } catch (NodataFoundException ex) {
157                    Log.Debug(ex);
158                }
159            }
160        }
161        return l;
162    }
163
164        /**
165     *
166     * @param dataOwner
167     * @return
168     */
169    public static ArrayList<Schedule> getEvents2(Contact dataOwner) {
170        ArrayList<Schedule> l = new ArrayList<Schedule>();
171        Object[][] data;
172        if (dataOwner != null) {
173            QueryCriteria criteria = new QueryCriteria("contactsids", dataOwner.__getIDS());
174            try {
175                data = QueryHandler.instanceOf().clone(Context.getSchedule()).select("IDS", criteria);
176                for (int i = 0; i < data.length; i++) {
177                    Object[] objects = data[i];
178                    try {
179                        Schedule s = (Schedule) DatabaseObject.getObject(Context.getSchedule(), Integer.valueOf(objects[0].toString()));
180                        l.add(s);
181                    } catch (NodataFoundException ex) {
182                        Log.Debug(ex);
183                    }
184                }
185            } catch (NodataFoundException ex) {
186                Log.Debug(Schedule.class, ex.getMessage());
187            }
188        }
189        return l;
190    }
191    
192    /**
193     *
194     * @param dataOwner 
195     * @return
196     */
197    public static ArrayList<Schedule> getEvents(Item dataOwner) {
198        ArrayList<Schedule> l = new ArrayList<Schedule>();
199        if (dataOwner != null) {
200            String query =
201                    "SELECT ids FROM " + Context.getSchedule().getDbIdentity() +
202                    " WHERE itemsids  = " + dataOwner.__getIDS();
203            Object[][] data = QueryHandler.instanceOf().clone(Context.getSchedule()).freeSelectQuery(query, MPSecurityManager.VIEW, null).getData();
204
205            for (int i = 0; i < data.length; i++) {
206                Object[] objects = data[i];
207                try {
208                    Schedule s = (Schedule) DatabaseObject.getObject(Context.getSchedule(), Integer.valueOf(objects[0].toString()));
209                    l.add(s);
210                } catch (NodataFoundException ex) {
211                    Log.Debug(ex);
212                }
213            }
214        }
215        return l;
216    }
217
218        /**
219     *
220     * @param dataOwner 
221     * @return
222     */
223    public static ArrayList<Schedule> getEvents2(Item dataOwner) {
224        ArrayList<Schedule> l = new ArrayList<Schedule>();
225        Object[][] data;
226        if (dataOwner != null) {
227            QueryCriteria criteria = new QueryCriteria("itemsids", dataOwner.__getIDS());
228            try {
229                data = QueryHandler.instanceOf().clone(Context.getSchedule()).select("IDS", criteria);
230                for (int i = 0; i < data.length; i++) {
231                    Object[] objects = data[i];
232                    try {
233                        Schedule s = (Schedule) DatabaseObject.getObject(Context.getSchedule(), Integer.valueOf(objects[0].toString()));
234                        if (s.__getIntervalmonth() >= 1) {
235                            l.add(s);
236                        }
237                    } catch (NodataFoundException ex) {
238                        Log.Debug(ex);
239                    }
240                }
241            } catch (NodataFoundException ex) {
242                Log.Debug(Schedule.class, ex.getMessage());
243            }
244        }
245        return l;
246    }
247    
248    /**
249     *
250     * @param day
251     * @return
252     */
253    public static ArrayList<Schedule> getEvents(Date day) {
254        return getEvents(new vTimeframe(day, new Date(DateConverter.addDay(day).getTime() - 1)));
255    }
256
257    public static ArrayList<Schedule> getEvents2(Date day) {
258        return getEvents2(new vTimeframe(day, new Date(DateConverter.addDay(day).getTime() - 1)));
259    }
260    
261    @Override
262    public JComponent getView() {
263        throw new UnsupportedOperationException("Not supported yet.");
264    }
265
266    /**
267     * @return the usersids
268     */
269    public int __getUsersids() {
270        return usersids;
271    }
272
273    /**
274     * @param usersids the usersids to set
275     */
276    public void setUsersids(int usersids) {
277        this.usersids = usersids;
278    }
279
280    /**
281     * @return the itemsids
282     */
283    public int __getItemsids() {
284        return itemsids;
285    }
286
287    /**
288     * @param itemsids the itemsids to set
289     */
290    public void setItemsids(int itemsids) {
291        this.itemsids = itemsids;
292    }
293
294    public int __getContactsids() {
295        return contactsids;
296    }
297
298    public void setContactsids(int contactsids) {
299        this.contactsids = contactsids;
300    }
301
302    public int __getEventtype() {
303        return eventtype;
304    }
305
306    public void setEventtype(int eventtype) {
307        this.eventtype = eventtype;
308    }
309
310    
311    /**
312     * @return the nextdate
313     */
314    public Date __getNextdate() {
315        return nextdate;
316    }
317
318    /**
319     * @param nextdate the nextdate to set
320     */
321    public void setNextdate(Date nextdate) {
322        this.nextdate = nextdate;
323    }
324
325    /**
326     * @return the intervalmonth
327     */
328    public int __getIntervalmonth() {
329        return intervalmonth;
330    }
331
332    /**
333     * @param intervalmonth the intervalmonth to set
334     */
335    public void setIntervalmonth(int intervalmonth) {
336        this.intervalmonth = intervalmonth;
337    }
338
339    @Override
340    public mpv5.utils.images.MPIcon getIcon() {
341        if (!__getIsdone()) {
342            return new MPIcon("/mpv5/resources/images/32/knewstuff.png");
343        } else {
344            return new MPIcon("/mpv5/resources/images/32/clean.png");
345        }
346    }
347        
348    /**
349     * Array representation of this event.
350     * <br/>{getItem(), intervalmonth, stopdate, user}
351     * @return
352     */
353    @Override
354    public Object[] toArray() {
355        Object[] t = new Object[]{this, intervalmonth, DateConverter.getDefDateString(stopdate),
356            User.getUsername(usersids)};
357        return t;
358    }
359
360    /**
361     * @return the stopdate
362     */
363    public Date __getStopdate() {
364        return stopdate;
365    }
366
367    /**
368     * @param stopdate the stopdate to set
369     */
370    public void setStopdate(Date stopdate) {
371        this.stopdate = stopdate;
372    }
373
374    /**
375     * @return the startdate
376     */
377    public Date __getStartdate() {
378        return startdate;
379    }
380
381    /**
382     * @param startdate the startdate to set
383     */
384    public void setStartdate(Date startdate) {
385        this.startdate = startdate;
386    }
387
388    /**
389     * @return the isdone
390     */
391    public boolean __getIsdone() {
392        return isdone;
393    }
394
395    /**
396     * @param isdone the isdone to set
397     */
398    public void setIsdone(boolean isdone) {
399        this.isdone = isdone;
400    }
401}