/*******************************************************************************
 * Copyright (c) 2012 Nokia Corporation
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Comarch team - initial API and implementation
 *******************************************************************************/
package org.ned.client.command;

import com.sun.lwuit.Command;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.io.ConnectionNotFoundException;
import org.ned.client.Localization;
import org.ned.client.NedConsts;
import org.ned.client.NedMidlet;
import org.ned.client.NedResources;
import org.ned.client.utils.NedConnectionUtils;
import org.ned.client.utils.Utils;
import org.ned.client.utils.Version;
import org.ned.client.view.GeneralAlert;

public class CheckForUpdateCommand extends NedCommandAsync {

    private static CheckForUpdateCommand instance;

    private CheckForUpdateCommand() {
        command = new Command( NedResources.CHECK_FOR_UPDATE );
    }

    public static CheckForUpdateCommand getInstance() {
        if ( instance == null ) {
            instance = new CheckForUpdateCommand();
        }
        return instance;
    }

    protected void doAction( Object param ) {
        Hashtable headers = new Hashtable( 1 );
        headers.put( NedConsts.HttpHeader.CACHECONTROL, NedConsts.HttpHeaderValue.NOCACHE );

        String rsp = NedConnectionUtils.httpGet( NedConsts.NedUpdateAddress.CHECKFORUPDATEURL, headers ).
                trim();

        if ( rsp == null || rsp.length() == 0 ) {
            throw new AsyncException( NedResources.TRYAGAINLATER );
        } else {
            processUpdate( rsp );
        }
    }

    private void processUpdate( String rsp ) {
        Vector params = Utils.split( rsp, "\n" );

        Enumeration en = params.elements();
        String version = "";
        String url = "";
        while ( en.hasMoreElements() ) {
            String value = (String)en.nextElement();
            if ( value.startsWith( NedConsts.NedUpdateInfo.VERSION ) ) {
                version = getParamValue( value, NedConsts.NedUpdateInfo.VERSION );
            } else if ( value.startsWith( NedConsts.NedUpdateInfo.OVISTORE ) ) {
                url = getParamValue( value, NedConsts.NedUpdateInfo.OVISTORE );
            }
        }

        if ( isNewerVersion( version ) ) {
            String message = Localization.getMessage( NedResources.NEWVERSIONAVAILABLE,
                                                      new Object[]{ version } );
            if ( GeneralAlert.RESULT_YES == GeneralAlert.showQuestion( message ) ) {
                try {
                    NedMidlet.getInstance().platformRequest( NedConsts.NedUpdateAddress.INSTALATIONFILE );
                } catch ( ConnectionNotFoundException ex ) {
                    throw new AsyncException( ex.getMessage()
                                              + NedResources.TRYAGAINLATER );
                }
            }
        } else {
            throw new AsyncException( NedResources.NEWESTVERSION );
        }
    }

    private String getParamValue( String aInput, String aKeyName ) {
        if ( aKeyName == null && aKeyName.length() == 0 ) {
            return aInput;
        } else {
            return aInput.substring( aKeyName.length() + 1 );//+1 is for colon
        }
    }

    private boolean isNewerVersion( String aNewVersion ) {
        Version currentVer =
                Utils.versionParser( NedMidlet.getInstance().getVersion() );

        Version availableVer =
                Utils.versionParser( aNewVersion );

        if ( currentVer.Major < availableVer.Major ) {
            return true;
        } else if ( currentVer.Minor < availableVer.Minor ) {
            return true;
        } else if ( currentVer.Build < availableVer.Build ) {
            return true;
        }

        return false;
    }
}

@@ -3848,19 +3848,38 @@
        i
-f (
+nt currentVersionInt =
  current
@@ -3893,95 +3893,90 @@
 or %3C
- available
+%3C 16 %7C current
 Ver.M
-ajor ) %7B%0A            return true;%0A        %7D else if ( currentVer.Minor %3C
+inor %3C%3C 8 %7C currentVer.Build;%0A        int availableVersionInt =
  ava
@@ -3990,80 +3990,46 @@
 er.M
-inor ) %7B%0A            return true;%0A        %7D else if ( currentVer.Build %3C
+ajor %3C%3C 16 %7C availableVer.Minor %3C%3C 8 %7C
  ava
@@ -4043,25 +4043,19 @@
 er.Build
- ) %7B%0A    
+;%0A%0A
         
@@ -4065,45 +4065,47 @@
 urn 
-true;%0A        %7D%0A%0A        return false
+currentVersionInt %3C availableVersionInt
 ;%0A  

version checking update