Package de.peacei.gae.foodsupplier.out.json

Source Code of de.peacei.gae.foodsupplier.out.json.JSONRenderer

/**
*
*/
package de.peacei.gae.foodsupplier.out.json;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;

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

import com.google.appengine.api.datastore.KeyFactory;

import de.peacei.gae.foodsupplier.data.Comment;
import de.peacei.gae.foodsupplier.data.Food;
import de.peacei.gae.foodsupplier.data.Menu;
import de.peacei.gae.foodsupplier.data.User;
import de.peacei.gae.foodsupplier.data.Weekplan;
import de.peacei.gae.foodsupplier.out.AbstractRenderer;
import de.peacei.gae.foodsupplier.out.Renderer;
import de.peacei.gae.foodsupplier.util.CalendarUtil;

/**
* @author peacei
*
*/
public class JSONRenderer extends AbstractRenderer implements Renderer {
 
  private final String COMMENTS = "comments";
  private final String COMMENT_NAME = "name";
  private final String COMMENT_EMAIL = "email";
  private final String COMMENT_MSG = "msg";
  //private final String COMMENT_MENU_KEY = "menukey";
  private final String COMMENT_TIMESTAMP = "timestamp";
  private final String COMMENT_CLIENT = "client";
 
  private final String FOOD_TITLE = "title";
  private final String FOOD_DESC = "desc";
  private final String FOOD_TYPE = "type";
  private final String FOOD_EXTRA = "extra";
  private final String FOOD_STUDENT_PRICE = "studentprice";
  private final String FOOD_STAFF_PRICE = "staffprice";
 
  private final String MENU_DAY = "day";
  private final String MENU_KEY = "id";
  private final String MENU_FOODS = "foods";
 
  private final String WEEKPLAN_WEEKNUMBER = "weeknumber";
  private final String WEEKPLAN_MENUES = "menues";
 
  @Override
  public void renderWeekplan(Weekplan weekplan, OutputStream os) {
    JSONObject weekplanJsonObj = new JSONObject();
   
    try {
      weekplanJsonObj.put(WEEKPLAN_WEEKNUMBER, weekplan.getWeek());
      JSONArray menuesJsonArr = new JSONArray();
      Iterator<Menu> menuIter = weekplan.getMenus().iterator();
      while (menuIter.hasNext()) {
        Menu menu = menuIter.next();
       
        JSONObject menuJsonObj = new JSONObject();
        menuJsonObj.put(MENU_DAY, CalendarUtil.getDayOfWeek(menu.getDay())-2);
        menuJsonObj.put(MENU_KEY, KeyFactory.keyToString(menu.getKey()));
       
        JSONArray foodsJsonArr = new JSONArray();
        Iterator<Food> foodIter = menu.getFoods().iterator();
        while (foodIter.hasNext()) {
          Food food = foodIter.next();
         
          JSONObject foodJsonObj = new JSONObject();
          foodJsonObj.put(FOOD_TITLE, food.getTitle());
          foodJsonObj.put(FOOD_DESC, food.getDescription());
          foodJsonObj.put(FOOD_TYPE, food.getType());
          foodJsonObj.put(FOOD_EXTRA, food.getExtra());
          foodJsonObj.put(FOOD_STUDENT_PRICE, food.getStudentPrice());
          foodJsonObj.put(FOOD_STAFF_PRICE, food.getStaffPrice());
         
          foodsJsonArr.put(foodJsonObj);
        }
        menuJsonObj.put(MENU_FOODS, foodsJsonArr);
       
        menuesJsonArr.put(menuJsonObj);
      }
     
      weekplanJsonObj.put(WEEKPLAN_MENUES, menuesJsonArr);
     
            String weekplanStr = weekplanJsonObj.toString();
      if(this.callback != null) weekplanStr = jsWrap(weekplanStr);
     
            OutputStreamWriter osWriter = new OutputStreamWriter(os, "UTF8");
            osWriter.write(weekplanStr);
            osWriter.close();
     
    } catch (JSONException e) { }
    catch(IOException e) { }
  }
 
  @Override
  public void renderComments(List<Comment> comments, OutputStream os) {
    JSONObject commentsJsonObj = new JSONObject();
    try {
      final JSONArray commentsJsonArr = new JSONArray();
      for(int i=0; i<comments.size(); i++){
        commentsJsonArr.put(commentToJSON(comments.get(i)));
      }
       
      commentsJsonObj.put(COMMENTS, commentsJsonArr);
     
      String commentsStr = commentsJsonObj.toString();
      if(callback != null) commentsStr = jsWrap(commentsStr);
     
      os.write(commentsJsonObj.toString().getBytes());
    } catch(JSONException ex) { }
    catch(IOException ex) { } 
  }
 
  private JSONObject commentToJSON(Comment comment) throws JSONException{
    final JSONObject result = new JSONObject();
   
    User user = comment.getUser();
    result.put(COMMENT_NAME, user.getName());
    String mailAddy = user.getEmail();
    try {
      mailAddy = mailAddy.substring(0, 3) + "..." + mailAddy.substring(mailAddy.indexOf('@'));
    } catch (Exception ex) {
      mailAddy = "---";
    }
    result.put(COMMENT_EMAIL, mailAddy);
    result.put(COMMENT_TIMESTAMP, comment.getTimestamp());
    result.put(COMMENT_MSG, comment.getText());
    result.put(COMMENT_CLIENT, comment.getClient());
    //result.put(COMMENT_MENU_KEY, KeyFactory.keyToString(comment.getMenuKey()));
   
    return result;
  }
}
TOP

Related Classes of de.peacei.gae.foodsupplier.out.json.JSONRenderer

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.