/**
 * Copyright (C) 2000 - 2009 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://repository.silverpeas.com/legal/licensing"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.silverpeas.ical;

import java.io.File;
import java.net.URL;

import com.stratelia.silverpeas.silvertrace.SilverTrace;
import com.stratelia.webactiv.agenda.control.AgendaRuntimeException;
import com.stratelia.webactiv.agenda.control.AgendaSessionController;
import com.stratelia.webactiv.calendar.control.CalendarBm;
import com.stratelia.webactiv.calendar.control.CalendarBmHome;
import com.stratelia.webactiv.util.EJBUtilitaire;
import com.stratelia.webactiv.util.JNDINames;
import com.stratelia.webactiv.util.exception.SilverpeasException;

/**
 * @author dle
 */
public class SynchroIcalManager {

  public final static String SYNCHRO_SUCCEEDED = "0";
  private AgendaSessionController agendaSessionController;
  private CalendarBm calendarBm;

  public SynchroIcalManager(AgendaSessionController agendaSessionController) {
    this.agendaSessionController = agendaSessionController;
    setCalendarBm();
  }

  public static void main(String[] args) {
    try {

      // Path to local iCalendar file
      File localCalendar = new File(
          "C:\\Silverpeas\\KMEdition\\temp\\icsCalendar\\agenda1.ics");

      URL remoteCalendar = new URL(args[0]);
      String username = args[1];
      String password = args[2];

      // Creates a synchronizer engine
      SyncEngine engine = new SyncEngine();

      // Do the synchronization remoteCalendar -> create localCalendar
      engine.synchronize(localCalendar, remoteCalendar, username, password);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Import remote calendar into Silverpeas calendar (update event if necessary)
   * @param urlCalendar
   * @param localCalendar
   * @param loginICalendar
   * @param pwdIcalendar
   * @return ReturnCode
   */
  public String synchroIcalAgenda(URL urlCalendar, File localCalendar,
      String loginICalendar, String pwdIcalendar) {
    String returnCodeSynchro = AgendaSessionController.SYNCHRO_FAILED;
    try {

      // Private iCal URL
      // Use the SyncEngine.listCalendars() method to get URLs
      URL remoteCalendar = urlCalendar;

      // Creates a synchronizer engine
      SyncEngine engine = new SyncEngine();

      // Do the synchronization :
      // Remote Calendar -> localfile Calendar
      String remoteConnect = engine.synchronize(localCalendar, remoteCalendar,
          loginICalendar, pwdIcalendar);
      if (remoteConnect.equals(SyncEngine.REMOTE_CONNECT_SUCCEEDED)) {
        // localfile -> Silverpeas Agenda
        ImportIcalManager impIcalManager = new ImportIcalManager(
            agendaSessionController);
        String returnImport = impIcalManager.importIcalAgenda(localCalendar);
        if (returnImport.equals(AgendaSessionController.IMPORT_FAILED))
          returnCodeSynchro = AgendaSessionController.SYNCHRO_FAILED;
        else
          returnCodeSynchro = AgendaSessionController.SYNCHRO_SUCCEEDED;
      } else
        returnCodeSynchro = remoteConnect;
    } catch (Exception e) {
      SilverTrace.error("agenda", "SynchroIcalManager.synchroIcalAgenda()", "",
          e.fillInStackTrace());
    }
    return returnCodeSynchro;
  }

  /**
   * Method declaration
   * @see
   */
  private void setCalendarBm() {
    if (calendarBm == null) {
      try {
        calendarBm = ((CalendarBmHome) EJBUtilitaire.getEJBObjectRef(
            JNDINames.CALENDARBM_EJBHOME, CalendarBmHome.class)).create();
      } catch (Exception e) {
        throw new AgendaRuntimeException("ImportIcalManager.setCalendarBm()",
            SilverpeasException.ERROR, "root.EX_CANT_GET_REMOTE_OBJECT", e);
      }
    }
  }
}
@@ -2052,626 +2052,14 @@
  %7D%0A%0A
-  public static void main(String%5B%5D args) %7B%0A    try %7B%0A%0A      // Path to local iCalendar file%0A      File localCalendar = new File(%0A          %22C:%5C%5CSilverpeas%5C%5CKMEdition%5C%5Ctemp%5C%5CicsCalendar%5C%5Cagenda1.ics%22);%0A%0A      URL remoteCalendar = new URL(args%5B0%5D);%0A      String username = args%5B1%5D;%0A      String password = args%5B2%5D;%0A%0A      // Creates a synchronizer engine%0A      SyncEngine engine = new SyncEngine();%0A%0A      // Do the synchronization remoteCalendar -%3E create localCalendar%0A      engine.synchronize(localCalendar, remoteCalendar, username, password);%0A    %7D catch (Exception e) %7B%0A      e.printStackTrace();%0A    %7D%0A  %7D%0A%0A
   /**%0A
+
    *
@@ -2471,17 +2471,16 @@
   try %7B%0A
-%0A
       //
@@ -2600,17 +2600,16 @@
 lendar;%0A
-%0A
       //
@@ -2683,17 +2683,16 @@
 gine();%0A
-%0A
       //
@@ -2897,29 +2897,8 @@
  if 
-(remoteConnect.equals
 (Syn
@@ -2929,16 +2929,37 @@
 UCCEEDED
+.equals(remoteConnect
 )) %7B%0A   
@@ -3250,16 +3250,18 @@
 FAILED))
+ %7B
 %0A       
@@ -3319,24 +3319,34 @@
 HRO_FAILED;%0A
+        %7D%0A
         else
@@ -3341,24 +3341,26 @@
         else
+ %7B
 %0A          r
@@ -3417,24 +3417,34 @@
 _SUCCEEDED;%0A
+        %7D%0A
       %7D else
@@ -3443,16 +3443,18 @@
   %7D else
+ %7B
 %0A       
@@ -3489,16 +3489,24 @@
 onnect;%0A
+      %7D%0A
     %7D ca

Cleaning the code