Package com.onpositive.gae.baseviewer.taskqueuehandlers

Source Code of com.onpositive.gae.baseviewer.taskqueuehandlers.AbstractHandler

package com.onpositive.gae.baseviewer.taskqueuehandlers;

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

import javax.servlet.http.HttpServletRequest;

import com.google.appengine.api.NamespaceManager;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

public abstract class AbstractHandler implements ITaskHandler {

  private static int count = 0;

  public static final int REMOVE_ALL = 11;
  public static final int REMOVE_PROP = 12;
  public static final int RENAME_PROP = 13;
  public static final int INIT = 14;
  public static final int COPY_ENT = 16;
  public static final int INDEXATION = 17;

  public static final int SET_PROP_VALUE = 18;
  public static final int SET_PROP_VALUE_BY_DEF = 19;

  public final static String TYPE_PARAM_NAME = "type";
  public final static String NAMESPACE_PARAM_NAME = "namespace";
  public final static String KIND_PARAM_NAME = "kind";
  public final static String PROP_FIRST_PARAM_NAME = "firstProp";
  public final static String PROP_SECOND_PARAM_NAME = "secondProp";
  public final static String LAST_KEY_PARAM_NAME = "last";
  public final static String LIMIT_PARAM_NAME = "limit";
  public final static String INIT_TYPE_PARAM_NAME = "itype";
  public final static String JOB_ID_PARAM_NAME = "jobId";
  public final static String PRE_CALCULATED_TOTAL_PARAM_NAME = "totalPre";

  public static final int FINISH = 1;
  public static final int NO_ERRORS = 0;
  public static final int WITH_ERRORS = -1;

  public static final int MIN_ENT_COUNT = 1;
  public static final int INIT_ENT_COUNT = 50;
  public static final int AVERAGE_ENT_COUNT = 800;
  public static final int MAX_ENT_COUNT = 6400;
  public static final String URL = "/appwrench/queuehandler";

  protected int type;

  protected String namespace;
  protected String idJob;
  protected String kind;
  protected String keyString;
  protected String limitString;
  protected int limit;

  protected String parameter_1;
  protected String parameter_2;

  public AbstractHandler(int id) {
    this.type = id;
  }

  protected FetchOptions insertParametersToQueue(Query q) {
    FetchOptions fo = null;
    // int limit = -1;

    q.addSort(Entity.KEY_RESERVED_PROPERTY);
    if (keyString != null) {
      Key kk = KeyFactory.stringToKey(keyString);
      q.addFilter(Entity.KEY_RESERVED_PROPERTY,
          FilterOperator.GREATER_THAN, kk);
    }

    fo = FetchOptions.Builder.withLimit(limit);
    return fo;
  }

  protected void addTaskToQueue(Map<String, String> params) {
    com.onpositive.gae.baseviewer.impl.Request.addTaskToQueue(params);
  }

  public void handle(HttpServletRequest req) {
    parseCommonParameters(req);
    NamespaceManager.set(namespace);

    int code = handleRequest(req);
    boolean needContinue = false;
    if (code == NO_ERRORS) {
      if (limit * 2 < AVERAGE_ENT_COUNT) {
        limit *= 2;
      }
      needContinue = true;
    } else if (code == WITH_ERRORS) {
      if (limit / 2 > 0) {
        limit /= 2;
      } else {
        limit = MIN_ENT_COUNT;
      }
      needContinue = true;
    } else if (code == FINISH) {
      needContinue = false;
    }

    if (needContinue) {
      Map<String, String> params = new HashMap();
      writeQueueParameters(params);
      addTaskToQueue(params);
    } else {
      StatisticHandler.finishJob(idJob, namespace);
    }
  }

  protected void writeQueueParameters(Map<String, String> params) {
    params.put(TYPE_PARAM_NAME, type + "");
    params.put(NAMESPACE_PARAM_NAME, namespace);
    params.put(KIND_PARAM_NAME, kind);
    params.put(JOB_ID_PARAM_NAME, idJob);
    params.put(LIMIT_PARAM_NAME, limit + "");
    if (parameter_1 != null) {
      params.put(PROP_FIRST_PARAM_NAME, parameter_1);
    }
    if (parameter_2 != null) {
      params.put(PROP_SECOND_PARAM_NAME, parameter_2);
    }
  }

  protected void parseCommonParameters(HttpServletRequest req) {
    namespace = req.getParameter(NAMESPACE_PARAM_NAME);
    kind = req.getParameter(KIND_PARAM_NAME);
    idJob = req.getParameter(JOB_ID_PARAM_NAME);

    keyString = req.getParameter(LAST_KEY_PARAM_NAME);

    limitString = req.getParameter(LIMIT_PARAM_NAME);
    if (limitString != null) {
      try {
        limit = Integer.parseInt(limitString);
      } catch (Throwable ee) {
        limit = INIT_ENT_COUNT;
      }
    } else {
      limit = INIT_ENT_COUNT;
    }

    parameter_1 = req.getParameter(PROP_FIRST_PARAM_NAME);
    parameter_2 = req.getParameter(PROP_SECOND_PARAM_NAME);
  }

  protected abstract int handleRequest(HttpServletRequest req);

}
TOP

Related Classes of com.onpositive.gae.baseviewer.taskqueuehandlers.AbstractHandler

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.