package org.yeastrc.proxl.import_xml_to_db.process_input;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.yeastrc.proxl.import_xml_to_db.objects.PerPeptideData;
import org.yeastrc.proxl_import.api.xml_dto.Modification;
import org.yeastrc.proxl_import.api.xml_dto.Modifications;
import org.yeastrc.proxl_import.api.xml_dto.Peptide;
import org.yeastrc.xlink.dao.PeptideDAO;
import org.yeastrc.xlink.dto.DynamicModDTO;
import org.yeastrc.xlink.dto.MatchedPeptideDTO;
import org.yeastrc.xlink.dto.PeptideDTO;

/**
 * 
 *
 */
public class GetPerPeptideData {

	private static final Logger log = Logger.getLogger(GetPerPeptideData.class);

	private GetPerPeptideData() { }
	public static GetPerPeptideData getInstance() { return new GetPerPeptideData(); }
	

	/**
	 * @param peptide
	 * @return
	 * @throws Exception 
	 */
	public PerPeptideData getPerPeptideData( Peptide peptide, int nrseqDatabaseId ) throws Exception {
		
		PerPeptideData perPeptideData = new PerPeptideData();
		
		//  Add the peptide sequence if not in DB, otherwise retrieve existing record.
		PeptideDTO peptideDTO = PeptideDAO.getInstance().getPeptideDTO( peptide.getSequence() );

		perPeptideData.setPeptideDTO( peptideDTO );
		
		MatchedPeptideDTO matchedPeptideDTO = new MatchedPeptideDTO();
		perPeptideData.setMatchedPeptideDTO( matchedPeptideDTO );
		matchedPeptideDTO.setPeptide_id( peptideDTO.getId() );
		
		
		Modifications modifications = peptide.getModifications();
		List<Modification> modificationList = modifications.getModification();
		

		List<DynamicModDTO> dynamicModDTOList_Peptide = new ArrayList<>();
		List<Integer> monolinkPositionList = new ArrayList<>();
		
		perPeptideData.setDynamicModDTOList_Peptide( dynamicModDTOList_Peptide );
		perPeptideData.setMonolinkPositionList( monolinkPositionList );
		
		for ( Modification modification : modificationList ) {
			
			int position = modification.getPosition().intValue();
			BigDecimal mass = modification.getMass();
			boolean monolink = modification.isIsMonolink();
			
			DynamicModDTO dynamicModDTO = new DynamicModDTO();
			dynamicModDTO.setPosition( position );
			dynamicModDTO.setMass( mass.doubleValue() );
			dynamicModDTOList_Peptide.add( dynamicModDTO );
			
			if ( monolink ) {
				
				monolinkPositionList.add( position );
			}
		}
		
		return perPeptideData;
	}
	
}

@@ -1459,147 +1459,8 @@
 ;%0A%09%09
-%0A%09%09%0A%09%09Modifications modifications = peptide.getModifications();%0A%09%09List%3CModification%3E modificationList = modifications.getModification();%0A%09%09
 %0A%0A%09%09
@@ -1584,18 +1584,16 @@
 st%3C%3E();%0A
-%09%09
 %0A%09%09perPe
@@ -1727,19 +1727,229 @@
 List );%0A
+%0A%09%09Modifications modifications = peptide.getModifications();%0A%09%09%0A%09%09if ( modifications != null ) %7B%0A%0A%09%09%09List%3CModification%3E modificationList = modifications.getModification();%0A%0A%09%09%09if ( modificationList != null ) %7B%0A%0A
 %09%09
-%0A
 %09%09for ( 
@@ -1997,20 +1997,19 @@
 ist ) %7B%0A
-%09
+%0A
 %09%09
-%0A
 %09%09%09int p
@@ -2060,16 +2060,18 @@
 e();%0A%09%09%09
+%09%09
 BigDecim
@@ -2107,16 +2107,18 @@
 s();%0A%09%09%09
+%09
+%09
 boolean 
@@ -2157,20 +2157,19 @@
 link();%0A
-%09
+%0A
 %09%09
-%0A
 %09%09%09Dynam
@@ -2213,24 +2213,26 @@
 odDTO();%0A%09%09%09
+%09%09
 dynamicModDT
@@ -2254,24 +2254,26 @@
 position );%0A
+%09%09
 %09%09%09dynamicMo
@@ -2311,16 +2311,18 @@
 ) );%0A%09%09%09
+%09%09
 dynamicM
@@ -2361,20 +2361,19 @@
 dDTO );%0A
-%09
+%0A
 %09%09
-%0A
 %09%09%09if ( 
@@ -2389,13 +2389,11 @@
 ) %7B%0A
-%09%09
+%0A
 %09%09
-%0A
 %09%09%09%09
@@ -2430,16 +2430,29 @@
 tion );%0A
+%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Handle null pointers in incoming ProxlInput