com.lifetime.util.TimeOffUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.lifetime.util.TimeOffUtil.java

Source

/**
 * The MIT License (MIT)
 * Copyright (c) 2016 Edmond Ho, Howie Chou, Rebecca Dai, Ryan Park,
 * Shalaka Tendulkar, Shane Merriss, Yury Butrymovich.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.lifetime.util;

import com.lifetime.model.RepliconTimeOff;
import com.lifetime.model.impl.RepliconTimeOffImpl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class TimeOffUtil {

    static final String COMPANY_NAME = PortletPropsValues.REPLICON_COMPANY_NAME;
    static final String USERNAME = PortletPropsValues.REPLICON_USER_NAME;
    static final String PASSWORD = PortletPropsValues.REPLICON_PASSWORD;

    public static HttpEntity getTimeoffRequestEntity(int page, int pagesize) {
        if ((page <= 0) || (pagesize <= 0)) {
            return null;
        }

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("page", String.valueOf(page));
        jsonObject.put("pagesize", String.valueOf(pagesize));

        JSONArray columnUris = new JSONArray();

        columnUris.add("urn:replicon:time-off-list-column:time-off");
        columnUris.add("urn:replicon:time-off-list-column:time-off-type");
        columnUris.add("urn:replicon:time-off-list-column:department-of-time-off-owner");

        jsonObject.put("columnUris", columnUris);

        JSONArray sort = new JSONArray();

        JSONObject sortObject = new JSONObject();

        sortObject.put("columnUri", "urn:replicon:time-off-list-column:time-off");
        sortObject.put("isAscending", "false");

        sort.add(sortObject);

        JSONObject filter = new JSONObject();

        JSONObject leftExpression = new JSONObject();

        leftExpression.put("filterDefinitionUri", "urn:replicon:time-off-list-filter:time-off-type");

        filter.put("leftExpression", leftExpression);

        filter.put("operatorUri", "urn:replicon:filter-operator:equal");

        JSONObject value = new JSONObject();

        value.put("string", "us-pto");

        JSONObject rightExpression = new JSONObject();

        rightExpression.put("value", value);

        filter.put("rightExpression", rightExpression);

        jsonObject.put("filterExpression", filter);

        HttpEntity entity = null;

        String jsonString = jsonObject.toJSONString();

        try {
            entity = new StringEntity(jsonString);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return entity;
    }

    public List<RepliconTimeOff> getTimeOffs(int page, int pageSize, boolean ascending) {

        throw new UnsupportedOperationException();
    }

    protected static List<RepliconTimeOff> createTimeOffList(String response) throws ParseException {

        JSONParser parser = new JSONParser();

        JSONObject jsonObject = (JSONObject) parser.parse(response);

        JSONObject d = (JSONObject) jsonObject.get("d");

        JSONArray rows = (JSONArray) d.get("rows");

        List<RepliconTimeOff> list = new ArrayList<RepliconTimeOff>();

        for (Object row : rows) {
            JSONArray cells = (JSONArray) ((JSONObject) row).get("cells");

            JSONObject owner = (JSONObject) cells.get(0);
            String employeeName = (String) owner.get("textValue");

            JSONObject department = (JSONObject) cells.get(1);
            String departmentName = (String) department.get("textValue");

            JSONObject ptoType = (JSONObject) cells.get(2);
            String ptoTypeName = (String) ptoType.get("textValue");

            SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy");

            JSONObject startDateJson = (JSONObject) cells.get(3);
            String startDateString = (String) startDateJson.get("textValue");

            Date startDate = null;

            try {
                startDate = dateFormat.parse(startDateString);
            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }

            JSONObject endDateJson = (JSONObject) cells.get(3);
            String endDateString = (String) endDateJson.get("textValue");

            Date endDate = null;

            try {
                endDate = dateFormat.parse(endDateString);
            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }

            JSONObject approvedJson = (JSONObject) cells.get(5);
            String approvedString = (String) approvedJson.get("textValue");

            boolean approved = approvedString.equals("Approved") ? true : false;

            RepliconTimeOff repliconTimeOff = new RepliconTimeOffImpl(employeeName, departmentName, startDate,
                    endDate, ptoTypeName, approved);

            list.add(repliconTimeOff);
        }

        return list;
    }

    private static List<RepliconTimeOff> getTimeOffInfo(int page, int pageSize) throws IOException, ParseException {

        String url = "https://na5.replicon.com/liferay/services/" + "TimeOffListService1.svc/GetData";

        CloseableHttpClient httpClient = HttpClients.createDefault();

        CredentialsProvider provider = new BasicCredentialsProvider();

        UsernamePasswordCredentials credential = new UsernamePasswordCredentials(COMPANY_NAME + '\\' + USERNAME,
                PASSWORD);

        provider.setCredentials(AuthScope.ANY, credential);

        HttpClientContext localContext = HttpClientContext.create();

        localContext.setCredentialsProvider(provider);

        HttpPost httpRequest = new HttpPost(url);

        HttpEntity requestEntity = getTimeoffRequestEntity(page, pageSize);

        if (requestEntity != null) {
            httpRequest.setHeader("Content-type", "application/json");
            httpRequest.setEntity(requestEntity);
        }

        CloseableHttpResponse response = httpClient.execute(httpRequest, localContext);

        HttpEntity entity = response.getEntity();

        return createTimeOffList(EntityUtils.toString(entity));
    }

}