/**
 * 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.Collection;
import java.util.HashMap;

import org.spdx.compare.LicenseCompareHelper;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;
import org.spdx.rdfparser.SPDXDocument;
import org.spdx.rdfparser.SPDXNonStandardLicense;

/**
 * @author Gang Ling
 *
 */
public class SpdxLicenseInfoMerger {

	public Collection<SPDXNonStandardLicense> mergeNonStandardLic(SPDXDocument[] mergeDocs,
					HashMap<SPDXDocument,HashMap<String,String>> licIdMap) throws InvalidSPDXAnalysisException{
		
		//an array to hold the final result 
		ArrayList<SPDXNonStandardLicense> licInfoResult = new ArrayList<SPDXNonStandardLicense>();
		//an array to hold the non-standard license info from master SPDX document
		SPDXNonStandardLicense[] MasterNonStandardLicInfo = mergeDocs[0].getExtractedLicenseInfos();
		//an index number to track license id
		int licId = 0;
		//first, add master's non-standard license into the final result array
		for(int q = 0; q < MasterNonStandardLicInfo.length; q++){
			SPDXNonStandardLicense temp = MasterNonStandardLicInfo[q];
			licInfoResult.add(temp);
			licId +=1;
			temp = null;
		}
		//compare and merge non-standard license info
		for(int i = 1; i < mergeDocs.length; i++){
			//an array to hold non-standard license info from current child SPDX document
			SPDXNonStandardLicense[] childNonStandardLicInfo = mergeDocs[i].getExtractedLicenseInfos();
			//an HashMap to track the changing of license ID from current child SPDX document
			HashMap<String, String> idMap = new HashMap<String, String>();
			
	        for(int k = 0; k < licInfoResult.size(); k++){
	        	boolean foundTextMatch = false;
	        	for(int p = 0; p < childNonStandardLicInfo.length; p++){
	        		if(LicenseCompareHelper.isLicenseTextEquivalent(licInfoResult.get(k).getText(), childNonStandardLicInfo[p].getText())){
	        			foundTextMatch = true;
	           		}
	        		if(!foundTextMatch){
	        			String oldLicId = childNonStandardLicInfo[p].getId();
	        			licId +=1;
	        			String newLicId = "LicenseRef-" + licId;	       
	        			SPDXNonStandardLicense clonedLicInfo = childNonStandardLicInfo[p].clone();//need work on the clone object
	        			//over-write the license ID
	        			clonedLicInfo.setId(newLicId);
	        			idMap.putIfAbsent(oldLicId, newLicId);
	        			licInfoResult.add(clonedLicInfo);
	        			newLicId = null;
	        			oldLicId = null;
	        		}
	        	}	      
	        }
	        licIdMap.put(mergeDocs[i], idMap);			
		}
		return licInfoResult;
	}
}

@@ -984,16 +984,79 @@
 rger %7B%0A%0A
+%09private int licId = 0;//an index number to track license id%0A%09%0A
 %09public 
@@ -1544,64 +1544,8 @@
 ();%0A
-%09%09//an index number to track license id%0A%09%09int licId = 0;
 %0A%09%09/
@@ -2617,83 +2617,53 @@
  %09%09%09
-String oldLicId = childNonStandardLicInfo%5Bp%5D.getId();%0A%09        %09%09%09licId +=1
+licId +=1;%0A%09        %09%09%09String orgLicId = null
 ;%0A%09 
@@ -2694,38 +2694,13 @@
 d = 
-%22LicenseRef-%22 + licId;%09       
+null;
 %0A%09  
@@ -2747,16 +2747,51 @@
 cInfo = 
+null;%0A%09        %09%09%09cloneLicenseInfo(
 childNon
@@ -2812,48 +2812,42 @@
 o%5Bp%5D
-.
+,
 clone
-();//need work on the clone object
+dLicInfo,orgLicId,newLicId);
 %0A%09  
@@ -2887,51 +2887,8 @@
  ID%0A
-%09        %09%09%09clonedLicInfo.setId(newLicId);%0A
 %09   
@@ -2914,18 +2914,18 @@
 Absent(o
-ld
+rg
 LicId, n
@@ -2984,66 +2984,8 @@
 o);%0A
-%09        %09%09%09newLicId = null;%0A%09        %09%09%09oldLicId = null;%0A
 %09   
@@ -3101,10 +3101,667 @@
 ult;%0A%09%7D%0A
+%09public void cloneLicenseInfo(SPDXNonStandardLicense childLicInfo, SPDXNonStandardLicense clonedLicInfo,String orgLicId, String newLicId)%7B%0A%09%09%09orgLicId = childLicInfo.getId();%0A%09%09%09String childLicText = childLicInfo.getText();%0A%09%09%09String childLicName = childLicInfo.getLicenseName();%0A%09%09%09String%5B%5D childLicSourceUrls = childLicInfo.getSourceUrls();%0A%09%09%09String childLicComment = childLicInfo.getComment();%0A%09%09%09newLicId = %22LicenseRef-%22 + licId;%0A%09%09%09clonedLicInfo.setId(newLicId);%0A%09%09%09clonedLicInfo.setText(childLicText);%0A%09%09%09clonedLicInfo.setLicenseName(childLicName);%0A%09%09%09clonedLicInfo.setSourceUrls(childLicSourceUrls);%0A%09%09%09clonedLicInfo.setComment(childLicComment);%0A%09%7D%0A
 %7D%0A

Add clone method. Not sure if it avoids the shallow copy problem.