/**
 * Copyright (C) 2004 - 2012 Shopzilla, Inc. 
 * All rights reserved. Unauthorized disclosure or distribution is prohibited.
 */
package com.shopzilla.api.client.model;

import java.util.List;

import org.apache.commons.lang.BooleanUtils;

import com.shopzilla.api.client.model.response.Classification;
import com.shopzilla.services.catalog.ClassificationResponse;
import com.shopzilla.services.catalog.QuerySuggestionType;

/**
 * Description of what the class is about
 * 
 * @author Tom Suthanurak
 * 
 * @since Jan 3, 2012
 */
public class ClassificationModelAdapter {
    public static Classification fromCatalogAPI(ClassificationResponse response) {
        final Classification output = new Classification();
        
        if (response.getClassification() != null) {
            output.setOriginalKeyword(response.getClassification().getOriginalKeyword());
            output.setMature(BooleanUtils.toBooleanObject(response.getClassification().getMature()));
            
            setSuggestions(output, response.getClassification().getQuerySuggestions().getQuerySuggestion());
            
            response.getClassification().getQuerySuggestions();
            
        }
        
        return output;
    }
    
    private static void setSuggestions(Classification classification, List<QuerySuggestionType> querySuggestions) {
        if (querySuggestions != null) {
            for (QuerySuggestionType querySuggestion : querySuggestions) {
                Suggestion suggestion = new Suggestion();
                suggestion.setSuggestionType(Suggestion.SuggestionType.valueOf(querySuggestion.getId()));
                suggestion.setKeyword(querySuggestion.getKeyword());
                
                if (querySuggestion.getOffer() != null) {
                    CatalogResponseModelAdapter.convertOffer(querySuggestion.getOffer());
                }
                if (querySuggestion.getProduct() != null) {
                    CatalogResponseModelAdapter.convertProduct(querySuggestion.getProduct());
                }
                
            }
        }
    }
}

@@ -235,16 +235,101 @@
 anUtils;
+%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;
 %0A%0Aimport
@@ -518,46 +518,72 @@
 %0A * 
-Description of what the class is about
+Convert service classification response into API response model.
 %0A * 
@@ -681,16 +681,114 @@
 apter %7B%0A
+    %0A    private static final Log LOG = LogFactory.getLog(ClassificationModelAdapter.class);%0A    %0A
     publ
@@ -1588,32 +1588,45 @@
 ions != null) %7B%0A
+            %0A
             for 
@@ -1676,32 +1676,49 @@
 ySuggestions) %7B%0A
+                %0A
                 
@@ -1751,32 +1751,58 @@
 w Suggestion();%0A
+                try %7B%0A    
                 
@@ -1887,24 +1887,238 @@
 .getId()));%0A
+                %7D catch (IllegalArgumentException e) %7B%0A                    LOG.error(%22Unable to convert query suggestion type to a known enum, type:%22+querySuggestion.getId(), e);%0A                %7D%0A                %0A
             
@@ -2261,32 +2261,52 @@
                 
+suggestion.setOffer(
 CatalogResponseM
@@ -2357,16 +2357,17 @@
 Offer())
+)
 ;%0A      
@@ -2458,16 +2458,38 @@
         
+suggestion.setProduct(
 CatalogR
@@ -2552,16 +2552,17 @@
 oduct())
+)
 ;%0A      
@@ -2581,32 +2581,424 @@
                 
+if (querySuggestion.getCategory() != null) %7B%0A                    Category category = new Category();%0A                    category.setId(querySuggestion.getCategory().getId());%0A                    category.setName(querySuggestion.getCategory().getName());%0A                    suggestion.setCategory(category);%0A                %7D%0A%0A                classification.getSuggestions().add(suggestion);
 %0A            %7D%0A 

set suggestion result in classification model.