[1009] | 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 | */ |
---|
| 19 | package br.com.prognus.psync.engine.source; |
---|
| 20 | |
---|
| 21 | import java.io.Serializable; |
---|
| 22 | import java.sql.Timestamp; |
---|
| 23 | import java.util.TimeZone; |
---|
| 24 | |
---|
| 25 | import br.com.prognus.psync.exception.EntityException; |
---|
| 26 | import br.com.prognus.psync.items.manager.PIMEntityManager; |
---|
| 27 | import br.com.prognus.psync.util.Def; |
---|
| 28 | |
---|
| 29 | import com.funambol.framework.core.AlertCode; |
---|
| 30 | import com.funambol.framework.engine.SyncItem; |
---|
| 31 | import com.funambol.framework.engine.SyncItemKey; |
---|
| 32 | import com.funambol.framework.engine.SyncItemState; |
---|
| 33 | import com.funambol.framework.engine.source.AbstractSyncSource; |
---|
| 34 | import com.funambol.framework.engine.source.MergeableSyncSource; |
---|
| 35 | import com.funambol.framework.engine.source.SyncContext; |
---|
| 36 | import com.funambol.framework.engine.source.SyncSourceException; |
---|
| 37 | import com.funambol.framework.logging.FunambolLogger; |
---|
| 38 | import com.funambol.framework.logging.FunambolLoggerFactory; |
---|
| 39 | import com.funambol.framework.security.Sync4jPrincipal; |
---|
| 40 | import com.funambol.framework.server.Sync4jDevice; |
---|
| 41 | import com.funambol.framework.tools.beans.LazyInitBean; |
---|
| 42 | |
---|
| 43 | public 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 | |
---|
[1103] | 108 | this.sourceQuery = context.getSourceQuery(); |
---|
| 109 | |
---|
[1009] | 110 | principal = context.getPrincipal(); |
---|
| 111 | userId = principal.getUsername(); |
---|
| 112 | syncMode = context.getSyncMode(); |
---|
| 113 | |
---|
| 114 | Sync4jDevice device = context.getPrincipal().getDevice(); |
---|
| 115 | String timezone = device.getTimeZone(); |
---|
| 116 | if (device.getConvertDate()) { |
---|
| 117 | if (timezone != null && timezone.length() > 0) { |
---|
| 118 | deviceTimeZoneDescription = timezone; |
---|
| 119 | deviceTimeZone = TimeZone |
---|
| 120 | .getTimeZone(deviceTimeZoneDescription); |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | deviceCharset = device.getCharset(); |
---|
| 124 | |
---|
| 125 | if (log.isTraceEnabled()) { |
---|
| 126 | StringBuilder sb = new StringBuilder("Beginning sync with:"); |
---|
| 127 | sb.append("\n> syncMode : ").append(syncMode); |
---|
| 128 | sb.append("\n> principal : ").append(principal); |
---|
| 129 | sb.append("\n> deviceTimeZoneDescr.: ").append( |
---|
| 130 | deviceTimeZoneDescription); |
---|
| 131 | sb.append("\n> deviceTimeZone : ").append(deviceTimeZone); |
---|
| 132 | sb.append("\n> charset : ").append(deviceCharset); |
---|
| 133 | |
---|
| 134 | log.trace(sb.toString()); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | if (syncMode == AlertCode.REFRESH_FROM_CLIENT) { |
---|
| 138 | if (log.isTraceEnabled()) { |
---|
| 139 | log.trace("Performing REFRESH_FROM_CLIENT (203)"); |
---|
| 140 | } |
---|
| 141 | removeAllSyncItems(); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | log.trace("PIMSyncSource beginSync end"); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | public void endSync() { |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * Gets the status of the SyncItem with the given key. |
---|
[1103] | 152 | * |
---|
[1009] | 153 | * @param syncItemKey |
---|
| 154 | * as a SyncItemKey object |
---|
| 155 | * @throws SyncSourceException |
---|
| 156 | * @return the status as a char |
---|
| 157 | */ |
---|
| 158 | public char getSyncItemStateFromId(SyncItemKey syncItemKey) |
---|
| 159 | throws SyncSourceException { |
---|
| 160 | |
---|
| 161 | if (log.isTraceEnabled()) { |
---|
| 162 | log.trace("PIMSyncSource getSyncItemStateFromId begin"); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | String id = "N/A"; // default value for error tracking |
---|
| 166 | |
---|
| 167 | try { |
---|
| 168 | |
---|
| 169 | // Slow sync |
---|
| 170 | // @todo Implement, depending on a syncMode check |
---|
| 171 | |
---|
| 172 | // Fast sync |
---|
| 173 | id = syncItemKey.getKeyAsString(); |
---|
| 174 | char itemRawState = manager.getItemState(id, previousSyncTime); |
---|
| 175 | |
---|
| 176 | if (log.isTraceEnabled()) { |
---|
| 177 | log.trace("PIMSyncSource getSyncItemStateFromId end"); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | if (itemRawState == Def.PIM_STATE_UNCHANGED) { |
---|
| 181 | return SyncItemState.SYNCHRONIZED; |
---|
| 182 | } else { |
---|
| 183 | return itemRawState; // Def uses SyncItemState.* as constant |
---|
| 184 | // values for N, D and U states |
---|
| 185 | } |
---|
| 186 | } catch (EntityException ee) { |
---|
| 187 | throw new SyncSourceException( |
---|
| 188 | "Error getting the state of SyncItem " + "with ID " + id, |
---|
| 189 | ee); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | * Not yet used. It just produces a log message. |
---|
| 196 | */ |
---|
| 197 | public void setOperationStatus(String operation, int statusCode, |
---|
| 198 | SyncItemKey[] keys) { |
---|
| 199 | |
---|
| 200 | if (log.isTraceEnabled()) { |
---|
| 201 | StringBuilder message = new StringBuilder("Received status code '"); |
---|
| 202 | message.append(statusCode).append("' for a '").append(operation) |
---|
| 203 | .append("' command for the following items: "); |
---|
| 204 | |
---|
| 205 | for (int i = 0; i < keys.length; i++) { |
---|
| 206 | message.append("\n> ").append(keys[i].getKeyAsString()); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | log.trace(message.toString()); |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | // ---------------------------------------------------------- Private |
---|
| 214 | // methods |
---|
| 215 | |
---|
| 216 | /** |
---|
| 217 | * Extracts the content from a syncItem. |
---|
[1103] | 218 | * |
---|
[1009] | 219 | * @param syncItem |
---|
| 220 | * @return as a String object |
---|
| 221 | */ |
---|
| 222 | protected String getContentFromSyncItem(SyncItem syncItem) { |
---|
| 223 | |
---|
| 224 | byte[] itemContent = syncItem.getContent(); |
---|
| 225 | |
---|
| 226 | // Add content processing here, if needed |
---|
| 227 | |
---|
| 228 | return new String(itemContent == null ? new byte[0] : itemContent); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | protected void saveSyncTiming(Timestamp since, Timestamp to) { |
---|
| 232 | |
---|
| 233 | if (log.isTraceEnabled()) { |
---|
| 234 | if (!since.equals(previousSyncTime)) { |
---|
| 235 | log.trace("PIMSyncSource sync timing updated to " + since |
---|
| 236 | + " / " + to); |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | this.previousSyncTime = since; |
---|
| 240 | this.lastSyncTime = to; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | protected void removeAllSyncItems() { |
---|
| 244 | |
---|
| 245 | if (log.isTraceEnabled()) { |
---|
| 246 | log.trace("Perform REFRESH_FROM_CLIENT (203) for user " + userId); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | try { |
---|
| 250 | manager.removeAllItems(); |
---|
| 251 | } catch (EntityException ee) { |
---|
| 252 | |
---|
| 253 | if (log.isTraceEnabled()) { |
---|
| 254 | log.trace("Error while performing REFRESH_FROM_CLIENT (203).", |
---|
| 255 | ee); |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | } |
---|