Package org.yaac.server.egql.evaluator.function

Source Code of org.yaac.server.egql.evaluator.function.UserFunction

package org.yaac.server.egql.evaluator.function;

import org.yaac.server.egql.evaluator.EvaluationResult;
import org.yaac.server.egql.exception.EGQLException;
import org.yaac.server.egql.processor.ProcessData.ProcessDataRecord;
import org.yaac.shared.ErrorCode;
import org.yaac.shared.SharedConstants;

import com.google.appengine.api.users.User;

/**
* @author Max Zhu (thebbsky@gmail.com)
*
*/
public class UserFunction extends Function {

  /**
   *
   */
  private static final long serialVersionUID = 1L;

  @Override
  public void validate() throws EGQLException {
    FunctionUtil.ensureParamSize(ops, 1, 2);
  }

  @Override
  public EvaluationResult evaluate(ProcessDataRecord record) {
    if (this.ops.isEmpty()) {
      return new EvaluationResult(null).withWarning(ErrorCode.W120);
    } else if (this.ops.size() == 1) {
      EvaluationResult r = this.ops.get(0).evaluate(record);
      if (r.getPayload() instanceof String) {         
        return new EvaluationResult(
            new User((String) r.getPayload(), SharedConstants.User.DEFAULT_AUTH_DOMAIN), r);
      } else {
        return r.withWarning(ErrorCode.W121);
      }
    } else if (this.ops.size() == 2) {
      EvaluationResult r0 = this.ops.get(0).evaluate(record);
      EvaluationResult r1 = this.ops.get(1).evaluate(record);
      if (r0.getPayload() instanceof String && r1.getPayload() instanceof String) {
        String email = (String)r0.getPayload();
        String authDomain = (String)r1.getPayload();
        return new EvaluationResult(new User(email, authDomain), r0, r1);
      } else {
        return r1.withWarning(ErrorCode.W121);
      }
    } else {
      return this.ops.get(0).evaluate(record).withWarning(ErrorCode.W120);
    }
  }

}
TOP

Related Classes of org.yaac.server.egql.evaluator.function.UserFunction

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.