package com.continuuity.examples.countcounts;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.continuuity.api.annotation.Handle;
import com.continuuity.api.annotation.UseDataSet;
import com.continuuity.api.common.Bytes;
import com.continuuity.api.data.OperationException;
import com.continuuity.api.procedure.AbstractProcedure;
import com.continuuity.api.procedure.ProcedureRequest;
import com.continuuity.api.procedure.ProcedureResponder;
import com.continuuity.api.procedure.ProcedureResponse;

/**
 *
 */
public class CountCountsProcedure extends AbstractProcedure {
  private static Logger LOG = LoggerFactory.getLogger(CountCountsProcedure.class);

  @UseDataSet(CountCounts.tableName)
  CountCounterTable counters;

  @Handle("countCounts")
  public void countCounts(ProcedureRequest request, ProcedureResponder responder)
    throws OperationException, IOException {

    // Primary method is countCounts that returns a string representation of
    // the counts of each per-line word count
      Map<Long,Long> counts = this.counters.getWordCountCounts();

      ObjectMapper mapper = new ObjectMapper();
      byte[] returnBytes = mapper.writeValueAsBytes(counts);

      // Write the returnBytes out to the response
      ProcedureResponse.Writer writer = responder.stream(
          new ProcedureResponse(ProcedureResponse.Code.SUCCESS));
      try {
        writer.write(returnBytes);
      } finally {
        writer.close();
      }
  }

  @Handle("wordCount")
  public void wordCount(ProcedureRequest request, ProcedureResponder responder)
    throws OperationException, IOException {
      writeCountInResponse(responder, this.counters.getTotalWordCount());
  }

  @Handle("lineCount")
  public void lineCount(ProcedureRequest request, ProcedureResponder responder)
    throws OperationException, IOException {
      writeCountInResponse(responder, this.counters.getLineCount());
  }

  @Handle("lineLength")
  public void lineLength(ProcedureRequest request, ProcedureResponder responder)
    throws OperationException, IOException {
      writeCountInResponse(responder, this.counters.getLineLength());
  }

  private void writeCountInResponse(ProcedureResponder responder, long returnCount) throws IOException {
    // Write the returnCount out to the response
    ProcedureResponse.Writer writer = responder.stream(
        new ProcedureResponse(ProcedureResponse.Code.SUCCESS));
    try {
      writer.write(Bytes.toBytes(Long.toString(returnCount)));
    } finally {
      writer.close();
    }
  }
}

@@ -1193,18 +1193,16 @@
 unt%0A    
-  
 Map%3CLong
@@ -1258,18 +1258,16 @@
 );%0A%0A    
-  
 ObjectMa
@@ -1296,26 +1296,24 @@
 ctMapper();%0A
-  
     byte%5B%5D r
@@ -1364,18 +1364,16 @@
 );%0A%0A    
-  
 // Write
@@ -1409,18 +1409,16 @@
 esponse%0A
-  
     Proc
@@ -1469,26 +1469,24 @@
 am(%0A        
- 
- 
 new Procedur
@@ -1533,26 +1533,22 @@
 ));%0A    
-  
 try %7B%0A
-  
       wr
@@ -1576,18 +1576,16 @@
 s);%0A    
- 
- 
 %7D finall
@@ -1584,26 +1584,24 @@
 %7D finally %7B%0A
-  
       writer
@@ -1610,18 +1610,16 @@
 lose();%0A
-  
     %7D%0A  

fix indentation in CountCountsProcedure.java