srcanaapps/apiquerytool/com.nokia.s60tools.apiquery.cache/src/com/nokia/s60tools/apiquery/cache/core/job/SeekMetaXMLFilesJob.java
changeset 0 a02c979e8dfd
equal deleted inserted replaced
-1:000000000000 0:a02c979e8dfd
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */ 
       
    17  
       
    18 package com.nokia.s60tools.apiquery.cache.core.job;
       
    19 
       
    20 import java.io.File;
       
    21 import java.io.FileNotFoundException;
       
    22 import java.io.IOException;
       
    23 import java.util.Collection;
       
    24 
       
    25 import org.eclipse.core.runtime.IProgressMonitor;
       
    26 import org.eclipse.core.runtime.IStatus;
       
    27 import org.eclipse.core.runtime.Platform;
       
    28 import org.eclipse.core.runtime.Status;
       
    29 import org.eclipse.core.runtime.jobs.Job;
       
    30 import org.eclipse.swt.SWT;
       
    31 import org.eclipse.swt.widgets.Display;
       
    32 
       
    33 import com.nokia.s60tools.apiquery.cache.configuration.CacheEntry;
       
    34 import com.nokia.s60tools.apiquery.cache.configuration.CacheEntryStorage;
       
    35 import com.nokia.s60tools.apiquery.cache.resources.Messages;
       
    36 import com.nokia.s60tools.apiquery.shared.datatypes.config.DuplicateEntryException;
       
    37 import com.nokia.s60tools.apiquery.shared.datatypes.config.EntryNotFoundException;
       
    38 import com.nokia.s60tools.apiquery.shared.datatypes.config.IConfigurationChangedListener;
       
    39 import com.nokia.s60tools.apiquery.shared.job.AbstractJob;
       
    40 import com.nokia.s60tools.apiquery.shared.job.JobCancelledByUserException;
       
    41 import com.nokia.s60tools.apiquery.shared.ui.dialogs.APIQueryMessageBox;
       
    42 import com.nokia.s60tools.apiquery.shared.util.console.APIQueryConsole;
       
    43 import com.nokia.s60tools.apiquery.ui.views.main.MainView;
       
    44 import com.nokia.s60tools.sdk.SdkInformation;
       
    45 import com.nokia.s60tools.util.resource.FileFinder;
       
    46 import com.nokia.s60tools.util.resource.FileUtils;
       
    47 import com.nokia.s60tools.util.resource.IFileFinderObserver;
       
    48 
       
    49 /**
       
    50  * Job for seeking .metaxml files from under SDK ({@link SdkInformation}) given. 
       
    51  */
       
    52 public class SeekMetaXMLFilesJob extends AbstractJob implements IFileFinderObserver {
       
    53 
       
    54 	/**
       
    55 	 * Metaxml file type
       
    56 	 */
       
    57 	private static final String METAXML_FILE_SUFFIX = ".metaxml"; //$NON-NLS-1$
       
    58 
       
    59 	/**
       
    60 	 * s60 folder name in R&D SDK
       
    61 	 */
       
    62 	private static final String S60 = "s60"; //$NON-NLS-1$
       
    63 	
       
    64 	/***
       
    65 	 * SF folder 
       
    66 	 * 
       
    67 	 */
       
    68 	private static final String SF = "sf"; //$NON-NLS-1$
       
    69 
       
    70 	/**
       
    71 	 * Information about selected SDK
       
    72 	 */
       
    73 	private final SdkInformation sdkInformation;
       
    74 	
       
    75 	/**
       
    76 	 * Total steps as double, to be able to count percentages.
       
    77 	 */
       
    78 	double stepsAsDouble = 1;
       
    79 
       
    80 	/**
       
    81 	 * Folders where to seek .metaxml files under selected SDK
       
    82 	 */
       
    83 	private static final String [] FOLDERS_TO_SEEK_META_FILES = new String [] {
       
    84 		"osext",  //$NON-NLS-1$
       
    85 		"mw",  //$NON-NLS-1$
       
    86 		"app",
       
    87 		"os" }; //$NON-NLS-1$
       
    88 	
       
    89 	
       
    90 	/**
       
    91 	 * Create a Job to seek .metaxml files under selected SDK.
       
    92 	 * @param name
       
    93 	 * @param sdkInformation
       
    94 	 */
       
    95 	public SeekMetaXMLFilesJob(String name, SdkInformation sdkInformation) {
       
    96 		super(name);
       
    97 		this.sdkInformation = sdkInformation;
       
    98 		setUser(true);
       
    99 	}
       
   100 
       
   101 
       
   102 	/* (non-Javadoc)
       
   103 	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
       
   104 	 */
       
   105 	protected IStatus run(IProgressMonitor monitor) {
       
   106 		
       
   107 			
       
   108 		
       
   109 		setMonitor(monitor);
       
   110 	
       
   111 		reportStartTime();
       
   112 		try {	
       
   113 			MainView.enablePropTabcontents(false);
       
   114 			FileFinder finder = new FileFinder(APIQueryConsole.getInstance());
       
   115 
       
   116 			String epocRoot = sdkInformation.getEpocRootDir();
       
   117 			
       
   118 			//System.out.println("epoc 32 " + epocRoot);
       
   119 			
       
   120 			String message = Messages.getString("SeekMetaXMLFilesJob.StartJobMsg_Part1")  //$NON-NLS-1$
       
   121 				+METAXML_FILE_SUFFIX +Messages.getString("SeekMetaXMLFilesJob.StartJobMsg_Part2") +sdkInformation.getSdkId() //$NON-NLS-1$
       
   122 				+Messages.getString("SeekMetaXMLFilesJob.StartJobMsg_Part3") +epocRoot +"'."; //$NON-NLS-1$ //$NON-NLS-2$
       
   123 			APIQueryConsole.getInstance().println(message);
       
   124 
       
   125 			String [] folders = new String[FOLDERS_TO_SEEK_META_FILES.length];
       
   126 			  
       
   127 			boolean isS60 = true;
       
   128 			//try to check if s60 is present
       
   129 			File directoryOfPdfs = new File(epocRoot );
       
   130 
       
   131 			if(directoryOfPdfs.isDirectory()) { // check to make sure it is a directory
       
   132 			String filenames[] = directoryOfPdfs.list(); //make array of filenames.
       
   133 			for(int i =0 ;i <filenames.length;i++)
       
   134 			{
       
   135 			if (	filenames[i].equalsIgnoreCase(SF) )
       
   136 			{ isS60 = false;				
       
   137 				break;
       
   138 			}
       
   139 			}
       
   140 			}
       
   141 			
       
   142 					
       
   143 			
       
   144 			for (int i = 0; i < FOLDERS_TO_SEEK_META_FILES.length; i++) {
       
   145 				folders[i] = epocRoot + ((isS60)?S60 : SF)+File.separatorChar + FOLDERS_TO_SEEK_META_FILES[i];
       
   146 			}		
       
   147 			
       
   148 		
       
   149 			finder.seekFilesFromFolders(this, METAXML_FILE_SUFFIX, folders);
       
   150 			return Job.ASYNC_FINISH;
       
   151 		}		
       
   152 		catch (Exception e) {
       
   153 			IStatus status = new Status(
       
   154 					Status.ERROR,Platform.PI_RUNTIME,
       
   155 					Status.ERROR,Messages.getString("SeekMetaXMLFilesJob.SeekErrorMsg") +sdkInformation.getSdkId(), e); //$NON-NLS-1$
       
   156 			return status;
       
   157 		}
       
   158 		
       
   159 		
       
   160 	}
       
   161 
       
   162 	/* (non-Javadoc)
       
   163 	 * @see com.nokia.s60tools.util.resource.IFileFinderObserver#completed(int, java.util.Collection)
       
   164 	 */
       
   165 	public void completed(int exitValue, Collection<File> files) {
       
   166 
       
   167 		try {
       
   168 	
       
   169 			if(isCanceled() || exitValue == IStatus.CANCEL){
       
   170 				interrupted(Messages.getString("SeekMetaXMLFilesJob.CancelledByUser")); //$NON-NLS-1$
       
   171 			}
       
   172 			else if(exitValue == IStatus.OK){
       
   173 				handleSeekCompleatedOK(files);
       
   174 			}
       
   175 			else{
       
   176 				handleSeekCompleatedError(exitValue);
       
   177 			}
       
   178 			reportEndTime();
       
   179 		} catch (Exception e) {
       
   180 			//Handle file not found e.g. errors.
       
   181 			e.printStackTrace();
       
   182 			handleSeekCompleatedError(e);
       
   183 		}
       
   184 		finally{
       
   185 			MainView.enablePropTabcontents(true);
       
   186 			
       
   187 			
       
   188 		}
       
   189 	}
       
   190 
       
   191 	/**
       
   192 	 * Handle Error situations after getting data
       
   193 	 * @param e
       
   194 	 */
       
   195 	private void handleSeekCompleatedError(Exception e) {
       
   196 		String message = Messages.getString("SeekMetaXMLFilesJob.SeekCompleatedErrorMsg") +sdkInformation.getSdkId(); //$NON-NLS-1$
       
   197 		APIQueryConsole.getInstance().println(message, APIQueryConsole.MSG_ERROR);
       
   198 		IStatus status = new Status(
       
   199 				Status.ERROR,Platform.PI_RUNTIME,
       
   200 				Status.ERROR,message, e);
       
   201 		
       
   202 		done(status);
       
   203 	}
       
   204 	
       
   205 	/**
       
   206 	 * Handle error return values
       
   207 	 * @param exitValue
       
   208 	 */
       
   209 	private void handleSeekCompleatedError(int exitValue) {
       
   210 		String message = Messages.getString("SeekMetaXMLFilesJob.SeekCompleatedErrorMsg") +sdkInformation.getSdkId() +Messages.getString("SeekMetaXMLFilesJob.SeekCompleatedExitValueErrorMsg") +exitValue; //$NON-NLS-1$ //$NON-NLS-2$
       
   211 		APIQueryConsole.getInstance().println(message, APIQueryConsole.MSG_ERROR);		
       
   212 		IStatus status = new Status(
       
   213 				Status.ERROR,Platform.PI_RUNTIME,
       
   214 				Status.ERROR,message, null);
       
   215 		
       
   216 		done(status);
       
   217 	}
       
   218 
       
   219 	/**
       
   220 	 * If status was OK, handling completed situation
       
   221 	 * @param files
       
   222 	 * @throws FileNotFoundException
       
   223 	 * @throws IOException
       
   224 	 */
       
   225 	private void handleSeekCompleatedOK(Collection<File> files) throws FileNotFoundException, IOException {
       
   226 
       
   227 		//We are pretty much done, so marking that we are in 100% of progress, updating storage does not really take any time
       
   228 		progress(steps, Messages.getString("SeekMetaXMLFilesJob.UpdatingDB_ProgressMsg")); //$NON-NLS-1$
       
   229 
       
   230 		CacheEntryStorage storage = CacheEntryStorage.getInstance();
       
   231 
       
   232 		try {
       
   233 		
       
   234 		
       
   235 		
       
   236 			//set to data store that all are deselected
       
   237 			storage.deselectAll(true);//When SDK is changed, all entries is deselected
       
   238 				
       
   239 			for (File file : files) {
       
   240 				//Read the File , get the API Name
       
   241 				//System.out.println("file path" + file.getAbsolutePath());
       
   242 				StringBuffer buf = FileUtils.loadDataFromFile(file.getAbsolutePath());
       
   243 				int startIndex = buf.indexOf("<name>");
       
   244 					String apiName =   buf.substring(startIndex+6,  buf.indexOf("</name>",startIndex ));	
       
   245 					System.out.println("api name while seeking" + apiName);
       
   246 				   
       
   247 				CacheEntry entry = new CacheEntry(
       
   248 						file.getAbsolutePath(), file.getName(), sdkInformation.getSdkId(), 
       
   249 						true, file.length(),  file.lastModified(),apiName.trim());
       
   250 				try {
       
   251 					
       
   252 					if(isCanceled()){
       
   253 						interrupted(Messages.getString("SeekMetaXMLFilesJob.CancelledByUser")); //$NON-NLS-1$
       
   254 						return;
       
   255 					}		
       
   256 					if(storage.contains(entry)){
       
   257 						storage.updateEntry(entry, true);					
       
   258 					}else{
       
   259 						storage.addEntry(entry, true);					
       
   260 					}
       
   261 				} catch (DuplicateEntryException e) {
       
   262 					// Should not be able to occur, because if(storage.contains(entry)) check before adding
       
   263 					e.printStackTrace();
       
   264 				}
       
   265 				catch (EntryNotFoundException e) {
       
   266 					// Should not be able to occur, because if(storage.contains(entry)) check before update
       
   267 					e.printStackTrace();
       
   268 				}
       
   269 				
       
   270 	 		}	
       
   271 	
       
   272 			reportStartTime();
       
   273 			storage.loadAllSelectedDatasToMemory(getMonitor());//There can be parsing errors, so listeners must be notified afterwards 
       
   274 	
       
   275 			//Notifying listeners, only one notifying should be enaugh.
       
   276 			storage.notifyConfigurationChangeListeners(IConfigurationChangedListener.ALL_ENTRYS_UPDATED);
       
   277 			
       
   278 			//If there was some load errors.
       
   279 			if(storage.isLoadErros()){
       
   280 				JobMessageUtils.showLoadErrorDialog();
       
   281 			}		
       
   282 			reportEndTime();
       
   283 			
       
   284 			
       
   285 			if(files == null || files.size()==0){
       
   286 				showNoMetadatasWarning();			
       
   287 			}		
       
   288 			
       
   289 			done(Status.OK_STATUS);
       
   290 			
       
   291 		} catch (JobCancelledByUserException e) {
       
   292 			e.printStackTrace();
       
   293 			//return original map 
       
   294 			done(Status.CANCEL_STATUS);
       
   295 		}		
       
   296 	}
       
   297 
       
   298 	/**
       
   299 	 * Show warning dialog when no metadata found from selected sdk 
       
   300 	 */
       
   301 	private void showNoMetadatasWarning() {
       
   302 		Runnable showWarning = new Runnable(){			
       
   303 
       
   304 			public void run() {
       
   305 				APIQueryMessageBox msg = new APIQueryMessageBox(
       
   306 						Messages.getString("SeekMetaXMLFilesJob.MetadataNotFoundWarningMsg_Part1")  //$NON-NLS-1$
       
   307 						+sdkInformation.getSdkId() 
       
   308 						+Messages.getString("SeekMetaXMLFilesJob.MetadataNotFoundWarningMsg_Part2") ,  //$NON-NLS-1$
       
   309 						SWT.OK| SWT.ICON_WARNING );
       
   310 				msg.open();				
       
   311 			}
       
   312 		};
       
   313 		Display.getDefault().asyncExec(showWarning);
       
   314 	}
       
   315 
       
   316 	public void interrupted(String reasonMsg) {
       
   317 		done(Status.CANCEL_STATUS);
       
   318 		super.cancel();
       
   319 	}
       
   320 	
       
   321 	/* (non-Javadoc)
       
   322 	 * @see com.nokia.s60tools.util.resource.IFileFinderObserver#isCancelled()
       
   323 	 */
       
   324 	public boolean isCanceled(){
       
   325 		return getMonitor().isCanceled();
       
   326 	}
       
   327 	
       
   328 	/* (non-Javadoc)
       
   329 	 * @see com.nokia.s60tools.util.resource.IFileFinderObserver#beginTask(java.lang.String, int)
       
   330 	 */
       
   331 	public void beginTask(String name, int steps) {
       
   332 
       
   333 		//Its not allowed to dived with "0"
       
   334 		if(steps < 1){
       
   335 			steps = 1;
       
   336 		}
       
   337 		this.steps = steps;
       
   338 		stepsAsDouble = new Double(steps).doubleValue();
       
   339 		
       
   340 		getMonitor().beginTask(name, PROGRESS_COMPLETED_PERCENTAGE);
       
   341 	}
       
   342 
       
   343 	/* (non-Javadoc)
       
   344 	 * @see com.nokia.s60tools.apiquery.shared.job.AbstractJob#progress(int, java.lang.String)
       
   345 	 */
       
   346 	public void progress(int stepsCompleated, String prosessing) {
       
   347 
       
   348         try {
       
   349         	int persentage = (new Double( stepsCompleated / stepsAsDouble * 100)).intValue();
       
   350         	
       
   351 			super.progress(persentage, prosessing);
       
   352 		} catch (JobCancelledByUserException e) {
       
   353 			e.printStackTrace();
       
   354 			done(Status.CANCEL_STATUS);
       
   355 		}
       
   356 	}
       
   357 
       
   358 
       
   359 }