V2_0.java [src/java/d/crlmod/cropImport] Revision: default  Date:
/*
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2024, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package d.crlmod.cropImport;

import crlmod.ServiceResources;
import static crlmod.ServiceResources.*;
import csip.ModelDataService;
import csip.annotations.*;
import csip.api.server.ServiceException;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Path;
import org.apache.commons.io.FileUtils;

/**
 *
 */
@Name("crop Import")
@Description("Crop import service for the CRLMOD_2018 Database")
@VersionInfo("2.0")
@Path("d/cropImport/2.0")
@Resource(from = ServiceResources.class)
public class V2_0 extends ModelDataService {

  private int rowsAffected = -1;

  @Override
  protected void doProcess() throws Exception {
    String sourceModel = parameter().getString("source_model");
    if (sourceModel.equalsIgnoreCase("weps")) {
      importWeps();
    } else if (sourceModel.equalsIgnoreCase("wepp")) {
      importWepp();
    } else {
      throw new ServiceException("required input missing 'source_model' must be included in your json.");
    }
  }

  @Override
  protected void postProcess() throws Exception {
    results().put("rowsAffected", rowsAffected);
  }

  private void importWeps() throws Exception {
    File[] unZippedFiles = null;
    File unzipDir = workspace().getFile("WEPSCrops");
    if (unzipDir.canRead()) {
      unZippedFiles = unzipDir.listFiles();
    }
    //Read into weps_op object
    List<Cr> wepsCrops = getCropsFromFiles(unZippedFiles);

    //TODO:  Use prepared statement here which will auto-filter/adjust the Strings being inserted.
    String query = "INSERT INTO weps_crops (weps_crop_path, weps_crop_name, weps_crop_param_set)\n"
        + "VALUES (?,?,?);";

    try (Connection connection = resources().getJDBC(getJDBCId())) {
      try (PreparedStatement statement = connection.prepareStatement(query)) {
        for (int i = 0; i < wepsCrops.size(); i++) {
          statement.setString(1, wepsCrops.get(i).crop_path);
          statement.setString(2, wepsCrops.get(i).crop_name);
          statement.setString(3, wepsCrops.get(i).crop_param_set);
          statement.addBatch();
        }
        connection.setAutoCommit(false);
        try {
          int[] rowResults = statement.executeBatch();
          for (int i = 0; i < rowResults.length; i++) {
            if (rowResults[i] < 0) {
              connection.rollback();
              throw new SQLException("Not all WEPS crop insert operations succeeded.");
            }
          }
          connection.commit();
          rowsAffected = rowResults.length;
        } catch (SQLException ex) {
          connection.rollback();
          throw ex;
        }
      }
    }
  }

  private void importWepp() throws Exception {
    File[] unZippedFiles = null;
    File unzipDir = workspace().getFile("WEPPCrops");
    if (unzipDir.canRead()) {
      unZippedFiles = unzipDir.listFiles();
    }
    //Read into wepp_op object
    List<Cr> weppCrops = getCropsFromFiles(unZippedFiles);

    String query = "INSERT INTO wepp_crops (wepp_crop_path, wepp_crop_name, wepp_crop_param_set) "
        + "VALUES (?,?,?)";

    try (Connection connection = resources().getJDBC(getJDBCId())) {
      try (PreparedStatement statement = connection.prepareStatement(query)) {
        for (int i = 0; i < weppCrops.size(); i++) {
          statement.setString(1, weppCrops.get(i).crop_path);
          statement.setString(2, weppCrops.get(i).crop_name);
          statement.setString(3, weppCrops.get(i).crop_param_set);
          statement.addBatch();
        }
        connection.setAutoCommit(false);
        try {
          int[] rowResults = statement.executeBatch();
          for (int i = 0; i < rowResults.length; i++) {
            if (rowResults[i] < 0) {
              connection.rollback();
              throw new SQLException("Not all WEPP crop insert operations succeeded.");
            }
          }
          connection.commit();
          rowsAffected = rowResults.length;
        } catch (SQLException ex) {
          connection.rollback();
          throw ex;
        }
      }
    }
  }

  public List<Cr> getCropsFromFiles(File[] cropFiles) throws IOException {
    String path = "";
    List<Cr> crops = new ArrayList<>();
    if (null != cropFiles && cropFiles.length > 0) {
      path = cropFiles[0].getParent().replace(workspace().getDir().getPath(), "");
    }

    //Get the path...so complicated.
    if (path.indexOf(File.separator, 2) != -1) {
      path = path.substring(path.indexOf(File.separator, 2) + 1);
    } else {
      path = "";
    }

    for (int i = 0; i < cropFiles.length; ++i) {
      if (cropFiles[i].isDirectory() && !cropFiles[i].getName().startsWith(".")) {
        crops.addAll(getCropsFromFiles(cropFiles[i].listFiles()));
      } else if (!cropFiles[i].isDirectory()) {
        Cr crop = new Cr();
        crop.crop_path = path;
        crop.crop_name = cropFiles[i].getName().substring(0, cropFiles[i].getName().lastIndexOf(".crop"));
        crop.crop_param_set = FileUtils.readFileToString(cropFiles[i], "UTF-8");
        System.out.println("Processed: [" + path + File.separator + crop.crop_name + "]");
        crops.add(crop);
      }
    }
    return crops;
  }

  protected String getJDBCId() {
    return CR_LMOD_2018_ID;
  }

  @Override
  protected Map<String, Object> getConfigInfo() {
    return new LinkedHashMap<String, Object>() {
      {
        put(getJDBCId(), resources().getResolved(getJDBCId()));
      }
    };
  }

}