com.ryderbot.utils.ModelUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.ryderbot.utils.ModelUtils.java

Source

package com.ryderbot.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.ryderbot.models.AccountBalance;
import com.ryderbot.models.Blueprint;
import com.ryderbot.models.Contract;
import com.ryderbot.models.CorporationSheet;
import com.ryderbot.models.Job;
import com.ryderbot.models.Logo;
import com.ryderbot.models.MarketOrder;

public class ModelUtils {

    private static Log logger = LogFactory.getLog(ModelUtils.class);

    public static AccountBalance generateAccountBalance(NamedNodeMap attributes) {

        Long accountID = new Long(attributes.getNamedItem("accountID").getNodeValue());
        Long accountKey = new Long(attributes.getNamedItem("accountKey").getNodeValue());
        Double balance = new Double(attributes.getNamedItem("balance").getNodeValue());

        AccountBalance accountBalance = new AccountBalance(accountID);
        accountBalance.setAccountKey(accountKey);
        accountBalance.setBalance(balance);

        return accountBalance;
    }

    public static CorporationSheet generateCorpSheet(Node node) {
        Map<String, String> map = new HashMap<>();
        Map<Long, String> divisions = new HashMap<>();
        Map<Long, String> walletDivisions = new HashMap<>();
        Logo logo = new Logo(null);

        NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);

