source: contrib/psync/src/main/java/br/com/prognus/psync/items/manager/PIMCalendarManager.java @ 1009

Revision 1009, 4.7 KB checked in by wmerlotto, 15 years ago (diff)

Ticket #554 - Commit da versão inicial do psync.

Line 
1/**
2 *
3 * @author Diorgenes Felipe Grzesiuk <diorgenes@prognus.com.br>
4 * @copyright Copyright 2007-2008 Prognus
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Foobar; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19package br.com.prognus.psync.items.manager;
20
21import java.sql.Timestamp;
22import java.util.List;
23
24import br.com.prognus.psync.exception.EntityException;
25import br.com.prognus.psync.exception.PIMDBAccessException;
26import br.com.prognus.psync.items.dao.PIMCalendarDAO;
27import br.com.prognus.psync.items.dao.PIMEntityDAO;
28import br.com.prognus.psync.items.model.CalendarWrapper;
29
30import com.funambol.common.pim.calendar.Calendar;
31import com.funambol.framework.security.Sync4jPrincipal;
32import com.funambol.framework.tools.merge.MergeResult;
33
34public class PIMCalendarManager extends PIMEntityManager {
35
36        // ------------------------------------------------------------- Private
37        // data
38
39        private PIMCalendarDAO dao;
40
41        // -------------------------------------------------------------
42        // Constructors
43
44        /**
45         * @see PIMEntityDAO#PIMEntityDAO(String, String)
46         */
47        public PIMCalendarManager(String jndiDataSourceName,
48                        Sync4jPrincipal principal) {
49
50                this.dao = new PIMCalendarDAO(jndiDataSourceName, principal);
51                super.dao = this.dao;
52
53                log.trace("Created new PIMCalendarManager for user ID "
54                                + principal.getUsername());
55        }
56
57        // ----------------------------------------------------------- Public
58        // methods
59
60        /**
61         * Updates a calendar.
62         *
63         * @param uid
64         *            the UID of the calendar
65         * @param c
66         *            a Calendar object
67         * @param t
68         *            the last update time that will be forced as the last update
69         *            time of the updated calendar
70         * @return the UID of the calendar (it should be the same as the argument)
71         * @throws EntityException
72         */
73        public String updateItem(String uid, Calendar c, Timestamp t)
74                        throws EntityException {
75
76                log.trace("Updating calendar with ID " + uid
77                                + " on the server, setting " + t
78                                + " as its last update timestamp.");
79
80                CalendarWrapper cw = new CalendarWrapper(uid, this.dao.getUserId(), c);
81                cw.setLastUpdate(t);
82
83                try {
84                        this.dao.updateItem(cw);
85                        return cw.getId();
86                } catch (Exception e) {
87                        throw new EntityException("Error updating item.", e);
88                }
89        }
90
91        /**
92         * @see PIMCalendarDAO#addItem(Calendar)
93         */
94        public String addItem(Calendar c, Timestamp t) throws EntityException {
95
96                try {
97                        CalendarWrapper cw = createCalendarWrapper(c);
98                        cw.setLastUpdate(t);
99                        this.dao.addItem(cw);
100                        return cw.getId();
101                } catch (PIMDBAccessException dbae) {
102                        throw new EntityException("Error adding item.", dbae);
103                }
104        }
105
106        /**
107         * @see PIMCalendarDAO#getItem(String)
108         */
109        public CalendarWrapper getItem(String id) throws EntityException {
110                try {
111                        return this.dao.getItem(id);
112                } catch (PIMDBAccessException dbae) {
113                        throw new EntityException("Error getting item. " + dbae);
114                }
115        }
116
117        /**
118         * @see PIMCalendarDAO#getTwinItems(Calendar)
119         */
120        public List getTwins(Calendar c) throws EntityException {
121                try {
122                        return this.dao.getTwinItems(c);
123                } catch (PIMDBAccessException dbae) {
124                        throw new EntityException("Error getting twins of an item.", dbae);
125                }
126        }
127
128        public boolean mergeItems(String serverCalendarID, Calendar clientCalendar,
129                        Timestamp t) throws EntityException {
130
131                log.info("\n\n=> PIMCalendarManager mergeItems begin");
132                log.info("\n\n=> Merging server item " + serverCalendarID
133                                + " with its client counterpart.");
134
135                try {
136
137                        CalendarWrapper serverCalendarWrapper = this.dao
138                                        .getItem(serverCalendarID);
139                        Calendar serverCalendar = serverCalendarWrapper.getCalendar();
140                        MergeResult mergeResult = clientCalendar.merge(serverCalendar);
141
142                        if (mergeResult.isSetBRequired()) {
143                                updateItem(serverCalendarID, serverCalendar, t);
144                        }
145
146                        log.info("MergeResult: " + mergeResult);
147
148                        return mergeResult.isSetARequired();
149
150                } catch (Exception e) {
151
152                        log.error("Error merging items.", e);
153                        throw new EntityException("Error merging items. " + e);
154                }
155        }
156
157        // ---------------------------------------------------------- Private
158        // methods
159
160        private CalendarWrapper createCalendarWrapper(Calendar c) {
161
162                log.info("Created a new CalendarWrapper (UID not yet generated).");
163                return new CalendarWrapper(null, this.dao.getUserId(), c);
164        }
165}
Note: See TracBrowser for help on using the repository browser.