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