All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.ui.PageRequestBuilderPojo Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.way.ui;

import static com.google.common.collect.Sets.newHashSet;

import java.util.Optional;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.sitebricks.headless.Request;

/**
 * @author [email protected] (Marcio Endo)
 */
class PageRequestBuilderPojo
    implements
    PageRequestBuilder {

  private final Provider requests;
  private final Provider servletRequests;

  private final Set methods = newHashSet();
  private final Set conditions = newHashSet();

  @Inject
  public PageRequestBuilderPojo(Provider requests,
                                Provider servletRequests) {
    this.requests = requests;
    this.servletRequests = servletRequests;
  }

  @Override
  public  PageRequestBuilder put(Class key, Optional optional) {
    store(key, optional.orElse(null));
    return this;
  }

  @Override
  public  PageRequestBuilder put(Class key, com.google.common.base.Optional optional) {
    store(key, optional.orNull());
    return this;
  }

  @Override
  public  PageRequestBuilder put(Class key, T value) {
    store(key, value);
    return this;
  }

  @Override
  public RespondToBuilder respondTo() {
    return new RespondToBuilderPojo();
  }

  PageRequestBuilderPojo after(PageRequest request) {
    request
        .toPojo()
        .addAllTo(conditions);
    return this;
  }

  Method getMethod() {
    Request request = requests.get();
    return Method.parse(request);
  }

  Provider getServletRequests() {
    return servletRequests;
  }

  Set getMethods() {
    return methods;
  }

  Set getConditions() {
    return conditions;
  }

  private PageRequest build() {
    return new PageRequestPojo(this);
  }

  private void store(Class key, Object value) {
    HttpServletRequest request = servletRequests.get();
    request.setAttribute(key.getName(), value);
  }

  private class RespondToBuilderPojo
      implements
      PageRequestBuilder.RespondToBuilder,
      PageRequestBuilder.GetBuilder,
      PageRequestBuilder.PostBuilder {

    @Override
    public GetBuilder get() {
      methods.add(Method.GET);
      return this;
    }

    @Override
    public PostBuilder post() {
      methods.add(Method.POST);
      return this;
    }

    @Override
    public SayBuilder always() {
      PageCondition condition = PageCondition.trueValue(conditions);
      return new SayBuilderPageRequest(condition);
    }

    @Override
    public SayBuilder otherwise() {
      PageCondition condition = PageCondition.trueValue(conditions);
      return new SayBuilderPageRequest(condition);
    }

    @Override
    public WhenBuilder when(Class key) {
      return new WhenBuilderPojo(key);
    }

  }

  private class SayBuilderPageRequest
      implements
      PageRequestBuilder.SayBuilder {

    private final PageCondition condition;

    public SayBuilderPageRequest(PageCondition condition) {
      this.condition = condition;
    }

    @Override
    public PageRequest sayOk() {
      condition.sayOk();
      return build();
    }

    @Override
    public PageRequest sayNotFound() {
      condition.sayNotFound();
      return build();
    }

  }

  private class WhenBuilderPojo
      implements
      PageRequestBuilder.WhenBuilder {

    private final Class key;

    public WhenBuilderPojo(Class key) {
      this.key = key;
    }

    @Override
    public SayBuilder notPresent() {
      PageCondition condition = PageCondition.requestNotPresent(conditions, key);
      return new SayBuilderConditionBuilder(condition);
    }

  }

  private class SayBuilderConditionBuilder
      implements
      PageRequestBuilder.SayBuilder {

    private final PageCondition condition;

    public SayBuilderConditionBuilder(PageCondition condition) {
      this.condition = condition;
    }

    @Override
    public ConditionBuilder sayOk() {
      condition.sayOk();
      return new RespondToBuilderPojo();
    }

    @Override
    public ConditionBuilder sayNotFound() {
      condition.sayNotFound();
      return new RespondToBuilderPojo();
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy