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.HashSet;
022import java.util.List;
023import java.util.Set;
024
025import org.apache.oozie.BundleJobBean;
026import org.apache.oozie.CoordinatorActionBean;
027import org.apache.oozie.CoordinatorJobBean;
028import org.apache.oozie.client.BulkResponse;
029import org.json.simple.JSONArray;
030import org.json.simple.JSONObject;
031
032/**
033 * Server-side implementation class of the client interface BulkResponse
034 * Declares all the bulk request specific user parameters and handling as JSON object
035 */
036public class BulkResponseImpl implements BulkResponse, JsonBean {
037    private BundleJobBean bundle;
038    private CoordinatorJobBean coordinator;
039    private CoordinatorActionBean action;
040
041    public static final String BULK_FILTER_BUNDLE = "bundle";
042    public static final String BULK_FILTER_COORD = "coordinators";
043    public static final String BULK_FILTER_LEVEL = "filterlevel";
044    public static final String BULK_FILTER_STATUS = "actionstatus";
045    public static final String BULK_FILTER_START_CREATED_EPOCH = "startcreatedtime";
046    public static final String BULK_FILTER_END_CREATED_EPOCH = "endcreatedtime";
047    public static final String BULK_FILTER_START_NOMINAL_EPOCH = "startscheduledtime";
048    public static final String BULK_FILTER_END_NOMINAL_EPOCH = "endscheduledtime";
049    public static final String BULK_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:SS'Z'";
050
051    public static final Set<String> BULK_FILTER_NAMES = new HashSet<String>();
052
053    static {
054
055        BULK_FILTER_NAMES.add(BulkResponseImpl.BULK_FILTER_BUNDLE);
056        BULK_FILTER_NAMES.add(BulkResponseImpl.BULK_FILTER_COORD);
057        BULK_FILTER_NAMES.add(BulkResponseImpl.BULK_FILTER_LEVEL);
058        BULK_FILTER_NAMES.add(BulkResponseImpl.BULK_FILTER_STATUS);
059        BULK_FILTER_NAMES.add(BulkResponseImpl.BULK_FILTER_START_CREATED_EPOCH);
060        BULK_FILTER_NAMES.add(BulkResponseImpl.BULK_FILTER_END_CREATED_EPOCH);
061        BULK_FILTER_NAMES.add(BulkResponseImpl.BULK_FILTER_START_NOMINAL_EPOCH);
062        BULK_FILTER_NAMES.add(BulkResponseImpl.BULK_FILTER_END_NOMINAL_EPOCH);
063
064    }
065
066    /**
067     * Construct JSON object using the bulk request object and the associated tags
068     */
069    public JSONObject toJSONObject() {
070        return toJSONObject("GMT");
071    }
072
073    /**
074     * Construct JSON object using the bulk request object and the associated tags
075     */
076    @SuppressWarnings("unchecked")
077    public JSONObject toJSONObject(String timeZoneId) {
078        JSONObject json = new JSONObject();
079
080        json.put(JsonTags.BULK_RESPONSE_BUNDLE, bundle.toJSONObject());
081        json.put(JsonTags.BULK_RESPONSE_COORDINATOR, coordinator.toJSONObject());
082        json.put(JsonTags.BULK_RESPONSE_ACTION, action.toJSONObject());
083
084        return json;
085    }
086
087    /* (non-Javadoc)
088     * @see org.apache.oozie.client.BulkResponse#getBundle()
089     */
090    @Override
091    public BundleJobBean getBundle() {
092        return bundle;
093    }
094
095    /* (non-Javadoc)
096     * @see org.apache.oozie.client.BulkResponse#getCoordinator()
097     */
098    @Override
099    public CoordinatorJobBean getCoordinator() {
100        return coordinator;
101    }
102
103    /* (non-Javadoc)
104     * @see org.apache.oozie.client.BulkResponse#getAction()
105     */
106    @Override
107    public CoordinatorActionBean getAction() {
108        return action;
109    }
110
111    /**
112     * Sets the bundle comprising this bulk response object
113     * @param bj
114     */
115    public void setBundle(BundleJobBean bj) {
116        this.bundle = bj;
117    }
118
119    /**
120     * Sets the coordinator comprising this bulk response object
121     * @param cj
122     */
123    public void setCoordinator(CoordinatorJobBean cj) {
124        this.coordinator = cj;
125    }
126
127    /**
128     * Sets the coord action comprising this bulk response object
129     * @param ca
130     */
131    public void setAction(CoordinatorActionBean ca) {
132        this.action = ca;
133    }
134
135    /**
136     * Convert a nodes list into a JSONArray.
137     *
138     * @param responses nodes list.
139     * @param timeZoneId time zone to use for dates in the JSON array.
140     * @return the corresponding JSON array.
141     */
142    @SuppressWarnings("unchecked")
143    public static JSONArray toJSONArray(List<? extends BulkResponseImpl> responses, String timeZoneId) {
144        JSONArray array = new JSONArray();
145        for (BulkResponseImpl response : responses) {
146            array.add(response.toJSONObject(timeZoneId));
147        }
148        return array;
149    }
150}