001/**
002 * Copyright 2016 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.apilta.alerts;
017
018import java.util.ArrayList;
019
020import core.tut.pori.http.Response;
021import core.tut.pori.http.Response.Status;
022import core.tut.pori.http.annotations.HTTPAuthenticationParameter;
023import core.tut.pori.http.annotations.HTTPMethodParameter;
024import core.tut.pori.http.annotations.HTTPService;
025import core.tut.pori.http.annotations.HTTPServiceMethod;
026import core.tut.pori.http.parameters.AuthenticationParameter;
027import core.tut.pori.http.parameters.DataGroups;
028import core.tut.pori.http.parameters.DateIntervalParameter;
029import core.tut.pori.http.parameters.DoubleParameter;
030import core.tut.pori.http.parameters.InputStreamParameter;
031import core.tut.pori.http.parameters.Limits;
032import core.tut.pori.http.parameters.LongParameter;
033import core.tut.pori.http.parameters.StringParameter;
034import core.tut.pori.utils.XMLFormatter;
035import service.tut.pori.apilta.alerts.datatypes.Alert;
036import service.tut.pori.apilta.alerts.datatypes.AlertList;
037import service.tut.pori.apilta.alerts.datatypes.LocationParameter;
038import service.tut.pori.apilta.files.datatypes.FileDetailsList;
039
040/**
041 * Service declaration for the alerts service
042 * 
043 */
044@HTTPService(name = Definitions.SERVICE_ALERTS)
045public class AlertService {
046  private XMLFormatter _formatter = new XMLFormatter();
047  
048  /**
049   * 
050   * @param authenticatedUser
051   * @param alertGroupIdFilter
052   * @param alertTypeFilter
053   * @param createdFilter 
054   * @param dataGroups 
055   * @param location
056   * @param range
057   * @param limits
058   * @return see {@link service.tut.pori.apilta.alerts.datatypes.AlertList}
059   * @see service.tut.pori.apilta.alerts.reference.ClientService#getAlerts(AuthenticationParameter, LongParameter, StringParameter, DateIntervalParameter, DataGroups, LocationParameter, DoubleParameter, Limits)
060   */
061  @HTTPServiceMethod(name = Definitions.METHOD_GET_ALERTS, acceptedMethods={core.tut.pori.http.Definitions.METHOD_GET})
062  public Response getAlerts(
063      @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser,
064      @HTTPMethodParameter(name = Definitions.PARAMETER_ALERT_GROUP_ID, required = false) LongParameter alertGroupIdFilter,
065      @HTTPMethodParameter(name = Definitions.PARAMETER_ALERT_TYPE, required = false) StringParameter alertTypeFilter,
066      @HTTPMethodParameter(name = Definitions.PARAMETER_CREATED, required = false) DateIntervalParameter createdFilter,
067      @HTTPMethodParameter(name = DataGroups.PARAMETER_DEFAULT_NAME, required = false) DataGroups dataGroups,
068      @HTTPMethodParameter(name = LocationParameter.PARAMETER_DEFAULT_NAME, required = false) LocationParameter location,
069      @HTTPMethodParameter(name = Definitions.PARAMETER_RANGE, required = false) DoubleParameter range,
070      @HTTPMethodParameter(name = Limits.PARAMETER_DEFAULT_NAME, required = false) Limits limits
071      )
072  {
073    return new Response(AlertsCore.retrieveAlerts(alertGroupIdFilter.getValues(), alertTypeFilter.getValues(), authenticatedUser.getUserIdentity(), createdFilter.getValues(), dataGroups, limits, location.getValue(), range.getValue()));
074  }
075
076  /**
077   * 
078   * @param authenticatedUser
079   * @param alertGroupId 
080   * @param xml Only the workload data should be in the body. See {@link service.tut.pori.apilta.alerts.datatypes.Alert}
081   * @return response See {@link service.tut.pori.apilta.alerts.datatypes.AlertList}
082   * @see service.tut.pori.apilta.alerts.reference.ClientService#addAlert(AuthenticationParameter, LongParameter, InputStreamParameter)
083   */
084  @HTTPServiceMethod(name = Definitions.METHOD_ADD_ALERT, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
085  public Response addAlert (
086      @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser,
087      @HTTPMethodParameter(name = Definitions.PARAMETER_ALERT_GROUP_ID) LongParameter alertGroupId,
088      @HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter xml
089      ) 
090  {
091    Response r = new Response();
092    String alertId = AlertsCore.addAlert(_formatter.toObject(xml.getValue(), Alert.class), alertGroupId.getValues(), authenticatedUser.getUserIdentity());
093    if(alertId == null){
094      r.setStatus(Status.FORBIDDEN);
095    }else{
096      Alert alert = new Alert();
097      alert.setAlertId(alertId);
098      ArrayList<Alert> list = new ArrayList<>(1);
099      list.add(alert);
100      AlertList alerts = new AlertList();
101      alerts.setAlerts(list);
102      r.setResponseData(alerts);
103    }
104    return r;
105  }
106  
107  /**
108   * @param authenticatedUser
109   * @param file only the file contents should be in the body.
110   * @return {@link service.tut.pori.apilta.files.datatypes.FileDetailsList} with identifier for the generated file
111   * @see service.tut.pori.apilta.alerts.reference.ClientService#createFile(AuthenticationParameter, InputStreamParameter)
112   */
113  @HTTPServiceMethod(name = service.tut.pori.apilta.alerts.Definitions.METHOD_CREATE_FILE, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
114  public Response createFile (
115      @SuppressWarnings("unused") @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser,
116      @HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter file
117      ) 
118  {
119    FileDetailsList list = new FileDetailsList();
120    list.addFile(AlertsCore.createFile(file.getValue()));
121    return new Response(list);
122  }
123}