diff --git a/core/README.md b/core/README.md new file mode 100644 index 0000000..e108028 --- /dev/null +++ b/core/README.md @@ -0,0 +1,30 @@ +#关于 USVisaTrack 项目 +-------- +USVisaTrack 是一个基于数据的商品信息平台 + + +#关于 USVisaTrack 这个仓库 +-------- + + +#JDK 配置 +-------- +JDK 需要使用 1.8 以上的版本。 + + +#安装 +-------- + + +#SCM +-------- +https://gitlab.com/ntc/us-visa-track + + +#文档 +-------- +https://www.cwiki.us + +#论坛 +-------- +www.ossez.com \ No newline at end of file diff --git a/core/pom.xml b/core/pom.xml new file mode 100644 index 0000000..3782f5a --- /dev/null +++ b/core/pom.xml @@ -0,0 +1,291 @@ + + + 4.0.0 + + + com.usvisatrack + usvisatrack-main + 0.1.0 + + + usvisatrack-core + + usvisatrack core + usvisatrack core code and view files to render HTML. + + + true + 1.250 + 2.5.6.SEC03 + 2.4.11 + + true + + + + + + + + org.mariadb.jdbc + mariadb-java-client + 1.5.9 + + + org.hibernate + hibernate-c3p0 + 5.1.2.Final + + + org.hibernate + hibernate-ehcache + 5.1.2.Final + + + org.hibernate + hibernate-search-orm + 5.5.5.Final + + + + org.hibernate + hibernate-validator + 5.2.4.Final + + + + org.apache.solr + solr-solrj + 5.5.3 + + + + + + + org.apache.commons + commons-lang3 + 3.4 + + + + org.apache.commons + commons-math3 + 3.0 + + + + commons-cli + commons-cli + 1.2 + + + + commons-net + commons-net + 3.3 + + + + org.apache.commons + commons-csv + 1.1 + + + + commons-validator + commons-validator + 1.4.1 + + + + org.apache.httpcomponents + httpclient + 4.5.2 + + + + commons-fileupload + commons-fileupload + 1.3.1 + + + + + + org.jdom + jdom2 + 2.0.6 + + + + gov.nih.imagej + imagej + 1.47 + + + + net.sf.barcode4j + barcode4j + 2.1 + + + + com.google.zxing + javase + 3.2.1 + + + + + + + com.amazonaws + aws-java-sdk + 1.11.114 + + + + + com.paypal.sdk + paypal-core + 1.7.2 + + + com.paypal.sdk + rest-api-sdk + 1.13.0 + + + + + + commons-beanutils + commons-beanutils + 1.8.3 + + + + com.fasterxml.jackson.core + jackson-databind + 2.8.7 + + + + org.springframework + spring-context + 4.1.2.RELEASE + + + + org.springframework + spring-orm + 4.1.2.RELEASE + + + + javax.servlet + servlet-api + 2.5 + + + + + net.sf.ehcache + ehcache-core + 2.6.9 + + + jaxen + jaxen + 1.1.6 + + + + + + + + + + + org.kohsuke.stapler + maven-stapler-plugin + ${maven-stapler-plugin.version} + + + /lib/.* + + + + + maven-project-info-reports-plugin + 2.6 + + false + + + + + + + + debug + + true + + + + release + + + + + org.kohsuke.stapler + maven-stapler-plugin + + + + + jelly-taglibdoc + + + + /lib/.* + + + + + + + + + + + + findbugs + + + true + + + + + cobertura + + + + + maven-surefire-plugin + + + true + + + + + + + diff --git a/core/src/main/java/META-INF/MANIFEST.MF b/core/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000..e41bf7b --- /dev/null +++ b/core/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 +Class-Path: \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/cloud/AwsS3.java b/core/src/main/java/com/usvisatrack/core/cloud/AwsS3.java new file mode 100644 index 0000000..cd55bec --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/cloud/AwsS3.java @@ -0,0 +1,95 @@ +package com.usvisatrack.core.cloud; + +import java.io.Closeable; +import java.io.File; +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.amazonaws.AmazonClientException; +import com.amazonaws.AmazonServiceException; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.model.PutObjectRequest; + +/** + * BcoDepot Amazon Utils class + * + * @author YuCheng Hu + * + */ +/** + * Common class to handle the movement of files into the Cloud (in this case Rackspace Cloud Files). + * + * @author YuCheng Hu + * + */ +public class AwsS3 implements Closeable { + + private static Logger logger = LoggerFactory.getLogger(AwsS3.class); + + private static AwsS3 instance = null; + private static String accessKey = null; + private static String secreKey = null; + + /** + * Constructor Function + */ + public AwsS3() { + + accessKey = "AKIAI6CDTBEK6PF3QBMA"; + secreKey = "F15vscDOpQ3Ac0L08eICSGw55ZlM6qbXgEuboB0B"; + + } + + public static AwsS3 getInstance() { + if (instance == null) { + instance = new AwsS3(); + } + return instance; + } + + /** + * Upload files of given directory to Cloud Files Path + * + * @param bucketName + * @param remotePath + * @param file + * @throws Exception + */ + public static void UploadObjectSingleOperation(String bucketName, String remotePath, File file) throws Exception { + + BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secreKey); + AmazonS3 s3client = new AmazonS3Client(awsCreds); + + // AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); + try { + System.out.println("Uploading a new object to S3 from a file\n"); + + s3client.putObject(new PutObjectRequest(bucketName, remotePath, file)); + + } catch (AmazonServiceException ase) { + System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + + "to Amazon S3, but was rejected with an error response" + " for some reason."); + System.out.println("Error Message: " + ase.getMessage()); + System.out.println("HTTP Status Code: " + ase.getStatusCode()); + System.out.println("AWS Error Code: " + ase.getErrorCode()); + System.out.println("Error Type: " + ase.getErrorType()); + System.out.println("Request ID: " + ase.getRequestId()); + } catch (AmazonClientException ace) { + System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); + System.out.println("Error Message: " + ace.getMessage()); + } + + } // End uploadDirectoryContentsToCloud + + @Override + public void close() throws IOException { + // TODO Auto-generated method stub + + } + +} diff --git a/core/src/main/java/com/usvisatrack/core/common/DataObject.java b/core/src/main/java/com/usvisatrack/core/common/DataObject.java new file mode 100644 index 0000000..dc7c54f --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/common/DataObject.java @@ -0,0 +1,127 @@ +package com.usvisatrack.core.common; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.EntityListeners; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; + +import org.hibernate.search.annotations.DateBridge; +import org.hibernate.search.annotations.DocumentId; +import org.hibernate.search.annotations.Resolution; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.usvisatrack.core.dao.model.EntityListener; + +/** + * + * @author YuCheng Hu + * + */ +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, creatorVisibility = JsonAutoDetect.Visibility.NONE) +@EntityListeners({EntityListener.class}) +@MappedSuperclass +public abstract class DataObject implements Serializable { + + public abstract interface Save { + + } + + public abstract interface Update { + + } + + private static final long serialVersionUID = -67188388306700736L; + public static final String ID_PROPERTY_NAME = "id"; + public static final String CREATE_DATE_PROPERTY_NAME = "createDate"; + public static final String MODIFY_DATE_PROPERTY_NAME = "modifyDate"; + private Long id; + private Date createDate; + private Date modifyDate; + + /** + * + * @return + */ + @JsonProperty + @DocumentId + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + public Long getId() { + return this.id; + } + + /** + * + * @param id + */ + public void setId(Long id) { + this.id = id; + } + + /** + * + * @return + */ + @JsonProperty + @DateBridge(resolution = Resolution.SECOND) + @Column(nullable = false, updatable = false) + public Date getCreateDate() { + return this.createDate; + } + + /** + * + * @param createDate + */ + public void setCreateDate(Date createDate) { + this.createDate = createDate; + } + + /** + * + * @return + */ + @JsonProperty + @DateBridge(resolution = Resolution.SECOND) + @Column(nullable = false) + public Date getModifyDate() { + return this.modifyDate; + } + + /** + * + * @param modifyDate + */ + public void setModifyDate(Date modifyDate) { + this.modifyDate = modifyDate; + } + + /** + * + */ + public boolean equals(Object obj) { + if (obj == null) + return false; + if (this == obj) + return true; + if (!DataObject.class.isAssignableFrom(obj.getClass())) + return false; + DataObject localBaseEntity = (DataObject) obj; + return getId() != null ? getId().equals(localBaseEntity.getId()) : false; + } + + /** + * + */ + public int hashCode() { + int i = 17; + i += (getId() == null ? 0 : getId().hashCode() * 31); + return i; + } +} diff --git a/core/src/main/java/com/usvisatrack/core/common/Factory.java b/core/src/main/java/com/usvisatrack/core/common/Factory.java new file mode 100644 index 0000000..be1d2ed --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/common/Factory.java @@ -0,0 +1,497 @@ +package com.usvisatrack.core.common; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.util.Date; +import java.util.List; + +import org.hibernate.Criteria; +import org.hibernate.Query; +import org.hibernate.ScrollMode; +import org.hibernate.ScrollableResults; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.criterion.Projections; +import org.hibernate.criterion.Restrictions; +import org.hibernate.resource.transaction.spi.TransactionStatus; +import org.hibernate.search.FullTextSession; +import org.hibernate.search.Search; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.usvisatrack.core.common.search.InternalSearch; +import com.usvisatrack.core.dao.model.User; + +/** + * + * @author YuCheng Hu + * + */ +public class Factory { + private static SessionFactory factory = null; + private static Logger logger = LoggerFactory.getLogger(Factory.class); + + private static SessionFactory sessionFactory; + // private static ServiceRegistry serviceRegistry; + + private static int indexBatchSize = 100; + private static boolean noCommit = false; // blocks the commit function from + // being called - this is useful + // for unit testing. + private static boolean autoRollback = false; // executes a rollback instead + // of a commit when commit + // is called - this is + // useful for unit + // testing. + private static boolean noClose = false; // prevents the connection from + // being closed. This is useful for + // unit testing. + + /** + * Get Hibernate connection Session + * + * @return + */ + public static Session getSession() { + return Factory.getFactory().getCurrentSession(); + } + + /** + * Get session connection Get + * + * @return + */ + public static SessionFactory getFactory() { + if (Factory.factory == null) + Factory.factory = Factory.initSession(); + + return Factory.factory; + } + + /** + * + * @return + */ + public static SessionFactory initSession() { + // Hibernate 4 init Session + // return new Configuration().configure().buildSessionFactory(); + // Configuration configuration = new Configuration(); + // configuration.configure(); + // serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); + // sessionFactory = configuration.buildSessionFactory(serviceRegistry); + + // Hibernate 5 init Session + // configures settings from hibernate.cfg.xml + final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build(); + + try { + sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); + } catch (Exception e) { + logger.error("HIBERNATE DB SESSION INIT ERROR", e); + // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory + // so destroy it manually. + StandardServiceRegistryBuilder.destroy(registry); + } + + return sessionFactory; + } + + /** + * + */ + public static void beginTransaction() { + // If transaction not exist, create one + if (!(Factory.getSession().getTransaction() != null + && Factory.getSession().getTransaction().getStatus() == TransactionStatus.ACTIVE)) { + Factory.getSession().getTransaction().begin(); + } + } + + public static Transaction getTransaction() { + return Factory.getSession().beginTransaction(); + } + + public static void commitTransaction() { + if (isAutoRollback()) { + Factory.rollbackTransaction(); + return; + } + if (Factory.getSession().getTransaction() != null + && Factory.getSession().getTransaction().getStatus() == TransactionStatus.ACTIVE) { + if (!noCommit) + Factory.getSession().getTransaction().commit(); + // zhaohc + Factory.getSession().close(); + } + } + + public static void commitTransaction(Transaction tx) { + if (isAutoRollback()) { + tx.rollback(); + } + if (tx != null && tx.getStatus() == TransactionStatus.ACTIVE) { + if (!noCommit) + tx.commit(); + } + } + + public static void rollbackTransaction() { + if (Factory.getSession().getTransaction() != null && Factory.getSession().getTransaction().getStatus() == TransactionStatus.ACTIVE) + Factory.getSession().getTransaction().rollback(); + } + + public static void rollbackTransaction(Transaction tx) { + if (tx != null && tx.getStatus() == TransactionStatus.ACTIVE) + tx.rollback(); + } + + /** + * Gets an object of type T from Hibernate. + * + * @param + * @param classEntity + * @param id + * @return + */ + @SuppressWarnings("unchecked") + public static T get(Class classEntity, long id) { + Object object = Factory.getSession().get(classEntity, id); + + if (object == null) + return null; + + return (T) object; + } + + /** + * Saves the specified object to the database. + * + * @param + * @param object + */ + public static void save(DataObject object) { + if (object == null) + throw new NullPointerException("Object supplied is null"); + + if (object.getId() != null && object.getId() > 0) + Factory.getSession().saveOrUpdate(object); + else { + Factory.getSession().save(object); + } + } + + public static void saveUsers(User users) { + if (users == null) + throw new NullPointerException("Object supplied is null"); + Factory.getSession().save(users); + } + + public static void updateUsers(User users) { + if (users == null) + throw new NullPointerException("Object supplied is null"); + Factory.getSession().update(users); + } + + /** + * Deletes the specified object. + * + * @param + * @param object + */ + public static void delete(DataObject object) { + try { + Factory.beginTransaction(); + Factory.getSession().delete(object); + Factory.commitTransaction(); + + } catch (Exception ex) { + logger.error("DB Record Delete Error:", ex); + Factory.rollbackTransaction(); + } + + } + + /** + * Creates an hibernate criteria object which is used to query against objects. + * + * @param classArg + * @return + */ + public static Criteria createCriteria(Class classArg) { + + Criteria criteria = Factory.getSession().createCriteria(classArg); + // Factory.commitTransaction(); + + return criteria; + } + + /** + * Creates an hibernate criteria object with the specified alias. + * + * @param classArg + * @param alias + * @return + */ + public static Criteria createCriteria(Class classArg, String alias) { + return Factory.getSession().createCriteria(classArg, alias); + } + + /** + * Serializes an object into a byte array. + * + * @param object + * @return + */ + public static byte[] serialize(Object object) throws IOException { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + + try { + ObjectOutputStream serializer = new ObjectOutputStream(stream); + serializer.writeObject(object); + serializer.close(); + } catch (IOException ex) { + Factory.handleException(ex); + } + + byte[] result = stream.toByteArray(); + stream.close(); + return result; + } + + private static void handleException(IOException ex) { + // TODO Auto-generated method stub + + } + + /** + * Merges the specified instance. + * + * @param obj + */ + public static void merge(DataObject obj) { + Factory.getSession().merge(obj); + } + + /** + * Refreshes the specified instance. + * + * @param approval + */ + public static void refresh(DataObject obj) { + Factory.getSession().refresh(obj); + } + + /** + * Saves or updates the object. + * + * @param obj + */ + public static void saveOrUpdate(DataObject obj) { + Factory.getSession().saveOrUpdate(obj); + } + + /** + * Evicts the specified object from the session. + * + * @param obj + */ + public static void evict(DataObject obj) { + Factory.getSession().evict(obj); + } + + /** + * Searches all available search sources (this is used for the back page only). + * + * @param search + * @return + */ + + // public static PagedResults search(InternalSearchCriteria + // criteria, User user) { + // List> types = new ArrayList>(); + // List result = new ArrayList(); + // FullTextSession fullTextSession = Factory.getFullTextSession(); + // List searches = InternalSearchFactory.getForUser(user); + // int totalRows = 0; + // BooleanQuery query = new BooleanQuery(); + // + // // loop through each type + // for (InternalSearch search : searches) { + // try { + // // add the type + // BooleanQuery typeQuery = new BooleanQuery(); + // typeQuery.add(new TermQuery(new Term(DocumentBuilder.CLASS_FIELDNAME, + // search.getResultType())), Occur.MUST); + // + // // add the fields + // String [] searchFields = search.getSearchFields().split(","); + // BooleanQuery subQuery = new BooleanQuery(); + // for (String field : searchFields) { + // subQuery.add(new TermQuery(new Term(field, "(" + + // criteria.getText().toLowerCase() + ")")), Occur.SHOULD); + // } + // + // // apply the analyzer to the sub query + // try { + // String queryString = subQuery.toString(); + // // typeQuery.add(new QueryParser(queryString, new + // StandardAnalyzer()).parse(queryString), Occur.MUST); + // typeQuery.add(new QueryParser(queryString, new + // WhitespaceAnalyzer()).parse(queryString), Occur.MUST); + // } catch (ParseException ex) { + // Factory.log.error("Could not parse query.", ex); + // } + // + // query.add(typeQuery, Occur.SHOULD); + // + // types.add(Class.forName(search.getResultType())); + // } catch (ClassNotFoundException ex) { + // Factory.log.error("Internal search class not found: " + + // search.getResultType() + " for search: " + search.getName(), ex); + // } + // } + // + // // run the query + // FullTextQuery searchQuery = fullTextSession.createFullTextQuery(query, + // types.toArray(new Class[0])); + // searchQuery.setFirstResult(criteria.getPageNumber() * + // criteria.getPageSize()); + // searchQuery.setMaxResults(criteria.getPageSize()); + // searchQuery.enableFullTextFilter("users").setParameter("user", user); + // searchQuery.enableFullTextFilter("documents").setParameter("user", user); + // searchQuery.enableFullTextFilter("accounts").setParameter("user", user); + // searchQuery.enableFullTextFilter("content").setParameter("user", user); + // Factory.log.debug("Search Query: " + query.toString()); + // + // result = searchQuery.list(); + // totalRows = searchQuery.getResultSize(); + // + // return new PagedResults(result, totalRows); + // } + // + // /** + // * Searches a specific type. + // * @param criteria + // * @param type + // * @param user + // * @return + // */ + // @SuppressWarnings("unchecked") + // public static PagedResults search(InternalSearchCriteria + // criteria, Class type, User user) { + // FullTextSession fullTextSession = Factory.getFullTextSession(); + // // get the internal search by the specified type + // InternalSearch search = InternalSearchFactory.getByType(type, user); + // List result = null; + // String [] queries = null; + // String [] fields = null; + // int totalRows = 0; + // + // if (search == null) + // return new PagedResults(new ArrayList(), 0); + // + // // get the fields + // fields = search.getSearchFields().split(","); + // queries = new String[fields.length]; + // + // // build the array of term queries + // for (int index = 0; index < fields.length; index++) { + // // add the query + // queries[index] = criteria.getText(); + // } + // + // // execute the search + // try { + // org.apache.lucene.search.Query query = + // MultiFieldQueryParser.parse(queries, fields, new StandardAnalyzer()); + // FullTextQuery searchQuery = fullTextSession.createFullTextQuery(query, + // type); + // searchQuery.setFirstResult(criteria.getPageNumber() * + // criteria.getPageSize()); + // searchQuery.setMaxResults(criteria.getPageSize()); + // searchQuery.enableFullTextFilter("users").setParameter("user", user); + // searchQuery.enableFullTextFilter("documents").setParameter("user", user); + // searchQuery.enableFullTextFilter("accounts").setParameter("user", user); + // searchQuery.enableFullTextFilter("content").setParameter("user", user); + // Factory.log.debug("Search Query: " + query.toString()); + // + // result = searchQuery.list(); + // totalRows = searchQuery.getResultSize(); + // } catch (ParseException e) { + // Factory.log.error("Could not parse search query", e); + // } + // + // return new PagedResults(result, totalRows); + // } + /** + * Creates a hibernate query. + * + * @param query + * @return + */ + public static Query createQuery(String query) { + return Factory.getSession().createQuery(query); + } + + public static FullTextSession getFullTextSession() { + return Search.getFullTextSession(Factory.getSession()); + } + + /** + * Closes the session factory. Should only be called when an application is closing. + */ + public static void close() { + if (!isNoClose()) + Factory.getFactory().close(); + } + + /** + * @param noCommit + * the noCommit to set + */ + public static void setNoCommit(boolean noCommit) { + Factory.noCommit = noCommit; + } + + /** + * @return the noCommit + */ + public static boolean isNoCommit() { + return noCommit; + } + + /** + * @param autoRollback + * the autoRollback to set + */ + public static void setAutoRollback(boolean autoRollback) { + Factory.autoRollback = autoRollback; + } + + /** + * @return the autoRollback + */ + public static boolean isAutoRollback() { + return autoRollback; + } + + /** + * @param noClose + * the noClose to set + */ + public static void setNoClose(boolean noClose) { + Factory.noClose = noClose; + } + + /** + * @return the noClose + */ + public static boolean isNoClose() { + return noClose; + } +} diff --git a/core/src/main/java/com/usvisatrack/core/common/InternalSearchFactory.java b/core/src/main/java/com/usvisatrack/core/common/InternalSearchFactory.java new file mode 100644 index 0000000..7d3507f --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/common/InternalSearchFactory.java @@ -0,0 +1,83 @@ +package com.usvisatrack.core.common; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.usvisatrack.core.common.search.InternalSearch; +import com.usvisatrack.core.dao.model.EntityListener; + +import java.io.Serializable; +import java.util.Date; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.EntityListeners; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; + +import org.hibernate.criterion.Restrictions; +import org.hibernate.search.annotations.DateBridge; +import org.hibernate.search.annotations.DocumentId; +import org.hibernate.search.annotations.Field; +import org.hibernate.search.annotations.Index; +import org.hibernate.search.annotations.Resolution; +import org.hibernate.search.annotations.Store; + +public class InternalSearchFactory { + /** + * Gets a list of all internal searches. + * + * @return + */ + @SuppressWarnings("unchecked") + public static List getAll() { + return Factory.createCriteria(InternalSearch.class).list(); + } + + /** + * Gets an internal search by the specified id. + * + * @param id + * @return + */ + public static InternalSearch get(int id) { + return Factory.get(InternalSearch.class, id); + } + + /** + * Saves the specified internal search. + * + * @param search + */ + public static void save(InternalSearch search) { + Factory.save(search); + } + + /** + * Deletes the specified internal search. + * + * @param search + */ + public static void delete(InternalSearch search) { + Factory.delete(search); + } + + /** + * Gets a list of searches available to the specified user. + * + * @param user + * @return + */ + @SuppressWarnings("unchecked") + /** + * Gets an internal search by name. + * @param name + * @return + */ + public static InternalSearch get(String name) { + return (InternalSearch) Factory.createCriteria(InternalSearch.class).add(Restrictions.eq("name", name)).setMaxResults(1) + .uniqueResult(); + } + +} diff --git a/core/src/main/java/com/usvisatrack/core/common/USVisaTrackConstant.java b/core/src/main/java/com/usvisatrack/core/common/USVisaTrackConstant.java new file mode 100644 index 0000000..5975ace --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/common/USVisaTrackConstant.java @@ -0,0 +1,25 @@ +package com.usvisatrack.core.common; + +/** + * UPCES Constant + * + * @author YuCheng Hu + * + */ +public final class USVisaTrackConstant { + + public static final String CDN_URL = "https://cdn.upcex.com/"; + + public static final String APIDATA_FOLDER = "/home/bcodepot/apidata/"; + + public static final String SESSION_NAME_USER = "upcexUser"; + public final static String CDN_FOLDER_ITEM_IMG = "/home/cdn/com-upcex/data/item/img"; + + public static enum ItemCodeStatus { + NORMAL, CHECK_DIGIT_ERROR, WEB_DATA_NOT_FOUND; + } + + public static enum ItemCodeType { + SKU, ASIN, BBYSKU; + } +} \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/common/USVisaTrackUtils.java b/core/src/main/java/com/usvisatrack/core/common/USVisaTrackUtils.java new file mode 100644 index 0000000..8e56dba --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/common/USVisaTrackUtils.java @@ -0,0 +1,1403 @@ +package com.usvisatrack.core.common; + +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.nio.charset.Charset; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.validator.routines.checkdigit.CheckDigitException; +import org.apache.commons.validator.routines.checkdigit.EAN13CheckDigit; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.filter.Filters; +import org.jdom2.output.Format; +import org.jdom2.output.XMLOutputter; +import org.jdom2.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; +import org.joda.time.DateTime; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.Assert; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Charsets; +import com.google.common.hash.Hasher; +import com.google.common.hash.Hashing; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + + +import ij.IJ; +import ij.ImagePlus; +import ij.process.ImageProcessor; + +/** + * BcoDepot BcodePotUtils class + * + * @author YuCheng Hu + * + */ +public final class USVisaTrackUtils { + + private static final Logger logger = LoggerFactory.getLogger(USVisaTrackUtils.class); + + private static ObjectMapper objectMapp = new ObjectMapper(); + + // log specificly for melissa data calls + // private static Logger mdLog = + // LoggerFactory.getLogger("com.verani.BcodePotUtils.melissaData"); + // private static Pattern emailPattern = Pattern + // .compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); + + public static String createCacheFile(String fileName) { + + Date currentDate = new Date(); + + DateFormat yyyyMM = new SimpleDateFormat("yyyyMM"); + DateFormat dd = new SimpleDateFormat("dd"); + + return "/" + yyyyMM.format(currentDate) + "/" + dd.format(currentDate) + "/" + currentDate.getTime() + "/" + currentDate.getTime() + + fileName; + + } + + public static String createCacheFile(String folderPath, String fileName) { + + Date currentDate = new Date(); + + DateFormat yyyyMM = new SimpleDateFormat("yyyyMM"); + DateFormat dd = new SimpleDateFormat("dd"); + + return folderPath + "/" + yyyyMM.format(currentDate) + "/" + dd.format(currentDate) + "/" + currentDate.getTime() + fileName; + + } + + public static void toJson(HttpServletResponse response, String contentType, Object value) { + Assert.notNull(response); + Assert.hasText(contentType); + try { + response.setContentType(contentType); + objectMapp.writeValue(response.getWriter(), value); + } catch (Exception localException) { + localException.printStackTrace(); + } + } + + public static void toJson(HttpServletResponse response, Object value) { + Assert.notNull(response); + PrintWriter localPrintWriter = null; + try { + localPrintWriter = response.getWriter(); + objectMapp.writeValue(localPrintWriter, value); + localPrintWriter.flush(); + } catch (Exception localException) { + localException.printStackTrace(); + } finally { + IOUtils.closeQuietly(localPrintWriter); + } + } + + public static T toObject(String json, Class valueType) { + Assert.hasText(json); + Assert.notNull(valueType); + try { + return objectMapp.readValue(json, valueType); + } catch (Exception localException) { + localException.printStackTrace(); + } + return null; + } + + public static T toObject(String json, TypeReference typeReference) { + Assert.hasText(json); + Assert.notNull(typeReference); + try { + return objectMapp.readValue(json, typeReference); + } catch (Exception localException) { + localException.printStackTrace(); + } + return null; + } + + public static T toObject(String json, JavaType javaType) { + Assert.hasText(json); + Assert.notNull(javaType); + try { + return objectMapp.readValue(json, javaType); + } catch (Exception localException) { + localException.printStackTrace(); + } + return null; + } + + public static String generateShortUUID() { + UUID uuid = UUID.randomUUID(); + logger.debug("UUID: " + uuid); + // trim UUID + char[] uuidChars = uuid.toString().toCharArray(); + String newUUID = ""; + for (int i = 0; i < 8; i++) { + newUUID += uuidChars[i]; + } + logger.debug("Trimmed UUID: " + newUUID); + return newUUID; + } + + /** + * This method is handy for cleaning the street address of a listing (for URL). Many of our listings do not have street addresses or + * parts of a street address. Rather than duplicate this code in several places, I have just created a BcodePotUtils method to do the + * cleaning. The method will check for nulls and do REGEX replaces to remove some things. First, any non-numeric or non-alphanumeric + * characters will be replaced with URL friendly "-" character. If multiple "--" characters occur... they will be replaced. Also, if the + * string has a trailing "-" remove that so it doesn't look funny on URL. + * + * @param streetNumber + * - Listing Street Number (maybe null) + * @param streetName + * - Listing Street Name (maybe null) + * @param separator + * - Character to separate Street Number and Street Name (will use space " " if null passes) + * @return + */ + public static String filterStreetAddressURL(String streetNumber, String streetName, String separator) { + if (streetNumber != null && streetName != null) { + streetNumber = streetNumber.trim().replaceAll("[^a-zA-Z0-9]", "-"); + streetName = streetName.trim().replaceAll("[^a-zA-Z0-9]", "-"); + separator = (separator != null && !separator.isEmpty() ? separator : "-"); + return (streetNumber + separator + streetName).replaceAll("[-]{2,10}", "-").replaceAll("[-]{1,10}$", ""); + } else if (streetNumber != null) { + return streetNumber.trim().replaceAll("[-]{2,10}", "-").replaceAll("[-]{1,10}$", ""); + } else if (streetName != null) { + return streetName.trim().replaceAll("[-]{2,10}", "-").replaceAll("[-]{1,10}$", ""); + } else + return ""; + } + + /** + * Override the previous method. Allow for simpler functionality. + * + * @param streetNumber + * @param streetName + * @return + */ + public static String filterStreetAddressURL(String streetNumber, String streetName) { + return filterStreetAddressURL(streetNumber, streetName, null); + } + + /** + * Parses an integer from the specified string. + * + * @param string + * @return + */ + public static int parseInt(String string) { + if (string == null) + return 0; + + try { + return Integer.parseInt(string); + } catch (NumberFormatException ex) { + return 0; + } + } + + /** + * Parses an integer from an object. + * + * @param input + * @return + */ + public static int parseInt(Object input) { + if (input != null) { + // is it an int already? + if (input instanceof Integer) + return (Integer) input; + + // string? + if (input instanceof String) + return USVisaTrackUtils.parseInt((String) input); + } + + return 0; + } + + /** + * Determines if the string is empty. + * + * @param string + * @return + */ + public static boolean isEmpty(String string) { + return string == null || string.equals(""); + } + + /** + * Joins a iterable string list into a string with the specified delimeter. + * + * @param list + * @param delimeter + * @return + */ + public static String join(String[] list, String delimeter) { + ArrayList arrayList = new ArrayList(); + for (String entry : list) { + arrayList.add(entry); + } + return USVisaTrackUtils.join(arrayList, delimeter); + } + + /** + * Joins a iterable string list into a string with the specified delimeter. + * + * @param list + * @param delimeter + * @return + */ + public static String join(Iterable list, String delimeter) { + StringBuffer buffer = new StringBuffer(); + Iterator iterator = list.iterator(); + + while (iterator.hasNext()) { + buffer.append(iterator.next()); + + if (iterator.hasNext()) + buffer.append(delimeter); + } + + return buffer.toString(); + } + + /** + * Creates a new {@link ArrayList} from the specified array. + * + * @param + * @param array + * @return + */ + public static List toList(T[] array) { + List result = new ArrayList(); + + for (T entry : array) { + result.add(entry); + } + + return result; + } + + /** + * Joins a iterable object list into a string with the specified delimeter. toString is called on each object. + * + * @param list + * @param delimeter + * @return + */ + public static String joinObjects(Iterable list, String delimeter) { + StringBuffer buffer = new StringBuffer(); + Iterator iterator = list.iterator(); + + while (iterator.hasNext()) { + buffer.append(iterator.next()); + + if (iterator.hasNext()) + buffer.append(delimeter); + } + + return buffer.toString(); + } + + /** + * Parses a boolean from a string. + * + * @param value + * @return + */ + public static boolean parseBoolean(String value) { + if (value != null && value.toLowerCase().equals("true")) + return true; + + return false; + } + + /** + * Parses a boolean from an object. + * + * @param value + * @return + */ + public static boolean parseBoolean(Object value) { + if (value == null) + return false; + + if (value instanceof String) + return USVisaTrackUtils.equals(((String) value).toLowerCase(), "true"); + + if (value instanceof Boolean) + return (Boolean) value; + + return false; + } + + /** + * Parses a float from a string. + * + * @param value + * @return + */ + public static float parseFloat(String value) { + + if (value != null) { + try { + return Float.parseFloat(value); + } catch (NumberFormatException ex) { + return 0.0f; + } + } + + return 0.0f; + } + + /** + * Parses a float from a string. + * + * @param value + * @return + */ + public static double parseDouble(String value) { + + if (value != null) { + try { + return Double.parseDouble(value); + } catch (NumberFormatException ex) { + return 0.0d; + } + } + + return 0.0d; + } + + /** + * Performs a null safe equality check on the two provided objects. + * + * @param a + * @param b + * @return + */ + public static boolean equals(Object a, Object b) { + // both null, equal + if (a == b) + return true; + + // one is null and the other is not, not equal + if ((a == null && b != null) || (a != null && b == null)) + return false; + + // neither are null, compare them + return a.equals(b); + } + + /** + * Creates a url friendly version of the string. + * + * @param name + * @return + */ + public static String makeUrlFriendly(String name) { + // Compile regular expression + Pattern pattern = Pattern.compile("[^A-Za-z0-9\\s]"); + String output = pattern.matcher(name).replaceAll(""); + + return output.replace(" ", "-").toLowerCase(); + } + + /** + * Ensures the uri ends with a trailing / + * + * @param value + * @return + */ + public static String uriTrailingSeparator(String value) { + if (value.endsWith("/")) + return value; + else + return value + "/"; + } + + /** + * Ensures the uri starts with a / + * + * @param value + * @return + */ + public static String uriPrecedingSeparator(String value) { + if (value != null) { + if (value.startsWith("/")) + return value; + else + return "/" + value; + } + + return ""; + } + + /** + * Ensures the value has both a preceding and trailing separator. + * + * @param value + * @return + */ + public static String uriWrapSeparator(String value) { + return USVisaTrackUtils.uriPrecedingSeparator(USVisaTrackUtils.uriTrailingSeparator(value)); + } + + /** + * Ensures the uri does not end with a separator. + * + * @param fullUrl + * @return + */ + public static String uriRemoveTrailingSeparator(String fullUrl) { + return fullUrl.endsWith("/") ? fullUrl.substring(0, fullUrl.length() - 1) : fullUrl; + } + + /** + * Pads a number with preceeding zeros. + * + * @param object + * the number to pad. + * @param padding + * the number of zeros to pad. + * @return + */ + public static String zeroPad(Object object, int padding) { + String rawFloat = object.toString(); + int dotIndex = rawFloat.indexOf("."); + if (dotIndex == -1) + dotIndex = rawFloat.length(); + // if (dotIndex > padding) + // throw new IllegalArgumentException("Try to pad on a number too big"); + StringBuilder paddedFloat = new StringBuilder(); + for (int padIndex = dotIndex; padIndex < padding; padIndex++) { + paddedFloat.append('0'); + } + return paddedFloat.append(rawFloat).toString(); + } + + /** + * Converts an input stream to string data. + * + * @param in + * @return + */ + public static String readString(InputStream in) { + StringBuffer out = new StringBuffer(); + byte[] b = new byte[4096]; + try { + for (int n; (n = in.read(b)) != -1;) { + out.append(new String(b, 0, n)); + } + } catch (IOException ex) { + } + return out.toString(); + } + + /** + * Gets the full state name based on the short state name. + * + * @param shortStateName + * @return + */ + public static String getFullState(String shortStateName) { + if (shortStateName.equalsIgnoreCase("NH")) + return "New Hampshire"; + else if (shortStateName.equalsIgnoreCase("MA")) + return "Massachusetts"; + else if (shortStateName.equalsIgnoreCase("ME")) + return "Maine"; + else if (shortStateName.equalsIgnoreCase("VT")) + return "Vermont"; + else if (shortStateName.equalsIgnoreCase("CT")) + return "Connecticut"; + else if (shortStateName.equalsIgnoreCase("RI")) + return "Rhode Island"; + else if (shortStateName.equalsIgnoreCase("NY")) + return "New York"; + + return ""; + } + + /* + * return StringBuilder and/or make Writer param and write to stream directly + */ + public static String htmlEncode(String s) { + StringBuilder buf = new StringBuilder(s.length()); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') { + buf.append(c); + } else { + buf.append("&#").append((int) c).append(";"); + } + } + return buf.toString(); + } + + /** + * Gets a byte array from the specified input stream. + * + * @param input + * @return + */ + public static byte[] readStream(InputStream input) { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + int buffer = 0; + + try { + while ((buffer = input.read()) > -1) { + stream.write(buffer); + } + } catch (IOException e) { + USVisaTrackUtils.logger.error("Error converting input stream", e); + } + + return stream.toByteArray(); + } + + /** + * Gets a charset for the specified charset name. + * + * @param charset + * @return + */ + public static Charset getCharset(String charset) { + try { + return Charset.forName(charset); + } catch (Exception ex) { + USVisaTrackUtils.logger.warn("Charset not found: " + charset, ex); + } + + return null; + } + + /** + * Writes the specified data to a file specified by the fileName parameter. + * + * @param fileName + * @param byteArray + */ + public static void writeFile(String fileName, byte[] data) throws IOException { + FileOutputStream output = new FileOutputStream(fileName); + output.write(data); + output.close(); + } + + /** + * Deletes a directory, even if it contains files. + * + * @param path + * @return + */ + public static boolean deleteDir(File path) { + if (path.exists()) { + File[] files = path.listFiles(); + for (int i = 0; i < files.length; i++) { + if (files[i].isDirectory()) { + USVisaTrackUtils.deleteDir(files[i]); + } else { + files[i].delete(); + } + } + } + + return path.delete(); + } + + /** + * Determines whether or not the string is in the specified array. NOTE: This is not intended to be used with large arrays. + * + * @param array + * @param value + * @return + */ + public static boolean contains(String[] array, String value) { + for (String string : array) { + if (USVisaTrackUtils.equals(string, value)) + return true; + } + + return false; + } + + /** + * Convertes a stack trace to string. + * + * @param stackTrace + * @return + */ + public static String getStackTrace(StackTraceElement[] stackTrace) { + StringBuffer buffer = new StringBuffer(); + + for (StackTraceElement element : stackTrace) { + buffer.append("\tat " + element.toString() + "\n"); + } + + return buffer.toString(); + } + + /** + * Formats an exception for logging. + * + * @param ex + * @return + */ + public static String formatException(Throwable ex) { + return ex.getMessage() + "\n" + USVisaTrackUtils.getStackTrace(ex.getStackTrace()); + } + + /** + * Determines whether the first date is before the second. If inclusive is true, then if they are equal it will also return true. This + * + * @param a + * @param b + * @param inclusive + * @return + */ + public static boolean before(Date a, Date b, boolean inclusive) { + if (a == null) + return true; + + if (b == null) + return false; + + return a.before(b) || (inclusive && a.equals(b)); + } + + /** + * Rounds a decimal value to the specified number of places. + * + * @param number + * @param places + * @return + */ + public static double round(double number, int places) { + double power = Math.pow(10, places); + + return (double) (Math.round(number * (power)) / power); + } + + /** + * Rounds a decimal value to the specified number of places. + * + * @param number + * @param places + * @return + */ + public static double round(Float number, int places) { + return round((double) number, places); + } + + /** + * Format the String to clean HTML tag. + * + * @param input + * @return + */ + public static String cleanHtmlTag(String input) { + if (input == null || input.trim().equals("")) { + return ""; + } + + String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", ""); + str = str.replaceAll("[(/>)<]", ""); + + return str; + } + + /** + * Parss HTML to TEXT String + * + * @param inputString + * @return + */ + public static String Html2Text(String inputString) { + String htmlStr = inputString; // + String textStr = ""; + + Pattern p_script; + Matcher m_script; + + Pattern p_style; + Matcher m_style; + + Pattern p_html; + Matcher m_html; + + try { + String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // script + // Regex + // {]*?>[\\s\\S]*?<\\/script> + // } + String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // style + // Regex{]*?>[\\s\\S]*?<\\/style> + // } + String regEx_html = "<[^>]+>"; // HTML Regex + + p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); + m_script = p_script.matcher(htmlStr); + htmlStr = m_script.replaceAll(""); // parse Script tag + + p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); + m_style = p_style.matcher(htmlStr); + htmlStr = m_style.replaceAll(""); // Parse Style tag + + p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); + m_html = p_html.matcher(htmlStr); + htmlStr = m_html.replaceAll(""); // Parse Html tag + + textStr = htmlStr; + + } catch (Exception e) { + System.err.println("Html2Text: " + e.getMessage()); + } + + return textStr;// retrun String + } + + /** + * Check a listing in Polygon or Not + * + * @param points + * @param lat + * @param lon + * @return + */ + public static Boolean checkPointInPolygon(double[][] points, double lat, double lon) { + int i; + int j = points.length - 1; + Boolean inPoly = false; + + // System.out.println("+A+" + + // points[i][0]+(lon-points[i][1])/(points[j][1]-points[i][1])*(points[j][0]-points[i][0])); + + for (i = 0; i < points.length; i++) { + if (points[i][1] < lon && points[j][1] >= lon || points[j][1] < lon && points[i][1] >= lon) { + if ((points[i][0] + (lon - points[i][1]) / (points[j][1] - points[i][1]) * (points[j][0] - points[i][0])) < lat) { + inPoly = !inPoly; + } + } + j = i; + } + return inPoly; + } + + /** + * Get MD5 Hash + * + * @param origin + * @return + */ + public static String getMD5Encode(String origin) { + + StringBuffer sb = new StringBuffer(); + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + + md.update(origin.getBytes()); + + Hasher hasher = Hashing.md5().newHasher(); + hasher.putString(origin, Charsets.UTF_8); + byte[] mdbytes = hasher.hash().asBytes(); + + // convert the byte to hex format method 1 + + for (int i = 0; i < mdbytes.length; i++) { + sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1)); + } + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return sb.toString(); + } + + /** + * Short URL for a long String + * + * @param key + * @param longUrl + * @return + */ + public static String[] getShortUrl(String key, String longUrl) { + String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", + "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", + "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; + + String hex = getMD5Encode(key + longUrl); + int hexLen = hex.length(); + int subHexLen = hexLen / 8; + String[] ShortStr = new String[4]; + + for (int i = 0; i < subHexLen; i++) { + String outChars = ""; + int j = i + 1; + String subHex = hex.substring(i * 8, j * 8); + long idx = Long.valueOf("3FFFFFFF", 16) & Long.valueOf(subHex, 16); + + for (int k = 0; k < 6; k++) { + int index = (int) (Long.valueOf("0000003D", 16) & idx); + outChars += chars[index]; + idx = idx >> 5; + } + ShortStr[i] = outChars; + } + + return ShortStr; + + } + + + + /** + * Replace url from HTTP to HTTPS + * + * + * @param url + * @return + */ + public static String httpsUrl(String url) { + if (StringUtils.startsWithIgnoreCase(url, "http")) { + return StringUtils.replace(url, "http", "https"); + } else + return url; + + } + + /** + * Log Data to DISK. + * + * @param logFilesPath + * @param logFilesName + * @param contentStream + * @throws IOException + */ + public static void logData(String logFilesPath, String logFilesName, OutputStream contentStream) throws IOException { + Date currentDate = new Date(); + + // Create a folder for files. + DateFormat yyyyMM = new SimpleDateFormat("yyyyMM"); + DateFormat dd = new SimpleDateFormat("dd"); + logFilesPath = logFilesPath + "/" + yyyyMM.format(currentDate) + "/" + dd.format(currentDate) + "/" + currentDate.getTime() + "/"; + + // Get file Obj by Path. + File logFile = new File(logFilesPath + logFilesName); + + byte[] data = ((ByteArrayOutputStream) contentStream).toByteArray(); + FileUtils.writeByteArrayToFile(logFile, data); + + } + + /** + * + * @param cDate + * @return + */ + public static String getCDNAttachmentName(File attachmentFile, Date cDate) { + + String filePath = null; + + // Create a folder for files. + DateFormat yyyyMM = new SimpleDateFormat("yyyyMM"); + DateFormat dd = new SimpleDateFormat("dd"); + filePath = yyyyMM.format(cDate) + "/" + dd.format(cDate) + "/" + cDate.getTime(); + + filePath = filePath + "_" + RandomStringUtils.random(12, true, true) + "." + FilenameUtils.getExtension(attachmentFile.getName()); + filePath = StringUtils.lowerCase(filePath); + + return filePath; + + } + + /** + * Wirte String to logFile + * + * @param logFilesPath + * @param logFilesName + * @param content + * @throws IOException + */ + public static void logData(String logFilesPath, String logFilesName, String content) throws IOException { + Date currentDate = new Date(); + + // Create a folder for files. + DateFormat yyyyMM = new SimpleDateFormat("yyyyMM"); + DateFormat dd = new SimpleDateFormat("dd"); + logFilesPath = logFilesPath + "/" + yyyyMM.format(currentDate) + "/" + dd.format(currentDate) + "/" + currentDate.getTime() + "/"; + + // Get file Obj by Path. + File logFile = new File(logFilesPath + logFilesName); + + FileUtils.writeStringToFile(logFile, content); + } + + /** + * Save multiple files as the same time + * + * + * @param logFilesPath + * @param contentStreamMap + * @throws IOException + */ + public static void logData(String logFilesPath, HashMap contentStreamMap) throws IOException { + Date currentDate = new Date(); + + // Create a folder for files. + DateFormat yyyyMM = new SimpleDateFormat("yyyyMM"); + DateFormat dd = new SimpleDateFormat("dd"); + logFilesPath = logFilesPath + "/" + yyyyMM.format(currentDate) + "/" + dd.format(currentDate) + "/" + currentDate.getTime() + "/"; + + // Get file Obj by Path. + Iterator> iter = contentStreamMap.entrySet().iterator(); + while (iter.hasNext()) { + Map.Entry entry = iter.next(); + String key = (String) entry.getKey(); + OutputStream contentStream = (OutputStream) entry.getValue(); + + File logFile = new File(logFilesPath + key); + + byte[] data = ((ByteArrayOutputStream) contentStream).toByteArray(); + FileUtils.writeByteArrayToFile(logFile, data); + + } + + } + + /** + * Save Search OBJ to DISK and CDN + * + * @param cDate + * @param searchId + * @param criteria + * @throws IOException + */ + + /** + * + * @param cDate + * @return + */ + public static String getDateFolderFormat(Date cDate) { + + String folderString = ""; + + DateFormat yyyyMM = new SimpleDateFormat("yyyyMM"); + DateFormat dd = new SimpleDateFormat("dd"); + folderString = folderString + "/" + yyyyMM.format(cDate) + "/" + dd.format(cDate) + "/"; + + return folderString; + } + + /** + * Get Date folder + * + * @param cDate + * @return + */ + public static String getFolderDated(DateTime cDate) { + return cDate.toString("/yyyyMM/dd/"); + } + + /** + * + * @param cDate + * @return + */ + public static String getFolderDated(Date cDate, Integer level) { + String folderString = ""; + DateFormat yyyyMM = new SimpleDateFormat("yyyyMM"); + DateFormat dd = new SimpleDateFormat("dd"); + + if (level.equals(1)) { + folderString = folderString + "/" + yyyyMM.format(cDate) + "/"; + } else { + folderString = folderString + "/" + yyyyMM.format(cDate) + "/" + dd.format(cDate) + "/"; + } + + return folderString; + } + + /** + * + * @param xPathStr + * @param document + * @return + */ + public static String getXMLValueByxPath(String xPathStr, Document document) { + XPathExpression xPath = XPathFactory.instance().compile(xPathStr, Filters.element()); + Element dealsDataElement = xPath.evaluateFirst(document); + + if (dealsDataElement != null) { + return dealsDataElement.getValue(); + } + + return null; + } + + public static Element getXMLFirstElementByxPath(String xPathStr, Document document) { + XPathExpression xPath = XPathFactory.instance().compile(xPathStr, Filters.element()); + Element dataElement = xPath.evaluateFirst(document); + + if (dataElement != null) { + return dataElement; + } + + return null; + } + + /** + * Get HTTP Resource and save to DISK + * + * @param remoteURI + * @param localFile + * @throws ClientProtocolException + * @throws IOException + */ + public static Long saveRemoteFile2Disk(String remoteURI, File localFile) { + + Long fileSize = 0L; + + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpget = new HttpGet(remoteURI); + + try { + logger.debug("Get content from URL: [{}]", httpget.getRequestLine()); + CloseableHttpResponse response = httpclient.execute(httpget); + + try { + HttpEntity entity = response.getEntity(); + + if (response.getStatusLine().getStatusCode() >= 400) { + logger.error("Got bad response, error code = [{}] for imageUrl [{}]", response.getStatusLine().getStatusCode(), + remoteURI); + } + if (entity != null) { + FileUtils.copyInputStreamToFile(entity.getContent(), localFile); + fileSize = entity.getContentLength(); + } + } finally { + response.close(); + } + + } catch (Exception e) { + logger.error("Response Error", e); + + } + + return fileSize; + } + + /** + * + * + * @param upcStr + * @return + */ + public static Integer getUpcCheckDigit(String upcStr) { + // If upcStr not number then return NULL, this is not UPC + if (!StringUtils.isNumeric(upcStr)) { + return null; + } + + // If upcStr not number AND lenght < 8 then return NULL, this is not UPC + if (StringUtils.isNumeric(upcStr) && StringUtils.length(upcStr) < 8) { + + return null; + } + + // UPC, GTIN-8 + if (StringUtils.length(upcStr) == 8) { + + int sumOdd = 0; + sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, 1, 2)); + sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, 3, 4)); + sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, 5, 6)); + + int sumEven = 0; + sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, 0, 1)); + sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, 2, 3)); + sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, 4, 5)); + sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, 6, 7)); + sumEven = 3 * sumEven; + + int checksum_value = sumOdd + sumEven; + int checksum_digit = 10 - (checksum_value % 10); + + if (checksum_digit == 10) { + checksum_digit = 0; + } + + return checksum_digit; + } + // UPC, GTIN-12 + if (StringUtils.length(upcStr) == 12) { + + int sumOdd = 0; + sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, 1, 2)); + sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, 3, 4)); + sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, 5, 6)); + sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, 7, 8)); + sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, 9, 10)); + + int sumEven = 0; + sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, 0, 1)); + sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, 2, 3)); + sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, 4, 5)); + sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, 6, 7)); + sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, 8, 9)); + sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, 10, 11)); + sumEven = 3 * sumEven; + + int checksum_value = sumOdd + sumEven; + int checksum_digit = 10 - (checksum_value % 10); + + if (checksum_digit == 10) { + checksum_digit = 0; + } + + return checksum_digit; + } + // UPC, GTIN-13 + if (StringUtils.length(upcStr) == 13) { + + try { + + String x = StringUtils.left(upcStr, 12); + Integer checksum_digit = Integer.valueOf(EAN13CheckDigit.EAN13_CHECK_DIGIT.calculate(x)); + return checksum_digit; + } catch (CheckDigitException e) { + + logger.debug("Get EAN-13 checksum ERROR" + e); + return null; + } + + // int sumOdd = 0; + // sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, + // 1, 2)); + // sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, + // 3, 4)); + // sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, + // 5, 6)); + // sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, + // 7, 8)); + // sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, + // 9, 10)); + // sumOdd = sumOdd + Integer.valueOf(StringUtils.substring(upcStr, + // 11, 12)); + // + // int sumEven = 0; + // sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, + // 0, 1)); + // sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, + // 2, 3)); + // sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, + // 4, 5)); + // sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, + // 6, 7)); + // sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, + // 8, 9)); + // sumEven = sumEven + Integer.valueOf(StringUtils.substring(upcStr, + // 10, 11)); + // sumEven = 3 * sumEven; + // + // int checksum_value = sumOdd + sumEven; + // int checksum_digit = 10 - (checksum_value % 10); + // + // if (checksum_digit == 10) { + // checksum_digit = 0; + // } + // + // return checksum_digit; + } + + return null; + } + + /** + * Check the GTIN Code correct nor not + * + * @param gtin + * @return + */ + public static boolean isGTIN(String gtin) { + // If upcStr not number then return NULL, this is not UPC + if (!StringUtils.isNumeric(gtin)) { + return false; + } + + // UPC, GTIN + if (StringUtils.length(gtin) == 8 || StringUtils.length(gtin) == 12 || StringUtils.length(gtin) == 13 + || StringUtils.length(gtin) == 14) { + try { + String checksumDigit = StringUtils.right(gtin, 1); + String eanCode = StringUtils.chop(gtin); + String checksumDigitCalculated = EAN13CheckDigit.EAN13_CHECK_DIGIT.calculate(eanCode); + + logger.trace("[{}] / [{}]", gtin, checksumDigitCalculated); + if (StringUtils.equals(checksumDigit, checksumDigitCalculated)) + return true; + } catch (CheckDigitException e) { + logger.trace("Get EAN-13 checksum ERROR" + e); + } + } + return false; + } + + /** + * Get price for Float + * + * @param priceStr + * @return + */ + public static Float getPrice(String priceStr) { + if (StringUtils.isNotBlank(priceStr)) { + if (StringUtils.contains(priceStr, ".")) { + return Float.valueOf(priceStr); + } else { + return Float.valueOf(Integer.valueOf(priceStr) / 100); + } + } else { + return 0.0f; + } + } + + + public static BufferedImage cropAndResize(File imageFile, int targetWidth, int targetHeight) { + + ImagePlus imp = IJ.openImage(imageFile.getAbsolutePath()); + + ImageProcessor ip = imp.getProcessor(); + System.out.println("size1: " + ip.getWidth() + "x" + ip.getHeight()); + ip.setInterpolationMethod(ImageProcessor.BILINEAR); + ip = ip.resize(targetWidth * 2, targetHeight * 2); + System.out.println("size2: " + ip.getWidth() + "x" + ip.getHeight()); + + int cropX = ip.getWidth() / 2; + int cropY = ip.getHeight() / 2; + ip.setRoi(cropX, cropY, targetWidth, targetHeight); + ip = ip.crop(); + System.out.println("size3: " + ip.getWidth() + "x" + ip.getHeight()); + BufferedImage croppedImage = ip.getBufferedImage(); + + System.out.println("size4: " + ip.getWidth() + "x" + ip.getHeight()); + new ImagePlus("croppedImage", croppedImage).show(); + + return croppedImage; + + } + + /** + * Get http response as string + * + * @param response + * @return + */ + public static String convertHttpResponse2String(HttpResponse response) { + + logger.debug("Response Code : " + response.getStatusLine().getStatusCode()); + + String responseStr = null; + + HttpEntity entity = response.getEntity(); + + if (entity != null) { + // Read content + try { + InputStream instream = entity.getContent(); + + StringWriter writer = new StringWriter(); + IOUtils.copy(instream, writer, "UTF-8"); + responseStr = writer.toString(); + } catch (Exception e) { + logger.error("Read http response Error", e); + } + + } + + // logger.debug("R {}", responseStr); + + return responseStr; + + } + + /** + * Get http response as string + * + * @param response + * @return + * @throws IOException + */ + public static void writeDom2File(File docFile, Document dom) throws IOException { + + logger.debug("Write Document to disk"); + + // new XMLOutputter().output(doc, System.out); + XMLOutputter xmlOutput = new XMLOutputter(); + + // display nice nice + xmlOutput.setFormat(Format.getPrettyFormat()); + + FileUtils.writeStringToFile(docFile, xmlOutput.outputString(dom)); + // System.out.println("SSS" + xmlReques); + + } + + /** + * Convert Document to String + * + * @param dom + * @return + * @throws IOException + */ + public static String convertDom2String(Document dom) { + logger.debug("Convert Document to string"); + + XMLOutputter xmlOutput = new XMLOutputter(); + xmlOutput.setFormat(Format.getPrettyFormat()); + + return xmlOutput.outputString(dom); + + } +} \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/common/data/DataCrawlStatus.java b/core/src/main/java/com/usvisatrack/core/common/data/DataCrawlStatus.java new file mode 100644 index 0000000..c2c17f0 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/common/data/DataCrawlStatus.java @@ -0,0 +1,11 @@ +package com.usvisatrack.core.common.data; + +/** + * DataCrawlStatus OBJ + * + * @author YuCheng Hu + * + */ +public enum DataCrawlStatus { + ACCEPTED, NOTUPC, BADUPC, DUPLICATEDUPC, NORESULTS +} \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/common/search/Facet.java b/core/src/main/java/com/usvisatrack/core/common/search/Facet.java new file mode 100644 index 0000000..6a75f0d --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/common/search/Facet.java @@ -0,0 +1,46 @@ +package com.usvisatrack.core.common.search; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author YuCheng Hu + * + *Represents a facet returned from the search back end. Facets have many values, and each value has a count of the number + * of results that have the facet value. + */ +public class Facet { + private List values = new ArrayList(); + private String name = ""; + private String displayName = ""; + + public Facet() { + + } + + public Facet(String name, String displayName) { + this.name = name; + this.displayName = displayName; + } + + public List getValues() { + return values; + } + + public String getName() { + return name; + } + + public String getDisplayName() { + return displayName; + } + + public void addValue(FacetValue value) { + this.values.add(value); + } + + public void setValues(List values) { + this.values = values; + } +} diff --git a/core/src/main/java/com/usvisatrack/core/common/search/FacetValue.java b/core/src/main/java/com/usvisatrack/core/common/search/FacetValue.java new file mode 100644 index 0000000..aff5e0c --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/common/search/FacetValue.java @@ -0,0 +1,34 @@ +package com.usvisatrack.core.common.search; + +/** + * + * @author YuCheng Hu + * + */ +public class FacetValue { + private String value = ""; + private String displayValue = ""; + private long count = 0; + + public FacetValue() { + + } + + public FacetValue(String value, String displayValue, long count) { + this.value = value; + this.displayValue = displayValue; + this.count = count; + } + + public String getValue() { + return value; + } + + public long getCount() { + return count; + } + + public String getDisplayValue() { + return displayValue; + } +} diff --git a/core/src/main/java/com/usvisatrack/core/common/search/InternalSearch.java b/core/src/main/java/com/usvisatrack/core/common/search/InternalSearch.java new file mode 100644 index 0000000..025e905 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/common/search/InternalSearch.java @@ -0,0 +1,81 @@ +package com.usvisatrack.core.common.search; + +import com.usvisatrack.core.common.DataObject; + +/** + * + * @author YuCheng Hu + * + */ +public class InternalSearch extends DataObject { + /** + * + */ + private static final long serialVersionUID = 1L; + + private Long id = 0L; + + private String name = ""; + private String resultType = ""; + private String searchFields = ""; + + /** + * @return the id + */ + public Long getId() { + return id; + } + + /** + * @param id + * the id to set + */ + public void setId(Long id) { + this.id = id; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name + * the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the resultType + */ + public String getResultType() { + return resultType; + } + + /** + * @param resultType + * the resultType to set + */ + public void setResultType(String resultType) { + this.resultType = resultType; + } + + /** + * @return the labelField + */ + public String getSearchFields() { + return searchFields; + } + + /** + * @param labelField + * the labelField to set + */ + public void setSearchFields(String labelField) { + this.searchFields = labelField; + } +} diff --git a/core/src/main/java/com/usvisatrack/core/common/search/InternalSearchCriteria.java b/core/src/main/java/com/usvisatrack/core/common/search/InternalSearchCriteria.java new file mode 100644 index 0000000..cb02ad4 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/common/search/InternalSearchCriteria.java @@ -0,0 +1,60 @@ +package com.usvisatrack.core.common.search; + +public class InternalSearchCriteria { + private String text = ""; + private int pageSize = 25; + private int pageNumber = 0; + + public InternalSearchCriteria() { + + } + + public InternalSearchCriteria(String text) { + this.text = text; + } + + /** + * @return the text + */ + public String getText() { + return text; + } + + /** + * @param text + * the text to set + */ + public void setText(String text) { + this.text = text; + } + + /** + * @return the pageSize + */ + public int getPageSize() { + return pageSize; + } + + /** + * @param pageSize + * the pageSize to set + */ + public void setPageSize(int pageSize) { + this.pageSize = pageSize; + } + + /** + * @return the pageNumber + */ + public int getPageNumber() { + return pageNumber; + } + + /** + * @param pageNumber + * the pageNumber to set + */ + public void setPageNumber(int pageNumber) { + this.pageNumber = pageNumber; + } +} diff --git a/core/src/main/java/com/usvisatrack/core/dao/model/EntityListener.java b/core/src/main/java/com/usvisatrack/core/dao/model/EntityListener.java new file mode 100644 index 0000000..2de02e3 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/dao/model/EntityListener.java @@ -0,0 +1,21 @@ +package com.usvisatrack.core.dao.model; + +import java.util.Date; + +import javax.persistence.PrePersist; +import javax.persistence.PreUpdate; + +import com.usvisatrack.core.common.DataObject; + +public class EntityListener { + @PrePersist + public void prePersist(DataObject entity) { + entity.setCreateDate(new Date()); + entity.setModifyDate(new Date()); + } + + @PreUpdate + public void preUpdate(DataObject entity) { + entity.setModifyDate(new Date()); + } +} diff --git a/core/src/main/java/com/usvisatrack/core/dao/model/User.java b/core/src/main/java/com/usvisatrack/core/dao/model/User.java new file mode 100644 index 0000000..b576540 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/dao/model/User.java @@ -0,0 +1,100 @@ + +package com.usvisatrack.core.dao.model; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import com.usvisatrack.core.common.DataObject; + +/** + * Merchant generated by hbm2java + */ +public class User extends DataObject implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -8094138967079219902L; + + private String email; + private String userName; + private String password; + private String apiKey; + private String apiToken; + private String firstName; + private String lirstName; + private Date dateReg; + + + + public User() { + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getApiKey() { + return apiKey; + } + + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + + public String getApiToken() { + return apiToken; + } + + public void setApiToken(String apiToken) { + this.apiToken = apiToken; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLirstName() { + return lirstName; + } + + public void setLirstName(String lirstName) { + this.lirstName = lirstName; + } + + public Date getDateReg() { + return dateReg; + } + + public void setDateReg(Date dateReg) { + this.dateReg = dateReg; + } + + + +} diff --git a/core/src/main/java/com/usvisatrack/core/dao/model/Visa.java b/core/src/main/java/com/usvisatrack/core/dao/model/Visa.java new file mode 100644 index 0000000..71c64e0 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/dao/model/Visa.java @@ -0,0 +1,144 @@ +package com.usvisatrack.core.dao.model; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.lang3.StringUtils; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.usvisatrack.core.common.DataObject; +import com.usvisatrack.core.common.USVisaTrackConstant.ItemCodeType; + +/** + * + * @author YuCheng Hu + * + */ +public class Visa extends DataObject implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = -7563954515794041068L; + private List upcList; + private String gtin; + private String itemCode; + + private String itemName; + private String price; + private String itemDescription = null; + + + + public Visa() { + + } + + /* + * (non-Javadoc) + * + * @see com.verani.common.DataObject#getId() + */ + + public List getUpcList() { + return upcList; + } + + public String getGtin() { + return gtin; + } + + public void setGtin(String gtin) { + this.gtin = gtin; + } + + public void setUpcList(List upcList) { + this.upcList = upcList; + } + + public String getItemCode() { + return itemCode; + } + + public void setItemCode(String itemCode) { + this.itemCode = itemCode; + } + + + public String getItemName() { + return itemName; + } + + public void setItemName(String itemName) { + this.itemName = itemName; + } + + public String getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } + + // public List getItemPrices() { + // return itemPrices; + // } + // + // public void setItemPrices(List itemPrices) { + // this.itemPrices = itemPrices; + // } + + public String getItemDescription() { + return itemDescription; + } + + public void setItemDescription(String itemDescription) { + this.itemDescription = itemDescription; + } + + + + public void updateItemCode(ItemCodeType itemCodeType, String itemCodeValue) { + + Map itemCodeMap = new HashMap<>(); + if (StringUtils.isBlank(this.itemCode)) { + itemCodeMap.put(ItemCodeType.SKU.name(), ""); + itemCodeMap.put(ItemCodeType.ASIN.name(), ""); + itemCodeMap.put(ItemCodeType.BBYSKU.name(), ""); + } else { + + Gson gson = new Gson(); + Type stringStringMap = new TypeToken>() { + }.getType(); + itemCodeMap = gson.fromJson(this.itemCode, stringStringMap); + + } + + itemCodeMap.put(itemCodeType.name(), itemCodeValue); + this.itemCode = new Gson().toJson(itemCodeMap); + } + // public List getItemPriceWebList() { + // return itemPriceWebList; + // } + // + // public void setItemPriceWebList(List itemPriceWebList) { + // this.itemPriceWebList = itemPriceWebList; + // } + + // public ItemNutrition getItemNutrition() { + // return itemNutrition; + // } + // + // public void setItemNutrition(ItemNutrition itemNutrition) { + // this.itemNutrition = itemNutrition; + // } + +} diff --git a/core/src/main/java/com/usvisatrack/core/factories/ConfFactory.java b/core/src/main/java/com/usvisatrack/core/factories/ConfFactory.java new file mode 100644 index 0000000..14f7223 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/factories/ConfFactory.java @@ -0,0 +1,22 @@ +package com.usvisatrack.core.factories; + +import java.util.List; + +import org.hibernate.Criteria; +import org.hibernate.Transaction; +import org.hibernate.criterion.Restrictions; +import org.hibernate.type.LongType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + + +public class ConfFactory { + private static final int LISTING_PAGE_SIZE = 20; + public static final int DEFAULT_AUTH_EXPIRATION = 10; + + private static Logger logger = LoggerFactory.getLogger(ConfFactory.class); + + + +} \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/factories/DataFactory.java b/core/src/main/java/com/usvisatrack/core/factories/DataFactory.java new file mode 100644 index 0000000..023b030 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/factories/DataFactory.java @@ -0,0 +1,18 @@ +package com.usvisatrack.core.factories; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * + * @author YuCheng Hu + * + */ +public class DataFactory { + // private static final int LISTING_PAGE_SIZE = 20; + public static final int DEFAULT_AUTH_EXPIRATION = 10; + private static Logger logger = LoggerFactory.getLogger(DataFactory.class); + + +} \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/factories/LogFactory.java b/core/src/main/java/com/usvisatrack/core/factories/LogFactory.java new file mode 100644 index 0000000..54a81da --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/factories/LogFactory.java @@ -0,0 +1,22 @@ +package com.usvisatrack.core.factories; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.usvisatrack.core.common.Factory; + +/** + * Item Data Factory + * + * @author YuCheng Hu + * + */ +public class LogFactory extends Factory { + private static final int LISTING_PAGE_SIZE = 20; + public static final int DEFAULT_AUTH_EXPIRATION = 10; + + private static Logger logger = LoggerFactory.getLogger(LogFactory.class); + + + +} \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/factories/UserFactory.java b/core/src/main/java/com/usvisatrack/core/factories/UserFactory.java new file mode 100644 index 0000000..9cad908 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/factories/UserFactory.java @@ -0,0 +1,23 @@ +package com.usvisatrack.core.factories; + +import java.util.List; + +import org.hibernate.Criteria; +import org.hibernate.Transaction; +import org.hibernate.criterion.Order; +import org.hibernate.criterion.Restrictions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.usvisatrack.core.common.Factory; + + +public class UserFactory { + private static final int LISTING_PAGE_SIZE = 20; + public static final int DEFAULT_AUTH_EXPIRATION = 10; + + private static Logger logger = LoggerFactory.getLogger(UserFactory.class); + + + +} \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/factories/VisaFactory.java b/core/src/main/java/com/usvisatrack/core/factories/VisaFactory.java new file mode 100644 index 0000000..ddd93fb --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/factories/VisaFactory.java @@ -0,0 +1,32 @@ +package com.usvisatrack.core.factories; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.Criteria; +import org.hibernate.Transaction; +import org.hibernate.criterion.Order; +import org.hibernate.criterion.Restrictions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +import com.usvisatrack.core.common.Factory; + + +/** + * Item Data Factory + * + * @author YuCheng Hu + * + */ +public class VisaFactory extends Factory { + private static final int LISTING_PAGE_SIZE = 20; + + public static final int DEFAULT_AUTH_EXPIRATION = 10; + + private static Logger logger = LoggerFactory.getLogger(VisaFactory.class); + + + +} \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/service/ItemCodeService.java b/core/src/main/java/com/usvisatrack/core/service/ItemCodeService.java new file mode 100644 index 0000000..5b8bf27 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/service/ItemCodeService.java @@ -0,0 +1,23 @@ +package com.usvisatrack.core.service; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Item Service + * + * @author YuCheng Hu + * + */ +public class ItemCodeService { + + private static final Logger logger = LoggerFactory.getLogger(ItemCodeService.class); + + + +} \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/service/ItemImageService.java b/core/src/main/java/com/usvisatrack/core/service/ItemImageService.java new file mode 100644 index 0000000..161c219 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/service/ItemImageService.java @@ -0,0 +1,24 @@ +package com.usvisatrack.core.service; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Item Service + * + * @author YuCheng Hu + * + */ +public class ItemImageService { + + private static final Logger logger = LoggerFactory.getLogger(ItemImageService.class); + + +} \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/service/ItemService.java b/core/src/main/java/com/usvisatrack/core/service/ItemService.java new file mode 100644 index 0000000..b0e68db --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/service/ItemService.java @@ -0,0 +1,23 @@ +package com.usvisatrack.core.service; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Item Service + * + * @author YuCheng Hu + * + */ +public class ItemService { + + private static final Logger logger = LoggerFactory.getLogger(ItemService.class); + + +} \ No newline at end of file diff --git a/core/src/main/java/com/usvisatrack/core/utils/AmazonUtils.java b/core/src/main/java/com/usvisatrack/core/utils/AmazonUtils.java new file mode 100644 index 0000000..75a3aa5 --- /dev/null +++ b/core/src/main/java/com/usvisatrack/core/utils/AmazonUtils.java @@ -0,0 +1,77 @@ +package com.usvisatrack.core.utils; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.collections.ListUtils; +import org.apache.commons.httpclient.util.URIUtil; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.apache.commons.math3.util.Precision; +import org.apache.http.client.ClientProtocolException; +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.Namespace; +import org.jdom2.filter.Filters; +import org.jdom2.input.SAXBuilder; +import org.jdom2.output.Format; +import org.jdom2.output.XMLOutputter; +import org.jdom2.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.ImmutableSet; +import com.google.gson.Gson; + + +/** + * BcoDepot Amazon Utils class + * + * @author YuCheng Hu + * + */ +public final class AmazonUtils { + + private static final Logger logger = LoggerFactory.getLogger(AmazonUtils.class); + + // private static String APIDATA_CDN_FOLDER = "/home/cdn/com-bcodepot-www/data/apidata"; + + private static String AWS_ACCESS_KEY_ID = ""; + private static String AWS_SECRET_KEY = ""; + private static String ENDPOINT = "ecs.amazonaws.com"; + private static String AWS_NAME = "AmazonMerchandiseData"; + + private static String AMAZON_NAME = "Amazon"; + + public final static String CACHE_DATA_FOLDER = "/home/bcodepot/cache"; + + public final static String API_DATA_FOLDER = "/home/bcodepot/api"; + + public final static String CDN_DATA_FOLDER = "/home/cdn/com-bcodepot-www/data/item"; + + public final static String ITEM_ATTACHMENT_CACHE_FOLDER = "/home/bcodepot/cache/" + AMAZON_NAME; + + public final static String ITEM_ATTACHMENT_CDN_FOLDER = "/home/cdn/com-bcodepot-www/data/item"; + + public final static Date C_DATE = new Date(); + + private static final Namespace AMAZON_NAMESPACE = Namespace.getNamespace("Amazon", + "http://webservices.amazon.com/AWSECommerceService/2011-08-01"); + + + +} \ No newline at end of file diff --git a/core/src/main/resources/hbm/User.hbm.xml b/core/src/main/resources/hbm/User.hbm.xml new file mode 100644 index 0000000..f8084a1 --- /dev/null +++ b/core/src/main/resources/hbm/User.hbm.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/core/src/main/resources/hbm/Visa.hbm.xml b/core/src/main/resources/hbm/Visa.hbm.xml new file mode 100644 index 0000000..6ecee53 --- /dev/null +++ b/core/src/main/resources/hbm/Visa.hbm.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/core/src/main/resources/hibernate.cfg.xml b/core/src/main/resources/hibernate.cfg.xml new file mode 100644 index 0000000..b98d024 --- /dev/null +++ b/core/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,36 @@ + + + + + + + + + + 20 + false + 20 + thread + + + + org.hibernate.connection.C3P0ConnectionProvider + org.hibernate.cache.EhCacheProvider + 1 + 30 + 30 + 0 + 100 + 6 + + SELECT 1 + true + + + + + + + + diff --git a/core/src/main/resources/log4j.properties b/core/src/main/resources/log4j.properties new file mode 100644 index 0000000..216801b --- /dev/null +++ b/core/src/main/resources/log4j.properties @@ -0,0 +1,41 @@ +# - ------------------------------------------------------------------------------------------------ +# - L O G L E V E L +# - ------------------------------------------------------------------------------------------------ +log4j.logger.org.hibernate=WARN,production,console +log4j.logger.com.upcex=INFO,production,console +log4j.logger.com.mchange=WARN,production,console + +# - ------------------------------------------------------------------------------------------------ +# - S E R V E R S T A T U S C H E C K A P P E N D E R +# - ------------------------------------------------------------------------------------------------ +log4j.appender.ServerStatusCheck=org.apache.log4j.RollingFileAppender +log4j.appender.ServerStatusCheck.encoding=UTF-8 +log4j.appender.ServerStatusCheck.Threshold=INFO +log4j.appender.ServerStatusCheck.File=/home/bcodepot/logs/services/serverstatuscheck/serverstatuscheck.log +log4j.appender.ServerStatusCheck.MaxFileSize=200MB +log4j.appender.ServerStatusCheck.MaxBackupIndex=7 +log4j.appender.ServerStatusCheck.layout=org.apache.log4j.PatternLayout +log4j.appender.ServerStatusCheck.layout.ConversionPattern=%-d{yyyy/MM/dd HH:mm:ss} %-5p [%c] - %m%n + +# - ------------------------------------------------------------------------------------------------ +# - P R O D U C T I O N E N V I R O N M E N T A P P E N D E R +# - ------------------------------------------------------------------------------------------------ +log4j.appender.production=org.apache.log4j.RollingFileAppender +log4j.appender.production.encoding=UTF-8 +log4j.appender.production.Threshold=INFO +log4j.appender.production.File=/home/bcodepot/logs/services/services.log +log4j.appender.production.MaxFileSize=200MB +log4j.appender.production.MaxBackupIndex=7 +log4j.appender.production.layout=org.apache.log4j.PatternLayout +log4j.appender.production.layout.ConversionPattern=%-d{yyyy/MM/dd HH:mm:ss} %-5p [%c] - %m%n + +# - ------------------------------------------------------------------------------------------------ +# - C O N S O L E A P P E N D E R +# - ------------------------------------------------------------------------------------------------ +log4j.appender.console=org.apache.log4j.ConsoleAppender +log4j.appender.console.target=System.out +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.Threshold=INFO +log4j.appender.console.layout.ConversionPattern=%-d{yyyy/MM/dd HH:mm:ss} %-5p [%c] - %m%n + + diff --git a/core/src/main/resources/log4j2.xml b/core/src/main/resources/log4j2.xml new file mode 100644 index 0000000..a68c6e9 --- /dev/null +++ b/core/src/main/resources/log4j2.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core/src/test/java/com/usvisatrack/core/test/CloudTest.java b/core/src/test/java/com/usvisatrack/core/test/CloudTest.java new file mode 100644 index 0000000..c554a20 --- /dev/null +++ b/core/src/test/java/com/usvisatrack/core/test/CloudTest.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF). + */ +package com.usvisatrack.core.test; + +import junit.framework.TestCase; + +import org.junit.After; +import org.junit.Before; + +import com.usvisatrack.core.common.Factory; + + +/** + * + * @author YuCheng Hu + * + */ +public class CloudTest extends TestCase { + + @Before + @Override + protected void setUp() throws Exception { + Factory.beginTransaction(); + } + + @After + @Override + protected void tearDown() throws Exception { + Factory.rollbackTransaction(); + } + +// @Test +// public void testUploadFolder() { +// try { +// CloudFiles.uploadDirectoryContentsToCloud("d:\\CDN\\Cloud2", "production-logs"); +// +// } catch (Exception ex) { +// Factory.rollbackTransaction(); +// ex.printStackTrace(); +// Assert.fail(); +// } +// } + +// @Test +// public void testGetFile() { +// try { +// CloudFiles.getCDNSearchSerializeObj(); +// +// } catch (Exception ex) { +// Factory.rollbackTransaction(); +// ex.printStackTrace(); +//// Assert.fail(); +// } +// } + +} diff --git a/core/src/test/java/com/usvisatrack/core/test/VisaTest.java b/core/src/test/java/com/usvisatrack/core/test/VisaTest.java new file mode 100644 index 0000000..005ba0c --- /dev/null +++ b/core/src/test/java/com/usvisatrack/core/test/VisaTest.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF). + */ +package com.usvisatrack.core.test; + +import java.util.List; + +import junit.framework.TestCase; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author YuCheng Hu + * + */ +public class VisaTest extends TestCase { + + private final static Logger logger = LoggerFactory.getLogger(VisaTest.class); + + @Test + public void testItemByGTIN() throws Exception { + // String gtin = "6902253867314"; + // ItemCode itemCode = ItemFactory.getItemCodeByGTIN(gtin); + // + // logger.debug("Item Name {}", itemCode.getItem().getName()); + // logger.debug("attachment Size {}", itemCode.getItem().getItemAttachments().size()); + // logger.debug("Price Size {}", itemCode.getItem().getItemPrices().size()); + // logger.debug("Accese URL {}", itemCode.getItem().getItemPrices().get(0).getAccessURL()); + + // List itemPriceList = ItemFactory.getNewestItemPriceList(394L); + + // logger.debug("Accese URL {}", itemPriceList.size()); + + } + + @Test + public void testDealsSave() throws Exception { + + } + + @Before + @Override + protected void setUp() throws Exception { + + } + + @After + @Override + protected void tearDown() throws Exception { + + } + +} diff --git a/core/src/test/resources/hibernate.properties b/core/src/test/resources/hibernate.properties new file mode 100644 index 0000000..256c398 --- /dev/null +++ b/core/src/test/resources/hibernate.properties @@ -0,0 +1,14 @@ +# JDBC Connection +hibernate.connection.driver_class=com.mysql.jdbc.Driver +hibernate.dialect=org.hibernate.dialect.MySQL5Dialect +hibernate.connection.url=jdbc:mysql://db.upcex.com/ + +# Username and Password +hibernate.connection.username=upcex.web +hibernate.connection.password=026l64e4rtfg + +# Performance +#hibernate.connection.pool.size=20 +#hibernate.connection.autocommit=true +#hibernate.jdbc.batch_size=20 +#hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext \ No newline at end of file diff --git a/core/src/test/resources/log4j2.xml b/core/src/test/resources/log4j2.xml new file mode 100644 index 0000000..a68c6e9 --- /dev/null +++ b/core/src/test/resources/log4j2.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file