Package com.metrictracker.ws

Source Code of com.metrictracker.ws.ManageMetricValue

package com.metrictracker.ws;

import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Logger;

import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ResourceException;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.metrictracker.model.Metric;
import com.metrictracker.model.MetricValue;
import com.metrictracker.model.MetricValueDao;
import com.metrictracker.util.ExceptionManager;


/**
* Resource which has only one representation.
*
*/
public class ManageMetricValue extends MetricTrackerServerResource {

  private static final Logger log = Logger.getLogger(ManageMetricValue.class.getName());
 
    @Get
    public String represent() {
      Metric metric = getMetric();  
      MetricValueDao dao = new MetricValueDao();
      Iterator<MetricValue> metricValues = dao.listByProperty("metricKey", metric.getKey()).iterator();
      StringBuffer sb = new StringBuffer("");
    sb.append("Metric Name: " + metric.getName());
    sb.append(System.getProperty("line.separator"));
    while (metricValues.hasNext()) {
       MetricValue metricValue = metricValues.next();
       sb.append(metricValue.getTimeFrame());
       sb.append("\t");
       sb.append(metricValue.getValue());
       sb.append(System.getProperty("line.separator"));
    }
    
        return sb.toString();
    }
   
    @Post("form:xml|json")
    public void acceptRepresentation(Representation entity) throws ResourceException {
     
      Objectify ofy = ObjectifyService.begin();
 
    try {
      HashMap<String, String> parameters = getParameters(entity);
          String value = parameters.get("value");
          String timeFrame = parameters.get("timeFrame");
         
          Metric metric = getMetric();
         
          MetricValue metricValue = new MetricValue();
          metricValue.setMetric(metric);
          metricValue.setTimeFrame(Date.valueOf(timeFrame));
          metricValue.setMetricValue(Long.valueOf(value));
         
          ofy.put(metricValue);
        getResponse().setStatus(Status.SUCCESS_CREATED)
           
    } catch (IOException e) {
      ExceptionManager.logException(log, e);
      getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
      return;
    }
    }
}
TOP

Related Classes of com.metrictracker.ws.ManageMetricValue

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.