All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.centurylink.mdw.service.rest.Values Maven / Gradle / Ivy

There is a newer version: 6.1.39
Show newest version
/*
 * Copyright (C) 2017 CenturyLink, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.centurylink.mdw.service.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Path;

import org.json.JSONException;
import org.json.JSONObject;

import com.centurylink.mdw.common.service.ServiceException;
import com.centurylink.mdw.common.service.types.StatusMessage;
import com.centurylink.mdw.model.JsonObject;
import com.centurylink.mdw.model.user.Role;
import com.centurylink.mdw.model.user.UserAction.Entity;
import com.centurylink.mdw.services.ServiceLocator;
import com.centurylink.mdw.services.WorkflowServices;
import com.centurylink.mdw.services.rest.JsonRestService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Path("/Values")
@Api("Runtime values")
public class Values extends JsonRestService {

    @Override
    public List getRoles(String path) {
        List roles = super.getRoles(path);
        roles.add(Role.PROCESS_EXECUTION);
        return roles;
    }

    @Override
    protected Entity getEntity(String path, Object content, Map headers) {
        return Entity.Value;
    }

    /**
     * Retrieve values.
     */
    @Override
    @Path("/{ownerType}/{ownerId}")
    @ApiOperation(value="Retrieve values for an ownerType and ownerId",
        notes="Response is a generic JSON object with names/values.")
    public JSONObject get(String path, Map headers) throws ServiceException, JSONException {
        Map parameters = getParameters(headers);
        String ownerType = getSegment(path, 1);
        if (ownerType == null) // fall back to parameter
            ownerType = parameters.get("ownerType");
        if (ownerType == null)
            throw new ServiceException("Missing path segment: {ownerType}");
        String ownerId = getSegment(path, 2);
        if (ownerId == null) // fall back to parameter
            ownerId = parameters.get("ownerId");
        if (ownerId == null)
            throw new ServiceException("Missing path segment: {ownerId}");

        JSONObject valuesJson = new JsonObject();
        Map values = ServiceLocator.getWorkflowServices().getValues(ownerType, ownerId);
        if (values != null) {
            for (String name : values.keySet())
                valuesJson.put(name, values.get(name));
        }
        return valuesJson;
    }

    /**
     * Create values for owner type and id.
     */
    @Override
    @Path("/{ownerType}/{ownerId}")
    @ApiOperation(value="Create values for an ownerType and ownerId", response=StatusMessage.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name="Values", paramType="body")})
    public JSONObject post(String path, JSONObject content, Map headers)
    throws ServiceException, JSONException {
        return put(path, content, headers);
    }

    /**
     * Update values for owner type and id.
     * Existing values are always overwritten.
     */
    @Override
    @Path("/{ownerType}/{ownerId}")
    @ApiOperation(value="Update values for an ownerType and ownerId", response=StatusMessage.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name="Values", paramType="body")})
    public JSONObject put(String path, JSONObject content, Map headers)
    throws ServiceException, JSONException {
        Map parameters = getParameters(headers);
        String ownerType = getSegment(path, 1);
        if (ownerType == null)
            ownerType = parameters.get("ownerType");
        if (ownerType == null)
            throw new ServiceException("Missing parameter: ownerType");
        String ownerId = getSegment(path, 2);
        if (ownerId == null)
            ownerId = parameters.get("ownerId");
        if (ownerId == null)
            throw new ServiceException("Missing parameter: ownerId");
        String updateOnly = getSegment(path, 3);
        if (updateOnly == null)
            updateOnly = parameters.get("updateOnly");
        if (content == null)
            throw new ServiceException("Missing JSON object: attributes");

        try {
            Map values = new HashMap();
            String[] names = JSONObject.getNames(content);
            if (names != null) {
                for (String name : names)
                    values.put(name, content.getString(name));
            }
            WorkflowServices workflowServices = ServiceLocator.getWorkflowServices();

            if (updateOnly != null) {
                // update values, without deleting all other values for that ownerId
                workflowServices.updateValues(ownerType, ownerId, values);
            } else {
                workflowServices.setValues(ownerType, ownerId, values);
            }
            return null;
        }
        catch (JSONException ex) {
            throw new ServiceException(ex.getMessage(), ex);
        }
    }

    /**
     * Delete values for owner type and id.
     */
    @Override
    @Path("/{ownerType}/{ownerId}")
    @ApiOperation(value="Delete values for an ownerType and ownerId", response=StatusMessage.class)
    public JSONObject delete(String path, JSONObject content, Map headers)
    throws ServiceException, JSONException {
        JSONObject empty = new JsonObject();
        return put(path, empty, headers);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy