/**
 * Copyright (c) 2014 Gang Ling.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
*/
package org.spdx.merge;

import java.util.ArrayList;
import java.util.HashMap;

import org.spdx.rdfparser.SPDXDocument;
import org.spdx.rdfparser.SPDXFile;
import org.spdx.rdfparser.SPDXLicenseInfo;
import org.spdx.rdfparser.SPDXNonStandardLicense;

/**
 * Application to build HashMaps to help SPDX documents merging. Currently, it helps mapping SPDX non-standard licenses
 * @author Gang Ling
 *
 */
public class SpdxLicenseMapper {

	private HashMap<SPDXDocument, HashMap<SPDXNonStandardLicense, SPDXNonStandardLicense>> nonStdLicIdMap = 
			new HashMap<SPDXDocument, HashMap< SPDXNonStandardLicense, SPDXNonStandardLicense>>();
	
	private SPDXDocument master = null;
	
	/**
	 * 
	 * @param masterDoc
	 */
	public SpdxLicenseMapper(SPDXDocument masterDoc){
		this.master = masterDoc;
	}
	
	/**
	 * 
	 * @param mergeDocs
	 * @param subNonStdLicInfo
	 * @return subNonStdLicInfo
	 */
	public SPDXNonStandardLicense mappingNonStdLic(SPDXDocument subDoc, SPDXNonStandardLicense subNonStdLicInfo){
		
	    HashMap<SPDXNonStandardLicense, SPDXNonStandardLicense> interMap = nonStdLicIdMap.get(subDoc);
	    if(interMap.isEmpty()){
	    	interMap = new HashMap<SPDXNonStandardLicense, SPDXNonStandardLicense>();
	    }
	    
		String NewNonStdLicId = master.getNextLicenseRef();
		SPDXNonStandardLicense subCopy = (SPDXNonStandardLicense) subNonStdLicInfo.clone();
		subNonStdLicInfo.setId(NewNonStdLicId);
		interMap.put(subCopy, subNonStdLicInfo);
		nonStdLicIdMap.put(subDoc, interMap);

		return subNonStdLicInfo;
	}
	
	/**
	 * 
	 * @param childFileInfo
	 * @param licId
	 * @return 
	 */
	public SPDXFile checkNonStdLicId(SPDXDocument spdxDoc, SPDXFile subFileInfo){
			SPDXLicenseInfo[] subLicInfo = subFileInfo.getSeenLicenses();
			HashMap<SPDXNonStandardLicense, SPDXNonStandardLicense> idMap = foundNonStdLicIds(spdxDoc);
			SPDXNonStandardLicense[] orgNonStdLics = (SPDXNonStandardLicense[]) idMap.keySet().toArray();
			ArrayList <SPDXLicenseInfo> retval = new ArrayList<SPDXLicenseInfo>();
			for(int i = 0; i < subLicInfo.length; i++){
				boolean foundLicId = false;
				for(int q = 0; q < orgNonStdLics.length; q++){
					//if the subfile's orgNonStdLic is found in the subLicInfo, 
					if(subLicInfo[i].toString().equals(orgNonStdLics[q].getId())){
						foundLicId = true;
					}
					//then we replace the orgNonStdLic's id with new id from the internal map
					if(foundLicId){
						retval.add(idMap.get(orgNonStdLics[q]));
					}else{
						retval.add(subLicInfo[i]);//if not, add this license to retval array directly. this license must be not non-standard license.
					}
				}
			}
			SPDXLicenseInfo[] mergedFileLics = new SPDXLicenseInfo[retval.size()];
			retval.toArray(mergedFileLics);
			subFileInfo.setSeenLicenses(mergedFileLics);
			retval.clear();
			return subFileInfo;
	}
	
	/**
	 * 
	 * @param spdxDoc
	 * @return foundDocMatch
	 */
	public boolean docInNonStdLicIdMap(SPDXDocument spdxDoc){
		boolean foundDocMatch = false;
		if(nonStdLicIdMap.containsKey(spdxDoc)){
			foundDocMatch = true;
		}
		return foundDocMatch;
	}
	
	/**
	 * 
	 * @param spdxDoc
	 * @return idMap
	 */
	public HashMap<SPDXNonStandardLicense, SPDXNonStandardLicense> foundNonStdLicIds(SPDXDocument spdxDoc){

		HashMap<SPDXNonStandardLicense, SPDXNonStandardLicense> idMap = 
				new HashMap<SPDXNonStandardLicense, SPDXNonStandardLicense>();
		
		idMap = this.nonStdLicIdMap.get(spdxDoc);
		return idMap;
		
	}
	
	/**
	 * 
	 * @return emptyMap
	 */
	public boolean isNonStdLicIdMapEmpty(){
		boolean emptyMap = false;
		if(nonStdLicIdMap.isEmpty()){
			emptyMap = true;
		}
		return emptyMap;
	}
	
	public void clearNonStdLicIdMap(){
		this.nonStdLicIdMap.clear();
	}
}

@@ -963,34 +963,25 @@
 ing 
+any 
 SPDX 
-non-standard 
 licenses
 %0A * 
@@ -976,16 +976,17 @@
 licenses
+.
 %0A * @aut
@@ -1087,50 +1087,36 @@
 SPDX
-NonStandard
 License
+Info
 , SPDX
-NonStandard
 License
+Info
 %3E%3E n
@@ -1178,50 +1178,36 @@
 SPDX
-NonStandard
 License
+Info
 , SPDX
-NonStandard
 License
+Info
 %3E%3E()
@@ -1596,50 +1596,36 @@
 SPDX
-NonStandard
 License
+Info
 , SPDX
-NonStandard
 License
+Info
 %3E in
@@ -1727,50 +1727,36 @@
 SPDX
-NonStandard
 License
+Info
 , SPDX
-NonStandard
 License
+Info
 %3E();
@@ -2300,50 +2300,36 @@
 SPDX
-NonStandard
 License
+Info
 , SPDX
-NonStandard
 License
+Info
 %3E id
@@ -3671,50 +3671,36 @@
 SPDX
-NonStandard
 License
+Info
 , SPDX
-NonStandard
 License
+Info
 %3E fo
@@ -3757,50 +3757,36 @@
 SPDX
-NonStandard
 License
+Info
 , SPDX
-NonStandard
 License
+Info
 %3E id
@@ -3816,50 +3816,36 @@
 SPDX
-NonStandard
 License
+Info
 , SPDX
-NonStandard
 License
+Info
 %3E();

update the SPDX license hashMap