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

Revision 1103, 4.8 KB checked in by wmerlotto, 15 years ago (diff)

Ticket #554 - Melhorias na sincronizacao de eventos, contatos e adaptacao para Funambol 7.0

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         * @throws Exception
46         * @see PIMEntityDAO#PIMEntityDAO(String, String)
47         */
48        public PIMCalendarManager(String jndiDataSourceName, Sync4jPrincipal principal, String sourceQuery) {
49
50                try {
51                        this.dao = new PIMCalendarDAO(jndiDataSourceName, principal, sourceQuery);
52                } catch (Exception e) {
53                }
54                super.dao = this.dao;
55
56                log.trace("Created new PIMCalendarManager for user ID "
57                                + principal.getUsername());
58        }
59
60        // ----------------------------------------------------------- Public
61        // methods
62
63        /**
64         * Updates a calendar.
65         *
66         * @param uid
67         *            the UID of the calendar
68         * @param c
69         *            a Calendar object
70         * @param t
71         *            the last update time that will be forced as the last update
72         *            time of the updated calendar
73         * @return the UID of the calendar (it should be the same as the argument)
74         * @throws EntityException
75         */
76        public String updateItem(String uid, Calendar c, Timestamp t)
77                        throws EntityException {
78
79                log.trace("Updating calendar with ID " + uid
80                                + " on the server, setting " + t
81                                + " as its last update timestamp.");
82
83                CalendarWrapper cw = new CalendarWrapper(uid, this.dao.getUserId(), c);
84                cw.setLastUpdate(t);
85
86                try {
87                        this.dao.updateItem(cw);
88                        return cw.getId();
89                } catch (Exception e) {
90                        throw new EntityException("Error updating item.", e);
91                }
92        }
93
94        /**
95         * @see PIMCalendarDAO#addItem(Calendar)
96         */
97        public String addItem(Calendar c, Timestamp t) throws EntityException {
98
99                try {
100                        CalendarWrapper cw = createCalendarWrapper(c);
101                        cw.setLastUpdate(t);
102                        this.dao.addItem(cw);
103                        return cw.getId();
104                } catch (PIMDBAccessException dbae) {
105                        throw new EntityException("Error adding item.", dbae);
106                }
107        }
108
109        /**
110         * @see PIMCalendarDAO#getItem(String)
111         */
112        public CalendarWrapper getItem(String id) throws EntityException {
113                try {
114                        return this.dao.getItem(id);
115                } catch (PIMDBAccessException dbae) {
116                        throw new EntityException("Error getting item. " + dbae);
117                }
118        }
119
120        /**
121         * @see PIMCalendarDAO#getTwinItems(Calendar)
122         */
123        public List getTwins(Calendar c) throws EntityException {
124                try {
125                        return this.dao.getTwinItems(c);
126                } catch (PIMDBAccessException dbae) {
127                        throw new EntityException("Error getting twins of an item.", dbae);
128                }
129        }
130
131        public boolean mergeItems(String serverCalendarID, Calendar clientCalendar,
132                        Timestamp t) throws EntityException {
133
134                log.info("\n\n=> PIMCalendarManager mergeItems begin");
135                log.info("\n\n=> Merging server item " + serverCalendarID
136                                + " with its client counterpart.");
137
138                try {
139
140                        CalendarWrapper serverCalendarWrapper = this.dao
141                                        .getItem(serverCalendarID);
142                        Calendar serverCalendar = serverCalendarWrapper.getCalendar();
143                        MergeResult mergeResult = clientCalendar.merge(serverCalendar);
144
145                        if (mergeResult.isSetBRequired()) {
146                                updateItem(serverCalendarID, serverCalendar, t);
147                        }
148
149                        log.info("MergeResult: " + mergeResult);
150
151                        return mergeResult.isSetARequired();
152
153                } catch (Exception e) {
154
155                        log.error("Error merging items.", e);
156                        throw new EntityException("Error merging items. " + e);
157                }
158        }
159
160        // ---------------------------------------------------------- Private
161        // methods
162
163        private CalendarWrapper createCalendarWrapper(Calendar c) {
164
165                log.info("Created a new CalendarWrapper (UID not yet generated).");
166                return new CalendarWrapper(null, this.dao.getUserId(), c);
167        }
168}
Note: See TracBrowser for help on using the repository browser.