SampleFileSyncListener.java

00001 package org.openmobileis.examples.mycrm.terminal.synchro;
00002 
00003 import java.io.File;
00004 
00005 import org.openmobileis.common.util.exception.SynchroException;
00006 import org.openmobileis.common.util.file.FileUtilities;
00007 import org.openmobileis.common.util.log.LogManager;
00008 import org.openmobileis.synchro.client.SynchroDescriptor;
00009 import org.openmobileis.synchro.journal.JournalManager;
00010 import org.openmobileis.synchro.journal.SimpleLogRenderer;
00011 import org.openmobileis.synchro.openmsp.OpenMSPException;
00012 import org.openmobileis.synchro.openmsp.client.DefaultOpenMSPSyncListener;
00013 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
00014 import org.openmobileis.synchro.openmsp.protocol.Command;
00015 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00016 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00017 import org.openmobileis.synchro.openmsp.protocol.Element;
00018 import org.openmobileis.synchro.openmsp.protocol.Item;
00019 import org.openmobileis.synchro.openmsp.protocol.Message;
00020 import org.openmobileis.synchro.openmsp.protocol.Result;
00021 import org.openmobileis.synchro.openmsp.protocol.Status;
00022 import org.openmobileis.synchro.security.auth.Credential;
00023 
00024 public class SampleFileSyncListener extends DefaultOpenMSPSyncListener {
00025         protected String synchroPath;
00026 
00027         boolean endsync = false;
00028 
00029         public SampleFileSyncListener() {
00030                 JournalManager.getManager().registerJournalLogRenderer(new SimpleLogRenderer(this.getSyncName(), "Files"));
00031         }
00032 
00033         public String getSyncName() {
00034                 return "samplefile";
00035         }
00036 
00037         public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00038                 super.startSync(cred, synchrodescriptor);
00039                 synchroPath = synchrodescriptor.getSynchroConduit().getIntallPath();
00040                 endsync = false;
00041         }
00042 
00043         /* (non-Javadoc)
00044          * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveResultCommand(org.openmobileis.synchro.openmsp.protocol.ContainerMessage, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00045          */
00046         public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand) throws OpenMSPException {
00047                 try {
00048                         while (resultContainer.hasMoreMessage()) {
00049                                 DataItem item = (DataItem) resultContainer.nextMessage().getElement();
00050                                 String meta = item.getMetaInformation();
00051                                 if (meta.equals("svs")) {
00052                                         String filename = item.getData();
00053                                         String inPath = this.synchroPath + File.separator + "filedir" + File.separator + filename;
00054                                         String destdir = System.getProperty("user.dir") + File.separator + "destfile";
00055                                         String outPath = destdir + File.separator + filename;
00056                                         FileUtilities.moveFile(inPath, outPath);
00057                                         if (this.isSyncOK()) {
00058                                                 Result resultCommand = (Result) resultContainer.getElement(); //result
00059                                                 long newSessionID = Long.parseLong(resultCommand.getMetaInformation());
00060                                                 NumSyncManagerDB.getManager().saveSyncNumberForService(newSessionID, this.getSyncName());
00061                                         }
00062                                 }
00063                                 if (meta.equals("nofile")) {
00064                                         /* do nothing  */
00065                                 }
00066 
00067                         }
00068                 } catch (Throwable ex) {
00069                         throw new OpenMSPException(ex);
00070                 }
00071         }
00072 
00073         public void receiveStatusCommand(Status statusCommande, ContainerMessage initialCommand) throws OpenMSPException {
00074                 LogManager.traceInfo(0, "SampleFileSyncListener receiveStatusCommand status :" + statusCommande.getStatus() + " commandRef:" + statusCommande.getCmdRef());
00075                 int status = statusCommande.getStatus();
00076                 if (status != Status.STATUS_OK) {
00077                         this.notifySynchroFailure();
00078                 }
00079         }
00080 
00081         public void sendData(Message message) throws OpenMSPException {
00082                 if (endsync) {
00083                         return; // return if an error occurs during synchro.
00084                 }
00085 
00086                 // get modified files
00087                 long ns = NumSyncManagerDB.getManager().getSyncNumberForService(this.getSyncName());
00088 
00089                 try {
00090                         Command getCommand = new Command(Element.GET, getSyncName(), getSyncName());
00091                         getCommand.setSourceSessionID(ns);
00092                         ContainerMessage getContainer = new ContainerMessage(getCommand);
00093                         Item newItem = null;
00094                         newItem = new DataItem(Element.ITEM, "Sample file name", "", null, null);
00095                         getContainer.add(newItem);
00096                         message.add(getContainer);
00097                 } catch (Throwable ex) {
00098                         this.setSynchroStatus(Status.STATUS_FAILED);
00099                         LogManager.trace(new SynchroException("DefaultDbSyncListener " + this.getSyncName() + " Send data exception ", ex));
00100                 } finally {
00101                         endsync = true;
00102                 }
00103 
00104         }
00105 
00106 }

Generated on Mon Jan 11 21:19:16 2010 for OpenMobileIS by  doxygen 1.5.4