001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.client.rest;
020
021import java.util.List;
022import java.util.Set;
023
024import org.apache.oozie.BundleJobBean;
025import org.apache.oozie.CoordinatorActionBean;
026import org.apache.oozie.CoordinatorJobBean;
027import org.apache.oozie.client.BulkResponse;
028import org.json.simple.JSONArray;
029import org.json.simple.JSONObject;
030
031import com.google.common.collect.ImmutableSet;
032
033/**
034 * Server-side implementation class of the client interface BulkResponse
035 * Declares all the bulk request specific user parameters and handling as JSON object
036 */
037public class BulkResponseImpl implements BulkResponse, JsonBean {
038    private BundleJobBean bundle;
039    private CoordinatorJobBean coordinator;
040    private CoordinatorActionBean action;
041
042    public static final String BULK_FILTER_BUNDLE = "bundle";
043    public static final String BULK_FILTER_COORD = "coordinators";
044    public static final String BULK_FILTER_LEVEL = "filterlevel";
045    public static final String BULK_FILTER_STATUS = "actionstatus";
046    public static final String BULK_FILTER_START_CREATED_EPOCH = "startcreatedtime";
047    public static final String BULK_FILTER_END_CREATED_EPOCH = "endcreatedtime";
048    public static final String BULK_FILTER_START_NOMINAL_EPOCH = "startscheduledtime";
049    public static final String BULK_FILTER_END_NOMINAL_EPOCH = "endscheduledtime";
050    public static final String BULK_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:SS'Z'";
051
052    public static final Set<String> BULK_FILTER_NAMES = ImmutableSet.of(BulkResponseImpl.BULK_FILTER_BUNDLE,
053            BulkResponseImpl.BULK_FILTER_COORD,
054            BulkResponseImpl.BULK_FILTER_LEVEL,
055            BulkResponseImpl.BULK_FILTER_STATUS,
056            BulkResponseImpl.BULK_FILTER_START_CREATED_EPOCH,
057            BulkResponseImpl.BULK_FILTER_END_CREATED_EPOCH,
058            BulkResponseImpl.BULK_FILTER_START_NOMINAL_EPOCH,
059            BulkResponseImpl.BULK_FILTER_END_NOMINAL_EPOCH);
060
061    /**
062     * Construct JSON object using the bulk request object and the associated tags
063     */
064    public JSONObject toJSONObject() {
065        return toJSONObject("GMT");
066    }
067
068    /**
069     * Construct JSON object using the bulk request object and the associated tags
070     */
071    @SuppressWarnings("unchecked")
072    public JSONObject toJSONObject(String timeZoneId) {
073        JSONObject json = new JSONObject();
074
075        json.put(JsonTags.BULK_RESPONSE_BUNDLE, bundle.toJSONObject());
076        json.put(JsonTags.BULK_RESPONSE_COORDINATOR, coordinator.toJSONObject());
077        json.put(JsonTags.BULK_RESPONSE_ACTION, action.toJSONObject());
078
079        return json;
080    }
081
082    /* (non-Javadoc)
083     * @see org.apache.oozie.client.BulkResponse#getBundle()
084     */
085    @Override
086    public BundleJobBean getBundle() {
087        return bundle;
088    }
089
090    /* (non-Javadoc)
091     * @see org.apache.oozie.client.BulkResponse#getCoordinator()
092     */
093    @Override
094    public CoordinatorJobBean getCoordinator() {
095        return coordinator;
096    }
097
098    /* (non-Javadoc)
099     * @see org.apache.oozie.client.BulkResponse#getAction()
100     */
101    @Override
102    public CoordinatorActionBean getAction() {
103        return action;
104    }
105
106    /**
107     * Sets the bundle comprising this bulk response object
108     * @param bj the bundle
109     */
110    public void setBundle(BundleJobBean bj) {
111        this.bundle = bj;
112    }
113
114    /**
115     * Sets the coordinator comprising this bulk response object
116     * @param cj the coord
117     */
118    public void setCoordinator(CoordinatorJobBean cj) {
119        this.coordinator = cj;
120    }
121
122    /**
123     * Sets the coord action comprising this bulk response object
124     * @param ca the action
125     */
126    public void setAction(CoordinatorActionBean ca) {
127        this.action = ca;
128    }
129
130    /**
131     * Convert a nodes list into a JSONArray.
132     *
133     * @param responses nodes list.
134     * @param timeZoneId time zone to use for dates in the JSON array.
135     * @return the corresponding JSON array.
136     */
137    @SuppressWarnings("unchecked")
138    public static JSONArray toJSONArray(List<? extends BulkResponseImpl> responses, String timeZoneId) {
139        JSONArray array = new JSONArray();
140        for (BulkResponseImpl response : responses) {
141            array.add(response.toJSONObject(timeZoneId));
142        }
143        return array;
144    }
145}