            String key = child.getNodeName();
            if (key.equals("rowset")) {
                String name = child.getAttributes().getNamedItem("name").getNodeValue();

                NodeList rowset = child.getChildNodes();
                for (int j = 0; j < rowset.getLength(); j++) {
                    try {
                        NamedNodeMap attributes = rowset.item(j).getAttributes();

                        Long accountKey = Long.parseLong(attributes.getNamedItem("accountKey").getNodeValue());
                        String description = attributes.getNamedItem("description").getNodeValue();

                        if (description.equals("Wallet Division 1")) {
                            description = "Master Wallet";
                        }

                        if (name.equals("divisions")) {
                            divisions.put(accountKey, description);
                        } else if (name.equals("walletDivisions")) {
                            walletDivisions.put(accountKey, description);
                        }
                    } catch (Exception e) {
                        continue;
                    }
                }
            } else if (key.equals("logo")) {
                //            Node current = child.getFirstChild();
                //            Long graphicID = Long.parseLong(current.getTextContent());
                //            logo = new Logo(graphicID);
                //            
                //            current = current.getNextSibling();
                //            Integer value = Integer.parseInt(current.getTextContent());
                //            logo.setShape1(value);
                //            
                //            current = current.getNextSibling();
                //            value = Integer.parseInt(current.getTextContent());
                //            logo.setShape2(value);
                //            
                //            current = current.getNextSibling();
                //            value = Integer.parseInt(current.getTextContent());
                //            logo.setShape3(value);
                //            
                //            current = current.getNextSibling();
                //            value = Integer.parseInt(current.getTextContent());
                //            logo.setColor1(value);
                //            
                //            current = current.getNextSibling();
                //            value = Integer.parseInt(current.getTextContent());
                //            logo.setColor2(value);
                //            
                //            current = current.getNextSibling();
                //            value = Integer.parseInt(current.getTextContent());
                //            logo.setColor3(value);
            } else {
                String value = child.getTextContent();

                map.put(key, value);
            }
        }

        Long corporationID = Long.parseLong(map.get("corporationID"));
        CorporationSheet corpSheet = new CorporationSheet(corporationID);

        String corporationName = map.get("corporationName");
        String ticker = map.get("ticker");
        Long ceoID = Long.parseLong(map.get("ceoID"));
        String ceoName = map.get("ceoName");
        Long stationID = Long.parseLong(map.get("stationID"));
        String stationName = map.get("stationName");
        String description = map.get("description");
        String url = map.get("url");
        Long allianceID = Long.parseLong(map.get("allianceID"));
        Long factionID = Long.parseLong(map.get("factionID"));
        String allianceName = map.get("allianceName");
        Double taxRate = Double.parseDouble(map.get("taxRate"));
        Integer memberCount = Integer.parseInt(map.get("memberCount"));
        Integer memberLimit = Integer.parseInt(map.get("memberLimit"));
        Long shares = Long.parseLong(map.get("shares"));

        corpSheet.setCorporationName(corporationName);
        corpSheet.setTicker(ticker);
        corpSheet.setCeoID(ceoID);
        corpSheet.setCeoName(ceoName);
        corpSheet.setStationID(stationID);
        corpSheet.setStationName(stationName);
        corpSheet.setDescription(description);
        corpSheet.setUrl(url);
        corpSheet.setAllianceID(allianceID);
        corpSheet.setFactionID(factionID);
        corpSheet.setAllianceName(allianceName);
        corpSheet.setTaxRate(taxRate);
        corpSheet.setMemberCount(memberCount);
        corpSheet.setMemberLimit(memberLimit);
        corpSheet.setShares(shares);
        corpSheet.setDivisions(divisions);
        corpSheet.setWalletDivisions(walletDivisions);
        corpSheet.setLogo(logo);

        return corpSheet;
    }

    public static Job generateJob(NamedNodeMap attributes) {
        DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        Long jobID = Long.parseLong(attributes.getNamedItem("jobID").getNodeValue());
        Long installerID = Long.parseLong(attributes.getNamedItem("installerID").getNodeValue());
        String installerName = attributes.getNamedItem("installerName").getNodeValue();
        Long facilityID = Long.parseLong(attributes.getNamedItem("facilityID").getNodeValue());
        Long solarSystemID = Long.parseLong(attributes.getNamedItem("solarSystemID").getNodeValue());
        String solarSystemName = attributes.getNamedItem("solarSystemName").getNodeValue();
        Long stationID = Long.parseLong(attributes.getNamedItem("stationID").getNodeValue());
        Integer activityID = Integer.parseInt(attributes.getNamedItem("activityID").getNodeValue());
        Long blueprintID = Long.parseLong(attributes.getNamedItem("blueprintID").getNodeValue());
        ;
        Long blueprintTypeID = Long.parseLong(attributes.getNamedItem("blueprintTypeID").getNodeValue());
        String blueprintTypeName = attributes.getNamedItem("blueprintTypeName").getNodeValue();
        Long blueprintLocationID = Long.parseLong(attributes.getNamedItem("blueprintLocationID").getNodeValue());
        Long outputLocationID = Long.parseLong(attributes.getNamedItem("outputLocationID").getNodeValue());
        Integer runs = Integer.parseInt(attributes.getNamedItem("runs").getNodeValue());
        Double cost = Double.parseDouble(attributes.getNamedItem("cost").getNodeValue());
        Long teamID = Long.parseLong(attributes.getNamedItem("teamID").getNodeValue());
        Integer licensedRuns = Integer.parseInt(attributes.getNamedItem("licensedRuns").getNodeValue());
        Double probability = Double.parseDouble(attributes.getNamedItem("probability").getNodeValue());
        Long productTypeID = Long.parseLong(attributes.getNamedItem("productTypeID").getNodeValue());
        String productTypeName = attributes.getNamedItem("productTypeName").getNodeValue();
        Integer status = Integer.parseInt(attributes.getNamedItem("status").getNodeValue());
        Long timeInSeconds = Long.parseLong(attributes.getNamedItem("timeInSeconds").getNodeValue());
        Long completedCharacterID = Long.parseLong(attributes.getNamedItem("completedCharacterID").getNodeValue());
        Integer successfulRuns = Integer.parseInt(attributes.getNamedItem("successfulRuns").getNodeValue());

        Date startDate = null;
        Date endDate = null;
        Date pauseDate = null;
        Date completedDate = null;

        try {
            startDate = dateFormatter.parse(attributes.getNamedItem("startDate").getNodeValue());
            endDate = dateFormatter.parse(attributes.getNamedItem("endDate").getNodeValue());
            pauseDate = dateFormatter.parse(attributes.getNamedItem("pauseDate").getNodeValue());
            completedDate = dateFormatter.parse(attributes.getNamedItem("completedDate").getNodeValue());
        } catch (DOMException | ParseException e) {

        }

        Job job = new Job(jobID);
        job.setInstallerID(installerID);
        job.setInstallerName(installerName);
        job.setFacilityID(facilityID);
        job.setSolarSystemID(solarSystemID);
        job.setSolarSystemName(solarSystemName);
        job.setStationID(stationID);
        job.setBlueprintTypeName(blueprintTypeName);
        job.setActivityID(activityID);
        job.setBlueprintID(blueprintID);
        job.setBlueprintTypeID(blueprintTypeID);
        job.setBlueprintLocationID(blueprintLocationID);
        job.setOutputLocationID(outputLocationID);
        job.setRuns(runs);
        job.setCost(cost);
        job.setTeamID(teamID);
        job.setLicensedRuns(licensedRuns);
        job.setProbability(probability);
        job.setProductTypeID(productTypeID);
        job.setProductTypeName(productTypeName);
        job.setStatus(status);
        job.setTimeInSeconds(timeInSeconds);
        job.setCompletedCharacterID(completedCharacterID);
        job.setSuccessfulRuns(successfulRuns);
        job.setStartDate(startDate);
        job.setEndDate(endDate);
        job.setPauseDate(pauseDate);
        job.setCompletedDate(completedDate);

        return job;

    }

    public static Contract generateContract(NamedNodeMap attributes) {
        DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Long contractID = Long.parseLong(attributes.getNamedItem("contractID").getNodeValue());
        Long issuerID = Long.parseLong(attributes.getNamedItem("issuerID").getNodeValue());
        Long issuerCorpID = Long.parseLong(attributes.getNamedItem("issuerCorpID").getNodeValue());
        Long assigneeID = Long.parseLong(attributes.getNamedItem("assigneeID").getNodeValue());
        Long acceptorID = Long.parseLong(attributes.getNamedItem("acceptorID").getNodeValue());
        Long startStationID = Long.parseLong(attributes.getNamedItem("startStationID").getNodeValue());
        Long endStationID = Long.parseLong(attributes.getNamedItem("endStationID").getNodeValue());
        String type = attributes.getNamedItem("type").getNodeValue();
        String status = attributes.getNamedItem("status").getNodeValue();
        String title = attributes.getNamedItem("title").getNodeValue();
        boolean forCorp = attributes.getNamedItem("forCorp").getNodeValue().equals("1") ? true : false;
        String availability = attributes.getNamedItem("availability").getNodeValue();
        Integer numDays = Integer.parseInt(attributes.getNamedItem("numDays").getNodeValue());
        Double price = Double.parseDouble(attributes.getNamedItem("price").getNodeValue());
        Double reward = Double.parseDouble(attributes.getNamedItem("reward").getNodeValue());
        ;
        Double collateral = Double.parseDouble(attributes.getNamedItem("collateral").getNodeValue());
        ;
        Double buyout = Double.parseDouble(attributes.getNamedItem("buyout").getNodeValue());
        ;
        Double volume = Double.parseDouble(attributes.getNamedItem("volume").getNodeValue());

        Date dateIssued = null;
        Date dateExpired = null;
        Date dateAccepted = null;
        Date dateCompleted = null;

        try {
            dateIssued = dateFormatter.parse(attributes.getNamedItem("dateIssued").getNodeValue());
            dateExpired = dateFormatter.parse(attributes.getNamedItem("dateExpired").getNodeValue());
            dateAccepted = dateFormatter.parse(attributes.getNamedItem("dateAccepted").getNodeValue());
            dateCompleted = dateFormatter.parse(attributes.getNamedItem("dateCompleted").getNodeValue());
        } catch (DOMException | ParseException e) {

        }

        Contract contract = new Contract(contractID);
        contract.setIssuerID(issuerID);
        contract.setIssuerCorpID(issuerCorpID);
        contract.setAssigneeID(assigneeID);
        contract.setAcceptorID(acceptorID);
        contract.setStartStationID(startStationID);
        contract.setEndStationID(endStationID);
        contract.setType(type);
        contract.setStatus(status);
        contract.setTitle(title);
        contract.setForCorp(forCorp);
        contract.setAvailability(availability);
        contract.setDateIssued(dateIssued);
        contract.setDateExpired(dateExpired);
        contract.setDateAccepted(dateAccepted);
        contract.setDateCompleted(dateCompleted);
        contract.setNumDays(numDays);
        contract.setPrice(price);
        contract.setReward(reward);
        contract.setCollateral(collateral);
        contract.setBuyout(buyout);
        contract.setVolume(volume);

        return contract;
    }

    public static MarketOrder generateMarketOrder(NamedNodeMap attributes) {
        DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Long orderID = Long.parseLong(attributes.getNamedItem("orderID").getNodeValue());
        Long charID = Long.parseLong(attributes.getNamedItem("charID").getNodeValue());
        Long stationID = Long.parseLong(attributes.getNamedItem("stationID").getNodeValue());
        Integer volEntered = Integer.parseInt(attributes.getNamedItem("volEntered").getNodeValue());
        Integer volRemaining = Integer.parseInt(attributes.getNamedItem("volRemaining").getNodeValue());
        Integer minVolume = Integer.parseInt(attributes.getNamedItem("minVolume").getNodeValue());
        Integer orderState = Integer.parseInt(attributes.getNamedItem("orderState").getNodeValue());
        Long typeID = Long.parseLong(attributes.getNamedItem("typeID").getNodeValue());
        Integer range = Integer.parseInt(attributes.getNamedItem("range").getNodeValue());
        Long accountKey = Long.parseLong(attributes.getNamedItem("accountKey").getNodeValue());
        Integer duration = Integer.parseInt(attributes.getNamedItem("duration").getNodeValue());
        Double escrow = Double.parseDouble(attributes.getNamedItem("escrow").getNodeValue());
        Double price = Double.parseDouble(attributes.getNamedItem("price").getNodeValue());
        Double bid = Double.parseDouble(attributes.getNamedItem("bid").getNodeValue());

        Date issued = null;

        try {
            issued = dateFormatter.parse(attributes.getNamedItem("issued").getNodeValue());
        } catch (DOMException | ParseException e) {

        }

        MarketOrder order = new MarketOrder(orderID);
        order.setCharID(charID);
        order.setStationID(stationID);
        order.setVolEntered(volEntered);
        order.setVolRemaining(volRemaining);
        order.setMinVolume(minVolume);
        order.setOrderState(orderState);
        order.setTypeID(typeID);
        order.setRange(range);
        order.setAccountKey(accountKey);
        order.setDuration(duration);
        order.setEscrow(escrow);
        order.setPrice(price);
        order.setBid(bid);
        order.setIssued(issued);

        return order;
    }

    public static Blueprint generateBlueprint(NamedNodeMap attributes) {
        Long itemID = Long.parseLong(attributes.getNamedItem("itemID").getNodeValue());
        Long locationID = Long.parseLong(attributes.getNamedItem("locationID").getNodeValue());
        Long typeID = Long.parseLong(attributes.getNamedItem("typeID").getNodeValue());
        String typeName = attributes.getNamedItem("typeName").getNodeValue();
        Integer flagID = Integer.parseInt(attributes.getNamedItem("flagID").getNodeValue());
        Integer quantity = Integer.parseInt(attributes.getNamedItem("quantity").getNodeValue());
        Integer timeEfficiency = Integer.parseInt(attributes.getNamedItem("timeEfficiency").getNodeValue());
        Integer materialEfficiency = Integer.parseInt(attributes.getNamedItem("materialEfficiency").getNodeValue());
        Integer runs = Integer.parseInt(attributes.getNamedItem("runs").getNodeValue());

        Blueprint blueprint = new Blueprint(itemID);
        blueprint.setLocationID(locationID);
        blueprint.setTypeID(typeID);
        blueprint.setTypeName(typeName);
        blueprint.setFlagID(flagID);
        blueprint.setQuantity(quantity);
        blueprint.setTimeEfficiency(timeEfficiency);
        blueprint.setMaterialEfficiency(materialEfficiency);
        blueprint.setRuns(runs);

        return blueprint;
    }

}