Subversion Repositories SmartDukaan

Rev

Rev 9280 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.util;

import in.shop2020.metamodel.core.Entity;
import in.shop2020.metamodel.core.FreeformContent;
import in.shop2020.metamodel.core.Media;
import in.shop2020.metamodel.definitions.Category;
import in.shop2020.metamodel.definitions.DefinitionsContainer;
import in.shop2020.metamodel.util.ExpandedEntity;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class EntityUtils {
    /**
     * Get the name of the product from entity. It considers null values also
     * @param expEntity
     * @return
     */
        public static String getProductName(Entity expEntity){
                //FIXME Use stringbuilder
                String productName = ((expEntity.getBrand() != null) ? expEntity.getBrand().trim() + " " : "")
                + ((expEntity.getModelName() != null) ? expEntity.getModelName().trim() + " " : "")
                + (( expEntity.getModelNumber() != null ) ? expEntity.getModelNumber().trim() : "" );   
                return productName;
        }

        /**
         * Get url of the entity. 
         * @param expEntity
         * @return
         */
        public static String getEntityURL(ExpandedEntity expEntity){
                String url = "/" + expEntity.getCategory().getParentCategory().getLabel().toLowerCase().replace(' ', '-') + "/";
                if(expEntity.getCategory().getParentCategory().getID() == Utils.ROOT_CATAGOEY){
                        url = "/" + expEntity.getCategory().getLabel().toLowerCase().replace(' ', '-') + "/";
                }
                
                String productName = getProductName(expEntity);
                String productUrl = productName.toLowerCase().replace(' ', '-') + "-" + expEntity.getID();
                productUrl = productUrl.replaceAll("/", "-");
                url = url + productUrl;
                url = url.replaceAll("-+", "-");
                return url;
        }

        /**
         * Get url of the entity without beginSlash. 
         * @param expEntity
         * @return
         */
        public static String getEntityNoSlashURL(ExpandedEntity expEntity){
                String url = expEntity.getCategory().getParentCategory().getLabel().toLowerCase().replace(' ', '-') + "/";
                if(expEntity.getCategory().getParentCategory().getID() == Utils.ROOT_CATAGOEY){
                        url = expEntity.getCategory().getLabel().toLowerCase().replace(' ', '-') + "/";
                }
                
                String productName = getProductName(expEntity);
                String productUrl = productName.toLowerCase().replace(' ', '-') + "-" + expEntity.getID();
                productUrl = productUrl.replaceAll("/", "-");
                url = url + productUrl;
                url = url.replaceAll("-+", "-");
                return url;
        }
        
        /**
         * Gets media prefix to be used for naming files
         *
         * @param expEntity
         * @return
         */
        public static String getMediaPrefix(Entity expEntity){
                String productName = getProductName(expEntity);
                String mediaPrefix = productName.toLowerCase().replace(' ', '-');
                mediaPrefix = mediaPrefix.replaceAll("/", "-");
                mediaPrefix = mediaPrefix.replaceAll("-+", "-");
                return mediaPrefix;
        }

        /**
         * Returns the creation timestamp of the media with a given label
         * in summary slide.
         *
         * @param expEntity
         * @return
         */
    public static long getCreationTimeFromSummarySlide(ExpandedEntity expEntity, String label) {
        long defaultSuffix = 0;

        if (expEntity != null && expEntity.getSlide(Utils.SUMMARY_SLIDE_DEFINITION_ID) != null) {
            FreeformContent content = expEntity.getSlide(Utils.SUMMARY_SLIDE_DEFINITION_ID).getFreeformContent();
            if (content != null) {
                Map<String, Media> media = content.getMedias();
                if (media != null) {
                    Media defaultImage = media.get(label);
                    if (defaultImage != null) {
                        defaultSuffix = defaultImage.getCreationTime().getTime();
                    }
                }
            }
        }

        return defaultSuffix;
    }
    
    public static String getTitle(Entity entity){
        String brand = entity.getBrand();
        String modelName = entity.getModelName();
        String modelNumber = entity.getModelNumber();
        String titleString = "";
        if(StringUtils.isEmpty(modelName)){
                titleString = brand + " " + modelNumber + " Price in India | Specifications, Features and Reviews";
        }else {
                titleString = brand + " " + modelName  + " " + modelNumber +" Price in India | Specifications, Features and Reviews";
        }
        return titleString;
    }
    
    public static String getMetaDescription(Entity entity, DefinitionsContainer defs){
        Category cat = defs.getCategory(entity.getCategoryID());
        Category parentCategory = cat.getParentCategory();
        String categoryName = parentCategory.getLabel();
        if (parentCategory.getID()== Utils.MOBILE_ACCESSORIES_CATEGORY){
                categoryName = cat.getLabel();
        }
        String template = "Buy {0} {1} {2} at $minPriceItem.getSellingPrice(). All {3} are 100% Original and carry full Manufacturers Warranty since we procure directly from the Brand. Free Next Day Delivery.";
        return MessageFormat.format(template, entity.getBrand(), entity.getModelName(), entity.getModelNumber(), categoryName.toLowerCase());
    }
}