package com.google.cloudsearch.ai;

import com.google.common.collect.Multimap;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import java.io.FileReader;
import java.util.List;
import org.apache.log4j.Logger;

/**
 * AI Skill Driver manages the creation and execution of skills.
 */
public class AISkillDriver {

    private static AISkillSet skillSet;
    private static Logger log = Logger.getLogger(AISkillDriver.class.getName());

    /**
     * Initialize the Driver
     * @param aiConfigName  File path of the Configuration
     * @param schemaName    File path of CloudSearch Schema
     * @throws Exception    Throws Exception if skill setup fails
     */
    public static void initialize(String aiConfigName, String schemaName) throws Exception {
        JSONParser parser = new JSONParser();
        JSONObject schema = (JSONObject) parser.parse(new FileReader(schemaName));
        JSONObject aiConfig = (JSONObject) parser.parse(new FileReader(aiConfigName));
        skillSet = new AISkillSet(aiConfig, schema);
        for(AISkill skill : skillSet.getSkills()) {
            skill.setupSkill();
        }
    }

    /**
     * Populates the structured Data by executing the skills.
     * @param structuredData    Multimap for storing the structured data.
     * @param contentOrURI      CloudStorage URI or File content in String format.
     */
    public static void populateStructuredData(String contentOrURI, Multimap<String, Object> structuredData) {

        List<AISkill> skillList = (List<AISkill>) skillSet.getSkills();
        if(skillList == null) {
            log.info("No skills Specified. Initialize the driver before calling populateStructuredData.");
        }
        for(AISkill skill : skillList) {
            skill.executeSkill(contentOrURI, structuredData);
        }
    }

    /**
     * Handles skill shutdown.
     */
    public static void closeSkillDriver() throws NullPointerException {
        try {
            List<AISkill> skillList = (List<AISkill>) skillSet.getSkills();
            for(AISkill skill : skillList) {
                skill.shutdownSkill();
            }
        }
        catch (NullPointerException e) {
            throw new NullPointerException("AISkill List is not initialized. Call AISkillDriver.initialize() before calling closeSkillDriver()");
        }
    }
}

@@ -1496,17 +1496,16 @@
 Data) %7B%0A
-%0A
         
@@ -1620,12 +1620,13 @@
 log.
-info
+error
 (%22No
@@ -1720,48 +1720,64 @@
     
-%7D%0A        for(AISkill skill : skillList)
+    return;%0A        %7D%0A        skillList.forEach(skill -%3E
  %7B%0A 
@@ -1838,32 +1838,34 @@
 Data);%0A        %7D
+);
 %0A    %7D%0A%0A    /**%0A
@@ -1979,26 +1979,8 @@
 n %7B%0A
-        try %7B%0A    
     
@@ -2059,191 +2059,53 @@
     
-    for(AISkill skill : skillList) %7B%0A                skill.shutdownSkill();%0A            %7D%0A        %7D%0A        catch (NullPointerException e) %7B%0A            throw new NullPointerException
+if(skillList == null) %7B%0A            log.error
 (%22AI
@@ -2208,22 +2208,126 @@
 %22);%0A        
-%7D
+    return;%0A        %7D%0A        skillList.forEach(skill -%3E %7B%0A            skill.shutdownSkill();%0A        %7D);
 %0A    %7D%0A%7D%0A

Use Lambda Expressions