Cronjob to process create a new csv file and create a parsed data based on product information
/**
*
*/
package com.test.ac.catalog.job;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.test.ac.catalog.ProductRecordData;
import com.test.ac.catalog.ResultData;
import com.test.ac.model.TestVariantProductModel;
import com.test.ac.model.ProcessDynamicDataForSalsifyCronJobModel;
import com.test.ac.servicelayer.product.TestSalsifyProductExportService;
import com.test.ac.util.SalsifySFTPHelper;
import de.hybris.platform.catalog.CatalogVersionService;
import de.hybris.platform.catalog.jalo.CatalogManager;
import de.hybris.platform.catalog.model.CatalogModel;
import de.hybris.platform.catalog.model.CatalogUnawareMediaModel;
import de.hybris.platform.catalog.model.CatalogVersionModel;
import de.hybris.platform.core.GenericSearchConstants.LOG;
import de.hybris.platform.core.model.media.MediaModel;
import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.cronjob.enums.CronJobResult;
import de.hybris.platform.cronjob.enums.CronJobStatus;
import de.hybris.platform.servicelayer.cronjob.AbstractJobPerformable;
import de.hybris.platform.servicelayer.cronjob.PerformResult;
import de.hybris.platform.servicelayer.exceptions.UnknownIdentifierException;
import de.hybris.platform.servicelayer.media.MediaService;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.util.Config;
import static org.apache.commons.lang3.StringEscapeUtils.escapeCsv;
/**
* @author svintha
*
* Process Dynamic Data For Salsify Cron Job is to process file data with products and variants for Salsify.
* This job will connect to a defined SFTP server, download csv files from a defined directory
* and process those files, get the info for products/variants and generate a CSV file for Salsify.
* Then the generated CSV files which contains products information are placed in Salsify sftp server location.
*
* The catalog version, language isocode and Sales Org are used to determine which product information is retrieved and processed to salsify.
*/
public class TestProcessDynamicDataForSalsifyJobPerformable
extends AbstractJobPerformable<ProcessDynamicDataForSalsifyCronJobModel> {
private static final Logger LOG = Logger.getLogger(TestProcessDynamicDataForSalsifyJobPerformable.class.getName());
private static final String UNDER_SCORE = "_";
private static final String SALSIFY_CSV_FILE_CODE = "salsifyCSVFile";
private static final String LINE_SEPARATOR = ",";
private static final char COLUMN_SEPARATOR = ',';
private static final String LINE_BREAK = "\r\n";
private static final String CSV_FILE_EXT = ".csv";
private String inputCatalogId = StringUtils.EMPTY;
private Locale inputLocale = null;
private String inputSalesOrg = StringUtils.EMPTY;
private String inputCatalogVersion = CatalogManager.OFFLINE_VERSION;// "Staged"
private String inputFileName = StringUtils.EMPTY;
private String outPutFileName = StringUtils.EMPTY;
@Resource(name = "catalogVersionService")
private CatalogVersionService catalogVersionService;
@Resource(name = "modelService")
private ModelService modelService;
@Resource(name = "salsifySFTPHelper")
private SalsifySFTPHelper salsifySFTPHelper;
@Resource(name = "testSalsifyProductExportService")
TestSalsifyProductExportService testSalsifyProductExportService;
@Autowired
MediaService mediaService;
@Override
public boolean isAbortable() {
return true;
}
@Override
public PerformResult perform(ProcessDynamicDataForSalsifyCronJobModel cronJobModel) {
boolean success = true;
List<String> filesToProcess = new ArrayList<String>();
LOG.info(" -- Updating the input parameter values according to the cronjob parameters -- ");
//update the input parameters values such as catalog version,locale,sales org,inputfilename and outputfilename
loadCronjobParameters(cronJobModel);
// List of files that are going to be processed for this run of the job.
filesToProcess = salsifySFTPHelper.getListOfFilesOverSSH(inputFileName, false, inputFileName);
if (filesToProcess.isEmpty()) {
LOG.error("No matching files to process at this time.");
LOG.info("No matching files to process at this time.");
return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
}
if (!filesToProcess.isEmpty()) {
//Defines catalog that is used.
final CatalogVersionModel defaultCatalogVersionModel = catalogVersionService.getCatalogVersion(inputCatalogId, inputCatalogVersion);
//Return list of files that were processed with record counts.
final List<String> files = new ArrayList<>();
final StringBuilder errorRecords = new StringBuilder();
final StringBuilder errorMetrics = new StringBuilder();
Map<String, Integer> errorMap = new HashMap<>();
//Loops the files and processes them one at a time.
for (String fileName : filesToProcess) {
if (clearAbortRequestedIfNeeded(cronJobModel)) {
return new PerformResult(CronJobResult.ERROR, CronJobStatus.ABORTED);
}
success = processFile(fileName, defaultCatalogVersionModel, files, errorRecords, errorMap);
}
for (Entry<String, Integer> errorEntry : errorMap.entrySet()) {
errorMetrics.append(errorEntry.getKey() + " : caused " + errorEntry.getValue() + " records to fail.<br/>");
}
cronJobModel.setFilesProcessed(files);
cronJobModel.setErrors(errorRecords.toString());
cronJobModel.setErrorMetrics(errorMetrics.toString());
modelService.save(cronJobModel);
}
if (success) {
return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
} else {
return new PerformResult(CronJobResult.ERROR, CronJobStatus.FINISHED);
}
}
/**
* @param cronJobModel
* <br>
* This method will load the input values according to the cronjob parameters(catalog version,language iso code,Sales Org,inputfilename and outputfilename)
*/
private void loadCronjobParameters(ProcessDynamicDataForSalsifyCronJobModel cronJobModel) {
CatalogModel srcProductCatalog = cronJobModel.getSrcProductCatalogVersion() != null ? cronJobModel.getSrcProductCatalogVersion().getCatalog() : null;
LOG.info(" -- srcProductCatalog -- : " + srcProductCatalog);
if (srcProductCatalog != null) {
inputCatalogId = srcProductCatalog.getId(); // Override the input Catalog ID to the cronjob instance src product catalog ID.
inputCatalogVersion = cronJobModel.getSrcProductCatalogVersion().getVersion();// Override the input Catalog Version to the cronjob instance src product catalog version.
}
LOG.info(" -- inputCatalogId -- : " + inputCatalogId+":"+inputCatalogVersion);
if (StringUtils.isNotBlank(cronJobModel.getLanguageIsoCode())) {
inputLocale = new Locale(cronJobModel.getLanguageIsoCode()); // Override the input LOCALE according to the cronjob instance LanguageIsoCode
}
LOG.info(" -- inputLocale -- : " + inputLocale);
if (StringUtils.isNotBlank(cronJobModel.getSalesOrg())) {
inputSalesOrg = cronJobModel.getSalesOrg(); // Override the input Sales Org according to the cronjob instance SalesOrg
}
LOG.info(" -- inputSalesOrg -- : " + inputSalesOrg);
if (StringUtils.isNotBlank(cronJobModel.getInputFileName())) {
inputFileName = cronJobModel.getInputFileName(); // Override the Input file name according to the cronjob instance inputFileName
}
LOG.info(" -- inputFileName -- : " + inputFileName);
if (StringUtils.isNotBlank(cronJobModel.getOutPutFileName())) {
outPutFileName = cronJobModel.getOutPutFileName(); // Override the Output file name according to the cronjob instance outPutFileName
}
LOG.info(" -- outPutFileName -- : " + outPutFileName);
}
/**
* Processes a single file that was downloaded from the ftp server.
*
* @param fileName
* @param catalogVersionModel
* @param files
* @param errorRecords
* @param errorMap map to count how many times each error is encountered in a file.
* @return
*/
private boolean processFile(final String fileName, final CatalogVersionModel catalogVersionModel, final List<String> files,
final StringBuilder errorRecords, Map<String, Integer> errorMap) {
boolean success = true;
Scanner fileScanner = null;
InputStream inputStream = null;
try {
int recordCount = 0;
int recordsFailed = 0;
int recordsPassed = 0;
String line;
LOG.info("Processing File: " + fileName);
errorRecords.append("<br/>File: ").append(fileName).append("<br/>");
fileScanner = new Scanner(new FileInputStream(fileName));
//create salsify csv file
LOG.info(" Start processing dynamic product data for Salsify... ");
List<ProductRecordData> variantProductRecords = new ArrayList<>();
List<ProductRecordData> baseProductRecords = new ArrayList<>();
List<String> productCodes = new ArrayList<>();
StringBuilder salsifyFileContent = new StringBuilder();
addHeader(salsifyFileContent);
while (fileScanner.hasNextLine()) {
line = fileScanner.nextLine();
ResultData lineResult = processLine(line, catalogVersionModel, productCodes, variantProductRecords, baseProductRecords);
boolean lineSuccess = lineResult.isSuccess();
if (!lineSuccess) {
recordsFailed = processRecordErrors(fileName, errorRecords, errorMap, recordsFailed, line, lineResult);
}
else {
recordsPassed++;
}
recordCount++;
}
addProductRecords(salsifyFileContent, baseProductRecords);
addProductRecords(salsifyFileContent, variantProductRecords);
populateSalsifyFile(salsifyFileContent);
// salsifySFTPHelper.sendFilesOverSSH(outPutFileName, outPutFileName + CSV_FILE_EXT);
files.add(fileName + "=" + recordCount + "=" + recordsFailed + "=" + recordsPassed);
LOG.info(" Dynamic Product data Salsify export job completed ");
} catch (FileNotFoundException fnfe) {
LOG.error("Error processing file " + fileName, fnfe);
success = false;
} finally {
if (null != fileScanner) {
fileScanner.close();
//Clean up file from filesystem
File file = null;
try {
file = new File(fileName);
file.delete();
} catch (RuntimeException re) {
LOG.error("Error deleting " + fileName + " from the filesystem.", re);
}
}
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
LOG.error("Error occurred writing text file", e);
}
}
return success;
}
/**
* This method will process a single line from the file. Processing a line means looking up the
* product code if the product exists getting the data from it.
*
* @param inputLine The current line from the file to process.
* @param catalogVersionModel The current catalog version which is the catalog and the mode.
*/
private ResultData processLine(final String inputLine, final CatalogVersionModel catalogVersionModel, List<String> processedProductCodes, List<ProductRecordData> variantProductRecords, List<ProductRecordData> baseProductRecords) {
ResultData resultData = new ResultData();
resultData.setSuccess(true);
try {
//Split up the line into the defined fields.
final String[] lineParts = inputLine.split(LINE_SEPARATOR, -1);
//Ran into a case where the first line was not processed because there was a phantom blank character at the beginning of the file
//which wasn't a space or a tab...
lineParts[0] = StringUtils.trim(lineParts[0]);
final String variantCode = lineParts[0];
if (!isProductCodeProcessed(processedProductCodes, variantCode)) {
//See if there is a match on the variant.
LOG.debug(" Searching for Varint "+inputSalesOrg+"_"+variantCode+" in "+catalogVersionModel.getCatalog().getId()+":"+catalogVersionModel.getCatalog().getVersion());
TESTVariantProductModel variantModel = testSalsifyProductExportService.getTestVariantProductModel(catalogVersionModel, variantCode , inputSalesOrg);
if (null != variantModel) {
processRecordData(processedProductCodes, catalogVersionModel, variantModel, variantProductRecords, baseProductRecords);
processedProductCodes.add(variantCode);
}
}
} catch (RuntimeException re) {
re.printStackTrace();
String errorMsg = "Error processing line:\n" + inputLine + re;
LOG.error(errorMsg);
resultData.setMessage(errorMsg);
resultData.setSuccess(false);
return resultData;
}
return resultData;
}
private void processRecordData(List<String> processedProductCodes, final CatalogVersionModel catalogVersionModel, TestVariantProductModel variantModel, List<ProductRecordData> variantProductRecords, List<ProductRecordData> baseProductRecords) {
String parentProductCode = variantModel.getBaseProduct().getCode();
ProductModel baseProductModel = TestSalsifyProductExportService.getBaseProductModelForCode(catalogVersionModel, parentProductCode);
String baseProductDescription = escapeCsv(TestSalsifyProductExportService.getBaseProductDescription(baseProductModel,inputLocale));
ProductRecordData variantProductRecordData = new ProductRecordData();
processVariantProductData(variantModel, parentProductCode, baseProductDescription, variantProductRecordData);
processCommonData(variantModel, variantProductRecordData);
variantProductRecords.add(variantProductRecordData);
if (!isProductCodeProcessed(processedProductCodes, parentProductCode)){
ProductRecordData baseProductRecordData = processBaseProductData(parentProductCode, baseProductModel, baseProductDescription);
baseProductRecords.add(baseProductRecordData);
processedProductCodes.add(parentProductCode);
}
}
private ProductRecordData processBaseProductData(String parentProductCode, ProductModel baseProductModel, String baseProductDescription) {
ProductRecordData baseProductRecordData = new ProductRecordData();
baseProductRecordData.setProductId(parentProductCode);
baseProductRecordData.setParentProductDescription(baseProductDescription);
processCommonData(baseProductModel, baseProductRecordData);
return baseProductRecordData;
}
private void processCommonData(ProductModel productModel, ProductRecordData recordData) {
recordData.setProductName(escapeCsv(TestSalsifyProductExportService.getProductName(productModel,inputLocale)));
recordData.setProductPageTitle(escapeCsv(TestSalsifyProductExportService.getProductPageTitle(productModel,inputLocale)));
recordData.setProductMetaDescription(escapeCsv(TestSalsifyProductExportService.getProductMetaDescription(productModel,inputLocale)));
recordData.setProductMetaKeyWords(escapeCsv(TestSalsifyProductExportService.getProductMetaKeywords(productModel,inputLocale)));
recordData.setProductMarkets(escapeCsv(TestSalsifyProductExportService.getProductMarkets(productModel,inputLocale)));
recordData.setProductApplications(escapeCsv(TestSalsifyProductExportService.getProductApplications(productModel,inputLocale)));
recordData.setProductHeadline(escapeCsv(TestSalsifyProductExportService.getProductHeadline(productModel,inputLocale)));
recordData.setProductSpecificationsSection(escapeCsv(TestSalsifyProductExportService.getProductSpecificationsSection(productModel,inputLocale)));
recordData.setProductFeaturesAndBenefits(escapeCsv(TestSalsifyProductExportService.getProductFeaturesAndBenefitsSection(productModel,inputLocale)));
recordData.setProductApprovalsCertifications(escapeCsv(TestSalsifyProductExportService.getProductApprovalsCertifications(productModel,inputLocale)));
recordData.setProductManualsSection(escapeCsv(TestSalsifyProductExportService.getProductManualsSection(productModel,inputLocale)));
}
private void processVariantProductData(TestVariantProductModel variantModel, String parentProductCode, String baseProductDescription, ProductRecordData variantProductRecordData) {
variantProductRecordData.setProductId(variantModel.getPartNumber());
variantProductRecordData.setParentProductCode(parentProductCode);
variantProductRecordData.setParentProductDescription(baseProductDescription);
variantProductRecordData.setVariantProductDescription(escapeCsv(TestSalsifyProductExportService.getVariantProductDescription(variantModel,inputLocale)));
}
private boolean isProductCodeProcessed(List<String> processedProductCodeList, String code) {
return (!processedProductCodeList.isEmpty() && processedProductCodeList.contains(code));
}
/**
* @return
* This header of the file is built according to the cronjob Locale parameter
*/
private List<String> getFileHeader() {
return Arrays.asList("code", "parent_code", "name"+UNDER_SCORE+inputLocale, "parent_description"+UNDER_SCORE+inputLocale, "variant_description"+UNDER_SCORE+inputLocale, "pageTitle"+UNDER_SCORE+inputLocale, "metaDescription"+UNDER_SCORE+inputLocale, "metaKeywords"+UNDER_SCORE+inputLocale, "@Markets", "@Applications", "headline"+UNDER_SCORE+inputLocale, "specificationsSection"+UNDER_SCORE+inputLocale, "featuresAndBenefitsSection"+UNDER_SCORE+inputLocale, "approvalsCertifications"+UNDER_SCORE+inputLocale, "manualsSection"+UNDER_SCORE+inputLocale);
}
private void addHeader(StringBuilder salsifyFileContent) {
List<String> headerLine = getFileHeader();
salsifyFileContent.append(StringUtils.join(headerLine, COLUMN_SEPARATOR));
salsifyFileContent.append(LINE_BREAK);
}
private void addProductRecords(StringBuilder salsifyFileContent, List<ProductRecordData> recordList) {
for (ProductRecordData record : recordList) {
salsifyFileContent.append(StringUtils.join(record.asRecord(), COLUMN_SEPARATOR));
salsifyFileContent.append(LINE_BREAK);
}
}
private int processRecordErrors(String fileName, StringBuilder errorRecords, Map<String, Integer> errorMap, int recordsFailed, String line, ResultData lineResult) {
errorRecords.append(line).append("<br/>");
errorRecords.append("<font color=\"red\">" + lineResult.getMessage() + "</font><br/>");
if (null != errorMap.get(lineResult.getMessage())) {
int errorCount = errorMap.get(lineResult.getMessage()) + 1;
errorMap.put(lineResult.getMessage(), errorCount);
} else {
int errorCount = 1;
errorMap.put(lineResult.getMessage(), errorCount);
}
//If there was an error processing a line, log it and continue.
LOG.error("Error found processing file: " + fileName + "\n" + line);
recordsFailed++;
LOG.error("Continuing processing...");
return recordsFailed;
}
private void populateSalsifyFile(StringBuilder salsifyFileContent) {
try {
MediaModel existingMedia = mediaService.getMedia(outPutFileName);
if (existingMedia != null) {
modelService.remove(existingMedia);
// create new media file with new data
createMediaFile();
}
} catch (UnknownIdentifierException ex) {
// if media item does not exist, create one
createMediaFile();
}
MediaModel salsifyFile = mediaService.getMedia(outPutFileName);
mediaService.setDataForMedia(salsifyFile, salsifyFileContent.toString().getBytes(Charset.forName("UTF-8")));
}
private void createMediaFile() {
CatalogUnawareMediaModel media = modelService.create(CatalogUnawareMediaModel.class);
media.setCode(outPutFileName);
media.setRealFileName(media.getCode() + CSV_FILE_EXT);
media.setMime("text/csv");
modelService.save(media);
}
}
package com.Test.ac.servicelayer.product.impl;
import com.Test.ac.classification.TestClassificationService;
import com.Test.ac.model.TestVariantProductModel;
import com.Test.ac.servicelayer.product.TestSalsifyProductExportService;
import de.hybris.platform.catalog.model.CatalogVersionModel;
import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.product.ProductService;
import de.hybris.platform.servicelayer.exceptions.UnknownIdentifierException;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import javax.annotation.Resource;
import java.util.Locale;
public class TestSalsifyProductExportServiceImpl implements TestSalsifyProductExportService {
private static final Logger LOG = Logger.getLogger(TestSalsifyProductExportServiceImpl.class.getName());
private static final Locale US_LOCALE = new Locale("en");
private static final String MARKETS_CODE = "TestClassification/1.0/products.markets";
private static final String APPLICATIONS_CODE = "TestClassification/1.0/products.applications";
@Resource(name = "productService")
private ProductService productService;
@Resource(name = "TestClassificationService")
private TestClassificationService TestClassificationService;
public ProductModel getBaseProductModelForCode(CatalogVersionModel catalogVersionModel, String parentProductCode) {
ProductModel productModel = null;
try {
productModel = productService.getProductForCode(catalogVersionModel, parentProductCode);
} catch (UnknownIdentifierException uie) {
LOG.error("Product " + parentProductCode + " not found");
}
return productModel;
}
public TestVariantProductModel getTestVariantProductModel(CatalogVersionModel catalogVersionModel, String variantCode) {
TestVariantProductModel variantModel = null;
try {
variantModel = (TestVariantProductModel) productService.getProductForCode(
catalogVersionModel, variantCode);
} catch (UnknownIdentifierException uie) {
LOG.error("Variant " + variantCode + " not found!");
}
return variantModel;
}
public TestVariantProductModel getTestVariantProductModel(CatalogVersionModel catalogVersionModel, String variantCode, String salesOrg) {
TestVariantProductModel variantModel = null;
try {
variantModel = (TestVariantProductModel) productService.getProductForCode(
catalogVersionModel, salesOrg+"_"+variantCode);
} catch (UnknownIdentifierException uie) {
LOG.error("Variant " + salesOrg + "_" + variantCode + " not found in catalog : "
+ catalogVersionModel.getCatalog().getId() + ":" + catalogVersionModel.getVersion());
}
return variantModel;
}
public String getProductManualsSection(ProductModel productModel) {
String variantManualsSection = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantManualsSection = TestVariantModel.getManualsSection(US_LOCALE);
return StringUtils.isBlank(variantManualsSection) ? TestVariantModel.getBaseProduct().getHeadline(US_LOCALE): variantManualsSection;
}
return StringUtils.isBlank(variantManualsSection) ? productModel.getHeadline(US_LOCALE): variantManualsSection;
}
public String getProductManualsSection(ProductModel productModel,Locale locale ) {
String variantManualsSection = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantManualsSection = TestVariantModel.getManualsSection(locale);
return StringUtils.isBlank(variantManualsSection) ? TestVariantModel.getBaseProduct().getHeadline(locale): variantManualsSection;
}
return StringUtils.isBlank(variantManualsSection) ? productModel.getHeadline(locale): variantManualsSection;
}
public String getProductApprovalsCertifications(ProductModel productModel) {
String variantApprovalCertifications = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantApprovalCertifications = TestVariantModel.getApprovalsCertifications(US_LOCALE);
return StringUtils.isBlank(variantApprovalCertifications) ? TestVariantModel.getBaseProduct().getApprovalsCertifications(US_LOCALE) : variantApprovalCertifications;
}
return StringUtils.isBlank(variantApprovalCertifications) ? productModel.getApprovalsCertifications(US_LOCALE) : variantApprovalCertifications;
}
public String getProductApprovalsCertifications(ProductModel productModel,Locale locale ) {
String variantApprovalCertifications = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantApprovalCertifications = TestVariantModel.getApprovalsCertifications(locale);
return StringUtils.isBlank(variantApprovalCertifications) ? TestVariantModel.getBaseProduct().getApprovalsCertifications(locale) : variantApprovalCertifications;
}
return StringUtils.isBlank(variantApprovalCertifications) ? productModel.getApprovalsCertifications(locale) : variantApprovalCertifications;
}
public String getProductFeaturesAndBenefitsSection(ProductModel productModel) {
String variantFeaturesAndBenefitsSection = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantFeaturesAndBenefitsSection = TestVariantModel.getFeaturesAndBenefitsSection(US_LOCALE);
return StringUtils.isBlank(variantFeaturesAndBenefitsSection) ? TestVariantModel.getBaseProduct().getFeaturesAndBenefitsSection(US_LOCALE) : variantFeaturesAndBenefitsSection;
}
return StringUtils.isBlank(variantFeaturesAndBenefitsSection) ? productModel.getFeaturesAndBenefitsSection(US_LOCALE) : variantFeaturesAndBenefitsSection;
}
public String getProductFeaturesAndBenefitsSection(ProductModel productModel,Locale locale ) {
String variantFeaturesAndBenefitsSection = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantFeaturesAndBenefitsSection = TestVariantModel.getFeaturesAndBenefitsSection(locale);
return StringUtils.isBlank(variantFeaturesAndBenefitsSection) ? TestVariantModel.getBaseProduct().getFeaturesAndBenefitsSection(locale) : variantFeaturesAndBenefitsSection;
}
return StringUtils.isBlank(variantFeaturesAndBenefitsSection) ? productModel.getFeaturesAndBenefitsSection(locale) : variantFeaturesAndBenefitsSection;
}
public String getProductSpecificationsSection(ProductModel productModel) {
String variantSpecificationsSection = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantSpecificationsSection = TestVariantModel.getSpecificationsSection(US_LOCALE);
return StringUtils.isBlank(variantSpecificationsSection) ? TestVariantModel.getBaseProduct().getSpecificationsSection(US_LOCALE) : variantSpecificationsSection;
}
return StringUtils.isBlank(variantSpecificationsSection) ? productModel.getSpecificationsSection(US_LOCALE) : variantSpecificationsSection;
}
public String getProductSpecificationsSection(ProductModel productModel,Locale locale ) {
String variantSpecificationsSection = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantSpecificationsSection = TestVariantModel.getSpecificationsSection(locale);
return StringUtils.isBlank(variantSpecificationsSection) ? TestVariantModel.getBaseProduct().getSpecificationsSection(locale) : variantSpecificationsSection;
}
return StringUtils.isBlank(variantSpecificationsSection) ? productModel.getSpecificationsSection(locale) : variantSpecificationsSection;
}
public String getBaseProductDescription(ProductModel productModel) {
return productModel.getDescription(US_LOCALE);
}
public String getBaseProductDescription(ProductModel productModel,Locale locale ) {
return productModel.getDescription(locale);
}
public String getProductHeadline(ProductModel productModel) {
String variantProductHeadline = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantProductHeadline = TestVariantModel.getHeadline(US_LOCALE);
return StringUtils.isBlank(variantProductHeadline) ? TestVariantModel.getBaseProduct().getHeadline(US_LOCALE) : variantProductHeadline;
}
return StringUtils.isBlank(variantProductHeadline) ? productModel.getHeadline(US_LOCALE) : variantProductHeadline;
}
public String getProductHeadline(ProductModel productModel,Locale locale ) {
String variantProductHeadline = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantProductHeadline = TestVariantModel.getHeadline(locale);
return StringUtils.isBlank(variantProductHeadline) ? TestVariantModel.getBaseProduct().getHeadline(locale) : variantProductHeadline;
}
return StringUtils.isBlank(variantProductHeadline) ? productModel.getHeadline(locale) : variantProductHeadline;
}
public String getProductApplications(ProductModel productModel) {
String variantApplications = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantApplications = TestClassificationService.getFeatureValueByCode(TestVariantModel, APPLICATIONS_CODE, US_LOCALE);
return StringUtils.isBlank(variantApplications) ? TestClassificationService.getFeatureValueByCode(TestVariantModel.getBaseProduct(), APPLICATIONS_CODE, US_LOCALE) : variantApplications;
}
return StringUtils.isBlank(variantApplications) ? TestClassificationService.getFeatureValueByCode(productModel, APPLICATIONS_CODE, US_LOCALE) : variantApplications;
}
public String getProductApplications(ProductModel productModel,Locale locale ) {
String variantApplications = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantApplications = TestClassificationService.getFeatureValueByCode(TestVariantModel, APPLICATIONS_CODE, locale);
return StringUtils.isBlank(variantApplications) ? TestClassificationService.getFeatureValueByCode(TestVariantModel.getBaseProduct(), APPLICATIONS_CODE, locale) : variantApplications;
}
return StringUtils.isBlank(variantApplications) ? TestClassificationService.getFeatureValueByCode(productModel, APPLICATIONS_CODE, locale) : variantApplications;
}
public String getProductMarkets(ProductModel productModel) {
String variantMarkets = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantMarkets = TestClassificationService.getFeatureValueByCode(TestVariantModel, MARKETS_CODE, US_LOCALE);
return StringUtils.isBlank(variantMarkets) ? TestClassificationService.getFeatureValueByCode(TestVariantModel.getBaseProduct(), MARKETS_CODE, US_LOCALE) : variantMarkets;
}
return StringUtils.isBlank(variantMarkets) ? TestClassificationService.getFeatureValueByCode(productModel, MARKETS_CODE, US_LOCALE) : variantMarkets;
}
public String getProductMarkets(ProductModel productModel,Locale locale ) {
String variantMarkets = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantMarkets = TestClassificationService.getFeatureValueByCode(TestVariantModel, MARKETS_CODE, locale);
return StringUtils.isBlank(variantMarkets) ? TestClassificationService.getFeatureValueByCode(TestVariantModel.getBaseProduct(), MARKETS_CODE, locale) : variantMarkets;
}
return StringUtils.isBlank(variantMarkets) ? TestClassificationService.getFeatureValueByCode(productModel, MARKETS_CODE, locale) : variantMarkets;
}
public String getProductMetaKeywords(ProductModel productModel) {
String variantMetaKeywords = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantMetaKeywords = TestVariantModel.getMetaKeywords(US_LOCALE);
return StringUtils.isBlank(variantMetaKeywords) ? TestVariantModel.getBaseProduct().getMetaKeywords(US_LOCALE) : variantMetaKeywords;
}
return StringUtils.isBlank(variantMetaKeywords) ? productModel.getMetaKeywords(US_LOCALE) : variantMetaKeywords;
}
public String getProductMetaKeywords(ProductModel productModel,Locale locale ) {
String variantMetaKeywords = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantMetaKeywords = TestVariantModel.getMetaKeywords(locale);
return StringUtils.isBlank(variantMetaKeywords) ? TestVariantModel.getBaseProduct().getMetaKeywords(locale) : variantMetaKeywords;
}
return StringUtils.isBlank(variantMetaKeywords) ? productModel.getMetaKeywords(locale) : variantMetaKeywords;
}
public String getProductMetaDescription(ProductModel productModel) {
String variantMetaDescription = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantMetaDescription = TestVariantModel.getMetaDescription(US_LOCALE);
return StringUtils.isBlank(variantMetaDescription) ? TestVariantModel.getBaseProduct().getMetaDescription(US_LOCALE) : variantMetaDescription;
}
return StringUtils.isBlank(variantMetaDescription) ? productModel.getMetaDescription(US_LOCALE) : variantMetaDescription;
}
public String getProductMetaDescription(ProductModel productModel,Locale locale ) {
String variantMetaDescription = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantMetaDescription = TestVariantModel.getMetaDescription(locale);
return StringUtils.isBlank(variantMetaDescription) ? TestVariantModel.getBaseProduct().getMetaDescription(locale) : variantMetaDescription;
}
return StringUtils.isBlank(variantMetaDescription) ? productModel.getMetaDescription(locale) : variantMetaDescription;
}
public String getProductPageTitle(ProductModel productModel) {
String variantPageTitle = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantPageTitle = TestVariantModel.getPageTitle(US_LOCALE);
return StringUtils.isBlank(variantPageTitle) ? TestVariantModel.getBaseProduct().getPageTitle(US_LOCALE) : variantPageTitle;
}
return StringUtils.isBlank(variantPageTitle) ? productModel.getPageTitle(US_LOCALE) : variantPageTitle;
}
public String getProductPageTitle(ProductModel productModel,Locale locale ) {
String variantPageTitle = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantPageTitle = TestVariantModel.getPageTitle(locale);
return StringUtils.isBlank(variantPageTitle) ? TestVariantModel.getBaseProduct().getPageTitle(locale) : variantPageTitle;
}
return StringUtils.isBlank(variantPageTitle) ? productModel.getPageTitle(locale) : variantPageTitle;
}
public String getVariantProductDescription(TestVariantProductModel variantModel) {
return variantModel.getDescription(US_LOCALE);
}
public String getVariantProductDescription(TestVariantProductModel variantModel,Locale locale ) {
return variantModel.getDescription(locale);
}
public String getProductName(ProductModel productModel) {
String variantProductName = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantProductName = TestVariantModel.getName(US_LOCALE);
return StringUtils.isBlank(variantProductName) ? TestVariantModel.getBaseProduct().getName(US_LOCALE) : variantProductName;
}
return StringUtils.isBlank(variantProductName) ? productModel.getName(US_LOCALE) : variantProductName;
}
public String getProductName(ProductModel productModel,Locale locale ) {
String variantProductName = StringUtils.EMPTY;
if (productModel instanceof TestVariantProductModel){
TestVariantProductModel TestVariantModel = (TestVariantProductModel) productModel;
variantProductName = TestVariantModel.getName(locale);
return StringUtils.isBlank(variantProductName) ? TestVariantModel.getBaseProduct().getName(locale) : variantProductName;
}
return StringUtils.isBlank(variantProductName) ? productModel.getName(locale) : variantProductName;
}
}
/**
*
*/
package com.safety.ac.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Vector;
import de.hybris.platform.core.model.media.MediaModel;
import de.hybris.platform.servicelayer.media.MediaService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.safety.ac.util.SFTPHelper.SFTPType;
import de.hybris.platform.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author
* <br>SalsifySFTPHelper</br>
* configuration and connectivity to and Salsify SFTP servers to
* process the files.
*/
public class SalsifySFTPHelper {
private static final Logger LOG = Logger.getLogger(SalsifySFTPHelper.class.getName());
private static final String BACKUP_EXT = ".bak";
private static final String JOB_ERROR_MESSAGE = "Unable to process any files.";
private JSch sftpClient;
private Session session;
private String strUserName = StringUtils.EMPTY;
private String strPassword = StringUtils.EMPTY;
private String strSftpHost = StringUtils.EMPTY;
private String strSftpUSourceDir = StringUtils.EMPTY;
private String strArchdir = StringUtils.EMPTY;
private int strSftpPort;
private ChannelSftp sftpChannel;
@Autowired
MediaService mediaService;
enum SFTPType {
SALSIFY;
}
public List<String> getListOfFilesOverSSH(String srcFileName, boolean archiveFileAfterProcessing, String cronjobInputFileName) {
final List<String> returnList = new ArrayList<>();
try {
sftpChannel = (ChannelSftp) openChannelConnection(srcFileName, SFTPType.);
sftpChannel.cd(strSftpUSourceDir);
Vector<ChannelSftp.LsEntry> list = sftpChannel.ls(".");
if (list.isEmpty()){
LOG.info("File list is empty: ");
} else LOG.info("Iterating through list of " + list.size() + " items.");
for (ChannelSftp.LsEntry remoteFile : list) {
String remoteFileName = remoteFile.getFilename();
LOG.info("Handling entry remotefile name : " + remoteFileName+" cronjobInputFileName : "+cronjobInputFileName);
if (remoteFileName.startsWith(cronjobInputFileName)) {
LOG.info(" Found matching file according to the cronjob input file name i.e "+remoteFileName);
SftpATTRS attrs = sftpChannel.stat(remoteFileName);
SftpATTRS lattrs = sftpChannel.lstat(remoteFileName);
if ((attrs.isDir()) || (lattrs.isLink())) {
LOG.info(remoteFileName + " is not a data file. Skipping.");
continue;
}
LOG.info("Processing File: " + remoteFileName);
// //Make sure that this isn't an already processed file
if (remoteFileName.indexOf(BACKUP_EXT) > 0) {
LOG.info("Already processed file: " + remoteFileName);
continue;
}
//Add the name to the list to process
returnList.add(remoteFileName);
//Download the file
LOG.info("Getting the file: " + remoteFileName);
sftpChannel.get(remoteFileName, remoteFileName);
//Move the File to archive
if (archiveFileAfterProcessing){
moveFileToArchive(strSftpUSourceDir, strArchdir, sftpChannel, remoteFileName);
}
}
}
} catch (JSchException pe) {
LOG.error(JOB_ERROR_MESSAGE);
LOG.error("Error while trying to get list of files over ssh", pe);
} catch (SftpException ioe) {
LOG.error(JOB_ERROR_MESSAGE);
LOG.error("Error while trying to get list of files over ssh", ioe);
} catch (Exception ee){
ee.printStackTrace();
} finally {
closeChannelConnection();
}
return returnList;
}
public void sendFilesOverSSH(String srcFileName, String destinationFileName) {
try {
final Channel channel = openChannelConnection(srcFileName, SFTPType.SALSIFY);
sftpChannel = (ChannelSftp) channel;
sftpChannel.cd(strSftpUSourceDir);
Vector<ChannelSftp.LsEntry> list = sftpChannel.ls(".");
MediaModel salsifyFile = mediaService.getMedia(srcFileName);
LOG.info("Processing File: " + srcFileName);
uploadFile(salsifyFile, sftpChannel, destinationFileName);
} catch (JSchException pe) {
LOG.error(JOB_ERROR_MESSAGE);
LOG.error("Error while trying to send files over ssh", pe);
} catch (SftpException ioe) {
LOG.error(JOB_ERROR_MESSAGE);
LOG.error("Error while trying to send files over ssh", ioe);
} finally {
closeChannelConnection();
}
}
private void initSFTPClient(String srcFileName, SFTPType type) {
strUserName = Config.getString(type.name().toLowerCase() + ".sftp.user", "");
strPassword = Config.getString(type.name().toLowerCase() + ".sftp.password", "");
strSftpHost = Config.getString(type.name().toLowerCase() + ".sftp.host", "");
strSftpUSourceDir = Config.getString(type.name().toLowerCase() + ".sftp.sourceDir", "");
strArchdir = Config.getString(type.name().toLowerCase() + ".sftp.arch", "");
strSftpPort = Config.getInt(type.name().toLowerCase() + ".sftp.port", 22);
}
private Channel openChannelConnection(String srcFileName, SFTPType type) throws JSchException {
initSFTPClient(srcFileName, type);
sftpClient = new JSch();
session = sftpClient.getSession(strUserName, strSftpHost, strSftpPort);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(strPassword);
session.connect();
final Channel channel = session.openChannel("sftp");
channel.connect();
return channel;
}
private void closeChannelConnection() {
try {
if (session != null) {
session.disconnect();
}
if (sftpChannel != null) {
sftpChannel.disconnect();
sftpChannel.quit();
}
} catch (Exception fe) {
LOG.error("SFTPException in SalsifySFTPHelper for ProcessDynamicDataForSalsifyJobPerformable during ftp quit", fe);
}
}
private void uploadFile(MediaModel fileToUpload, ChannelSftp sftpChannel, String remoteFileName) throws SftpException {
LOG.info("Uploading file " + fileToUpload.getLocation() + " to Salsify SFTP as " + remoteFileName);
String sourceFilePath = Config.getString("media.source.salsify.file.location", "") + fileToUpload.getLocation();
sftpChannel.put(sourceFilePath, remoteFileName);
}
private void moveFileToArchive(String strSftpUSourceDir, String strArchdir, ChannelSftp sftpChannel, String remoteFileName) throws SftpException {
LOG.info("Archiving file: " + remoteFileName);
sftpChannel.cd(strArchdir);
sftpChannel.put(remoteFileName, remoteFileName + BACKUP_EXT);
sftpChannel.cd(strSftpUSourceDir);
sftpChannel.rm(remoteFileName);
}
}
Comments
Post a Comment