gov.wa.wsdot.cms.templates.ProjectTemplate.java Source code

Java tutorial

Introduction

Here is the source code for gov.wa.wsdot.cms.templates.ProjectTemplate.java

Source

/*
 * Copyright (c) 2015 Washington State Department of Transportation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package gov.wa.wsdot.cms.templates;

import gov.wa.wsdot.cms.Template;
import gov.wa.wsdot.cms.shared.ChannelsAndPostingsBase;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class ProjectTemplate implements Template {

    @Override
    public ChannelsAndPostingsBase build(ChannelsAndPostingsBase item, HashMap<String, String> controlsMap,
            HashMap<String, String> genericPropertiesMap) {

        StringBuilder leftNav = new StringBuilder();
        StringBuilder update = new StringBuilder();
        StringBuilder pageContent = new StringBuilder();
        StringBuilder contact = new StringBuilder();
        StringBuilder map = new StringBuilder();
        List<String> fundingList = new ArrayList<String>();

        MongoClient mongo = null;
        try {
            mongo = new MongoClient("localhost");
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        DB db = mongo.getDB("projects");
        DBCollection projectsCollection = db.getCollection("posts");
        BasicDBObject query = new BasicDBObject("Link", "/" + item.getLocation().replace(">>", "/") + "/");
        DBCursor cursor = projectsCollection.find(query);

        try {
            while (cursor.hasNext()) {
                DBObject dbObject = cursor.next();
                item.setProjectId(dbObject.get("ID").toString());
                item.setCounties(dbObject.get("County").toString());
                item.setRegions(dbObject.get("Region").toString());
                item.setHighways(getHighwayTaxonomy(dbObject.get("Highway").toString()));

                if (!dbObject.get("Nickel").toString().equalsIgnoreCase("0")) {
                    fundingList.add("Nickel");
                } else if (!dbObject.get("Partner").toString().equalsIgnoreCase("0")) {
                    fundingList.add("TPA");
                } else if (!dbObject.get("FedStim").toString().equalsIgnoreCase("0")) {
                    fundingList.add("ARRA");
                }
            }

            String[] fundingArray = fundingList.toArray(new String[fundingList.size()]);
            item.setFunding(Arrays.toString(fundingArray).replace("[", "").replace("]", "").replace(",", ";"));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            cursor.close();
            mongo.close();
        }

        // This can be concatenated into the leftNav field
        leftNav.append("");
        if (controlsMap.get("LeftNav") != null) {
            leftNav.append(controlsMap.get("LeftNav"));
        }
        if (controlsMap.get("LeftNavPlaceholderDefinition2") != null) {
            leftNav.append(controlsMap.get("LeftNavPlaceholderDefinition2"));
        }
        item.setLeftNav(leftNav.toString());

        // All this can be concatenated into the projectUpdate field
        update.append("<p>");
        update.append("<strong>" + controlsMap.get("ProjectUpdateDate") + "</strong>");
        update.append("</p>");
        update.append(controlsMap.get("ProjectUpdate"));
        item.setUpdate(update.toString());

        // All this can be concatenated into the pageContent field
        if (controlsMap.get("OverviewDefinition") != null) {
            pageContent.append("<p>");
            pageContent.append(controlsMap.get("OverviewDefinition"));
            pageContent.append("</p>");
        }
        if (controlsMap.get("What") != null) {
            pageContent.append("<p>");
            pageContent.append("<strong>Why is WSDOT " + controlsMap.get("What") + "</strong>");
            pageContent.append("<br />");
            pageContent.append(controlsMap.get("Why"));
            pageContent.append("</p>");
        }
        if (controlsMap.get("EndResult") != null) {
            pageContent.append("<p>");
            pageContent.append("<strong>The End Result</strong>");
            pageContent.append("<br />");
            pageContent.append(controlsMap.get("EndResult"));
            pageContent.append("</p>");
        }
        if (controlsMap.get("ProjectBenefits") != null) {
            pageContent.append("<p>");
            pageContent.append("<strong>Project Benefits</strong>");
            pageContent.append("<br />");
            pageContent.append(controlsMap.get("ProjectBenefits"));
            pageContent.append("</p>");
        }
        if (controlsMap.get("ProjectTimelines") != null) {
            pageContent.append("<p>");
            pageContent.append("<strong>What is the project timeline?</strong>");
            pageContent.append("<br />");
            pageContent.append(controlsMap.get("ProjectTimelines"));
            pageContent.append("</p>");
        }
        if (controlsMap.get("Financial") != null) {
            pageContent.append("<p>");
            pageContent.append("<strong>Financial Information</strong>");
            pageContent.append("<br />");
            pageContent.append(controlsMap.get("Financial"));
            pageContent.append("</p>");
        }

        item.setPageContent(pageContent.toString());

        // This will be separate and go into contact field
        if (controlsMap.get("Contact") != null) {
            contact.append("<p>");
            contact.append("<strong>How can I get more information?<br />Contact:</strong><br />");
            contact.append(controlsMap.get("Contact"));
            contact.append("</p>");
        }
        item.setContact(contact.toString());

        item.setPhase(getProjectPhaseTaxonomy(controlsMap.get("PhaseImage")));

        // This can be concatenated into the projectMap field
        if (controlsMap.get("MapPlaceholder") != null) {
            map.append(controlsMap.get("MapPlaceholder"));
        }
        // FlickrLightbox <- Ignore for now
        // This appears under Map -> [Flickr Photos] -> RightPlaceHolder
        if (controlsMap.get("RightPlaceHolder") != null) {
            map.append(controlsMap.get("RightPlaceHolder"));
        }
        item.setLegacyMap(map.toString());

        // Process GenericProperties
        item.setShowSignageDisclaimer(genericPropertiesMap.get("ShowCostEscalation"));
        item.setShowFinancialTable(genericPropertiesMap.get("ShowFinancialTable"));
        item.setShowMonthlyUpdateLeftNav(genericPropertiesMap.get("ShowMonthlyUpdateLeftNav"));
        item.setShowFlickrLightbox(genericPropertiesMap.get("ShowFlickrLightbox"));

        try {
            Pattern pattern = Pattern.compile("([\\d+]{10,})");
            Matcher matcher = pattern.matcher(genericPropertiesMap.get("FlickrFeedURL"));
            if (matcher.find()) {
                item.setFlickrAlbumId(matcher.group(1));
            }
        } catch (NullPointerException e) {
        }

        return item;
    }

    private String getHighwayTaxonomy(String hwy) {
        String highway = hwy.replaceAll("([A-Z]+|[0-9]+A?)", "$1 ").trim(); // Also catches US 97A
        highway = highway.replaceAll("I\\s", "I-").trim();

        return highway;
    }

    private String getProjectPhaseTaxonomy(String phaseImage) {
        Map<String, String> projectPhaseImageMap = new HashMap<String, String>();
        projectPhaseImageMap.put("Status_Planning.gif", "Planning");
        projectPhaseImageMap.put("Status_PlanningDesign.gif", "Planning design");
        projectPhaseImageMap.put("Status_Design.gif", "Design");
        projectPhaseImageMap.put("Status_DesignConstruct.gif", "Design construction");
        projectPhaseImageMap.put("Status_Construction.gif", "Construction");
        projectPhaseImageMap.put("Status_Complete.gif", "Complete");

        Matcher matcher;
        try {
            Pattern pattern = Pattern.compile("([A-Z]\\w+.gif)");
            matcher = pattern.matcher(phaseImage);
            if (matcher.find()) {
                return projectPhaseImageMap.get(matcher.group(1));
            } else {
                return "";
            }
        } catch (Exception e) {
        }

        return "";
    }

}