source: contrib/funambol/trunk/modules/psync/src/main/java/br/com/prognus/psync/engine/source/PIMSyncSource.java @ 2082

Revision 2082, 7.4 KB checked in by emersonfaria, 14 years ago (diff)

Ticket #927 - Reestruturacao dos diretorios do Funambol

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.engine.source;
20
21import java.io.Serializable;
22import java.sql.Timestamp;
23import java.util.TimeZone;
24
25import br.com.prognus.psync.exception.EntityException;
26import br.com.prognus.psync.items.manager.PIMEntityManager;
27import br.com.prognus.psync.util.Def;
28
29import com.funambol.framework.core.AlertCode;
30import com.funambol.framework.engine.SyncItem;
31import com.funambol.framework.engine.SyncItemKey;
32import com.funambol.framework.engine.SyncItemState;
33import com.funambol.framework.engine.source.AbstractSyncSource;
34import com.funambol.framework.engine.source.MergeableSyncSource;
35import com.funambol.framework.engine.source.SyncContext;
36import com.funambol.framework.engine.source.SyncSourceException;
37import com.funambol.framework.logging.FunambolLogger;
38import com.funambol.framework.logging.FunambolLoggerFactory;
39import com.funambol.framework.security.Sync4jPrincipal;
40import com.funambol.framework.server.Sync4jDevice;
41import com.funambol.framework.tools.beans.LazyInitBean;
42
43public abstract class PIMSyncSource extends AbstractSyncSource implements
44                MergeableSyncSource, Serializable, LazyInitBean {
45
46        // ----------------------------------------------------------------
47        // Constants
48
49        protected static final String JNDI_DATA_SOURCE_NAME = "jdbc/fnblds";
50
51        public static final int SIFC = 0; // To be used as index for SIF-Contact
52
53        public static final int SIFE = 1; // To be used as index for SIF-Event
54
55        public static final int SIFN = 2; // To be used as index for SIF-Note
56
57        public static final int SIFT = 3; // To be used as index for SIF-Task
58
59        public static final int VCARD = 4; // To be used as index for VCard
60
61        public static final int VCAL = 5; // To be used as index for VCal
62
63        public static final int ICAL = 6; // To be used as index for ICal
64
65        public static final int VNOTE = 7; // To be used as index for VNote
66
67        public static final String[] TYPE = { "text/x-s4j-sifc", // SIF-Contact
68                        "text/x-s4j-sife", // SIF-Event
69                        "text/x-s4j-sifn", // SIF-Note
70                        "text/x-s4j-sift", // SIF-Task
71                        "text/x-vcard", // VCard
72                        "text/x-vcalendar", // VCal
73                        "text/calendar", // ICal
74                        "text/x-vnote", // VNote
75        };
76
77        // ----------------------------------------------------------- Protected
78        // data
79
80        protected static final FunambolLogger log = FunambolLoggerFactory
81                        .getLogger(Def.LOGGER_NAME);
82
83        protected PIMEntityManager manager;
84
85        protected Sync4jPrincipal principal;
86
87        protected String userId;
88
89        protected int syncMode;
90
91        protected Timestamp lastSyncTime; // n-th synchronization
92
93        protected Timestamp previousSyncTime; // (n - 1)-th synchronization
94
95        protected TimeZone deviceTimeZone = null;
96
97        protected String deviceTimeZoneDescription = null;
98
99        protected String deviceCharset = null;
100
101        // ----------------------------------------------------------- Public
102        // methods
103
104        public void beginSync(SyncContext context) {
105
106                log.trace("PIMSyncSource beginSync start");
107
108                principal = context.getPrincipal();
109                userId = principal.getUsername();
110                syncMode = context.getSyncMode();
111
112                Sync4jDevice device = context.getPrincipal().getDevice();
113                String timezone = device.getTimeZone();
114                if (device.getConvertDate()) {
115                        if (timezone != null && timezone.length() > 0) {
116                                deviceTimeZoneDescription = timezone;
117                                deviceTimeZone = TimeZone
118                                                .getTimeZone(deviceTimeZoneDescription);
119                        }
120                }
121                deviceCharset = device.getCharset();
122
123                if (log.isTraceEnabled()) {
124                        StringBuilder sb = new StringBuilder("Beginning sync with:");
125                        sb.append("\n> syncMode            : ").append(syncMode);
126                        sb.append("\n> principal           : ").append(principal);
127                        sb.append("\n> deviceTimeZoneDescr.: ").append(
128                                        deviceTimeZoneDescription);
129                        sb.append("\n> deviceTimeZone      : ").append(deviceTimeZone);
130                        sb.append("\n> charset             : ").append(deviceCharset);
131
132                        log.trace(sb.toString());
133                }
134
135                if (syncMode == AlertCode.REFRESH_FROM_CLIENT) {
136                        if (log.isTraceEnabled()) {
137                                log.trace("Performing REFRESH_FROM_CLIENT (203)");
138                        }
139                        removeAllSyncItems();
140                }
141
142                log.trace("PIMSyncSource beginSync end");
143        }
144
145        public void endSync() {
146        }
147
148        /**
149         * Gets the status of the SyncItem with the given key.
150         *
151         * @param syncItemKey
152         *            as a SyncItemKey object
153         * @throws SyncSourceException
154         * @return the status as a char
155         */
156        public char getSyncItemStateFromId(SyncItemKey syncItemKey)
157                        throws SyncSourceException {
158
159                if (log.isTraceEnabled()) {
160                        log.trace("PIMSyncSource getSyncItemStateFromId begin");
161                }
162
163                String id = "N/A"; // default value for error tracking
164
165                try {
166
167                        // Slow sync
168                        // @todo Implement, depending on a syncMode check
169
170                        // Fast sync
171                        id = syncItemKey.getKeyAsString();
172                        char itemRawState = manager.getItemState(id, previousSyncTime);
173
174                        if (log.isTraceEnabled()) {
175                                log.trace("PIMSyncSource getSyncItemStateFromId end");
176                        }
177
178                        if (itemRawState == Def.PIM_STATE_UNCHANGED) {
179                                return SyncItemState.SYNCHRONIZED;
180                        } else {
181                                return itemRawState; // Def uses SyncItemState.* as constant
182                                // values for N, D and U states
183                        }
184                } catch (EntityException ee) {
185                        throw new SyncSourceException(
186                                        "Error getting the state of SyncItem " + "with ID " + id,
187                                        ee);
188                }
189
190        }
191
192        /**
193         * Not yet used. It just produces a log message.
194         */
195        public void setOperationStatus(String operation, int statusCode,
196                        SyncItemKey[] keys) {
197
198                if (log.isTraceEnabled()) {
199                        StringBuilder message = new StringBuilder("Received status code '");
200                        message.append(statusCode).append("' for a '").append(operation)
201                                        .append("' command for the following items: ");
202
203                        for (int i = 0; i < keys.length; i++) {
204                                message.append("\n> ").append(keys[i].getKeyAsString());
205                        }
206
207                        log.trace(message.toString());
208                }
209        }
210
211        // ---------------------------------------------------------- Private
212        // methods
213
214        /**
215         * Extracts the content from a syncItem.
216         *
217         * @param syncItem
218         * @return as a String object
219         */
220        protected String getContentFromSyncItem(SyncItem syncItem) {
221
222                byte[] itemContent = syncItem.getContent();
223
224                // Add content processing here, if needed
225
226                return new String(itemContent == null ? new byte[0] : itemContent);
227        }
228
229        protected void saveSyncTiming(Timestamp since, Timestamp to) {
230
231                if (log.isTraceEnabled()) {
232                        if (!since.equals(previousSyncTime)) {
233                                log.trace("PIMSyncSource sync timing updated to " + since
234                                                + " / " + to);
235                        }
236                }
237                this.previousSyncTime = since;
238                this.lastSyncTime = to;
239        }
240
241        protected void removeAllSyncItems() {
242
243                if (log.isTraceEnabled()) {
244                        log.trace("Perform REFRESH_FROM_CLIENT (203) for user " + userId);
245                }
246
247                try {
248                        manager.removeAllItems();
249                } catch (EntityException ee) {
250
251                        if (log.isTraceEnabled()) {
252                                log.trace("Error while performing REFRESH_FROM_CLIENT (203).",
253                                                ee);
254                        }
255                }
256        }
257
258}
Note: See TracBrowser for help on using the repository browser.