Package com.casamind.adware.client.presenter

Source Code of com.casamind.adware.client.presenter.CompanyEditPresenter$Display

/**
* Copyright 2010 Daniel Guermeur and Amy Unruh
*
*   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.
*
*   See http://connectrapp.appspot.com/ for a demo, and links to more information
*   about this app and the book that it accompanies.
*/
package com.casamind.adware.client.presenter;

import java.util.List;

import com.casamind.adware.client.event.EntityAddEvent;
import com.casamind.adware.client.event.EntityDeletedEvent;
import com.casamind.adware.client.event.EntityEditCancelledEvent;
import com.casamind.adware.client.event.EntityEditEvent;
import com.casamind.adware.client.event.EntityUpdatedEvent;
import com.casamind.adware.client.event.UserMessageEvent;
import com.casamind.adware.client.helper.RPCCall;
import com.casamind.adware.client.resources.ErrorMessages;
import com.casamind.adware.client.service.DataServiceAsync;
import com.casamind.adware.shared.AccessLevels;
import com.casamind.adware.shared.AuthTypes;
import com.casamind.adware.shared.EntityTypes;
import com.casamind.adware.shared.InfoTypes;
import com.casamind.adware.shared.Pages;
import com.casamind.adware.shared.model.CompanyDTO;
import com.casamind.adware.shared.model.PublisherDTO;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.ListDataProvider;

public class CompanyEditPresenter implements Presenter {
  public interface Display {
    HasValue<Boolean> isReceiveNewsLetter();

    HasValue<Boolean> isReceiveNotifications();

    HasValue<String> getLogin();

    HasValue<String> getName();

    HasValue<String> getStreet();

    HasValue<String> getCity();

    HasValue<String> getPhone();

    HasValue<String> getMobile();

    HasValue<String> getFax();

    HasValue<String> getEmail();

    HasValue<String> getWebsite();

    HasClickHandlers getSaveButton();

    HasClickHandlers getDeleteButton();

    HasClickHandlers getCancelButton();

    HasClickHandlers getNavigationLink();

    HasData<PublisherDTO> getPubisherGrid();

    Column<PublisherDTO, String> getClickableColumn();

    ListDataProvider<PublisherDTO> getDataProvider();

    ListBox getServicesList();

    void addDataDisplay(HasData<PublisherDTO> display);

    void hideNavigationLink();

    Widget asWidget();
  }

  private CompanyDTO company;
  private final DataServiceAsync rpcService;
  private final SimpleEventBus eventBus;
  private final Display display;
  private final ErrorMessages errorMessages = GWT.create(ErrorMessages.class);

  public CompanyEditPresenter(DataServiceAsync rpcService, SimpleEventBus eventBus, Display display) {
    this.rpcService = rpcService;
    this.eventBus = eventBus;
    this.display = display;
    this.company = new CompanyDTO();
    bind();
  }

  public CompanyEditPresenter(final DataServiceAsync rpcService, final SimpleEventBus eventBus, Display display, final Long id) {
    this(rpcService, eventBus, display);

    if (id == null) {
      this.company.setAccessLevel(AccessLevels.Company);
      this.display.getPubisherGrid().setRowCount(0);
      this.display.isReceiveNewsLetter().setValue(true);
      this.display.isReceiveNotifications().setValue(true);
    } else {
      this.display.hideNavigationLink();
      new RPCCall<CompanyDTO>() {
        @Override
        protected void callService(AsyncCallback<CompanyDTO> cb) {
          rpcService.getCompany(id, cb);
        }

        @Override
        public void onSuccess(CompanyDTO result) {
          if (result != null) {
            company = result;
            CompanyEditPresenter.this.display.getLogin().setValue(company.getLogin());
            CompanyEditPresenter.this.display.getName().setValue(company.getLastname());
            CompanyEditPresenter.this.display.getStreet().setValue(company.getStreet());
            CompanyEditPresenter.this.display.getCity().setValue(company.getCity());
            CompanyEditPresenter.this.display.getPhone().setValue(company.getPhone());
            CompanyEditPresenter.this.display.getMobile().setValue(company.getMobile());
            CompanyEditPresenter.this.display.getFax().setValue(company.getFax());
            CompanyEditPresenter.this.display.getEmail().setValue(company.getEmail());
            CompanyEditPresenter.this.display.getWebsite().setValue(company.getWebsite());
            CompanyEditPresenter.this.display.isReceiveNewsLetter().setValue(company.isReceiveNewsLetter());
            CompanyEditPresenter.this.display.isReceiveNotifications().setValue(company.isReceiveNotifications());
            String authProvider = company.getService();
            if (authProvider.equals(AuthTypes.GOOGLE))
              CompanyEditPresenter.this.display.getServicesList().setSelectedIndex(0);
            else if (authProvider.equals(AuthTypes.FACEBOOK))
              CompanyEditPresenter.this.display.getServicesList().setSelectedIndex(1);
            else if (authProvider.equals(AuthTypes.TWITTER))
              CompanyEditPresenter.this.display.getServicesList().setSelectedIndex(2);
            puopulateCellTable();
          } else {
            eventBus.fireEvent(new UserMessageEvent(null, InfoTypes.Error, 0));
          }
        }

        @Override
        public void onFailure(Throwable caught) {
          Window.alert(errorMessages.rpcError(caught.getMessage()));
        }
      }.retry(3);
    }
  }

  protected void puopulateCellTable() {
    new RPCCall<List<PublisherDTO>>() {
      @Override
      protected void callService(AsyncCallback<List<PublisherDTO>> cb) {
        rpcService.getPublishersByCompanyId(CompanyEditPresenter.this.company.getId(), cb);
      }

      @Override
      public void onSuccess(List<PublisherDTO> result) {
        if (result == null)
          return;
        for (PublisherDTO entity : result) {
          CompanyEditPresenter.this.display.getDataProvider().getList().add(entity);
        }
        CompanyEditPresenter.this.display.addDataDisplay(CompanyEditPresenter.this.display.getPubisherGrid());
      }

      @Override
      public void onFailure(Throwable caught) {
        Window.alert(errorMessages.rpcError(caught.getMessage()));
      }
    }.retry(3);

  }

  public void bind() {
    this.display.getSaveButton().addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        doSave();
      }
    });

    this.display.getDeleteButton().addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        doDelete();
      }

    });

    this.display.getCancelButton().addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        eventBus.fireEvent(new EntityEditCancelledEvent());
      }
    });

    this.display.getNavigationLink().addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        eventBus.fireEvent(new EntityAddEvent(EntityTypes.Publisher));
      }
    });
    this.display.getClickableColumn().setFieldUpdater(new FieldUpdater<PublisherDTO, String>() {
      public void update(int index, PublisherDTO object, String value) {
        eventBus.fireEvent(new EntityEditEvent(object, EntityTypes.Publisher));
      }
    });
  }

  public void go(final DeckPanel container) {
    for (int i = 0; i < container.getWidgetCount(); i++) {
      if (!Pages.isHomePage(container.getWidget(i).getClass()))
        container.remove(i);
    }
    container.add(display.asWidget());
    container.showWidget(container.getWidgetIndex(display.asWidget()));
  }

  private void doDelete() {
    if (company == null || company.getId() == null)
      return;
    if (Window.confirm(errorMessages.warningDeleteEntity(company.getDisplayName()))) {
      deleteEntity(company);
    }
  }

  private void deleteEntity(final CompanyDTO dto) {
    new RPCCall<Boolean>() {
      @Override
      protected void callService(AsyncCallback<Boolean> cb) {
        rpcService.deleteCompany(dto, cb);
      }

      @Override
      public void onSuccess(Boolean result) {
        if(result == true){
          eventBus.fireEvent(new EntityDeletedEvent());
          eventBus.fireEvent(new UserMessageEvent(null, InfoTypes.Info, 0));         
        }else {
          eventBus.fireEvent(new UserMessageEvent(null, InfoTypes.Error, 0));
        }
      }

      @Override
      public void onFailure(Throwable caught) {
        if (caught instanceof com.casamind.adware.shared.exception.NotLoggedInException) {
          Window.alert(errorMessages.youNeedToLogin());
        } else {
          Window.alert(errorMessages.rpcError(caught.getMessage()));
        }
      }
    }.retry(3);
  }

  private void doSave() {
    company.setLogin(display.getLogin().getValue().trim());
    company.setService(display.getServicesList().getItemText(display.getServicesList().getSelectedIndex()));
    company.setLastname(display.getName().getValue().trim());
    company.setStreet(display.getStreet().getValue().trim());
    company.setCity(display.getCity().getValue().trim());
    company.setPhone(display.getPhone().getValue().trim());
    company.setMobile(display.getMobile().getValue().trim());
    company.setFax(display.getFax().getValue().trim());
    company.setEmail(display.getEmail().getValue().trim());
    company.setWebsite(display.getWebsite().getValue().trim());
    company.setReceiveNewsLetter(display.isReceiveNewsLetter().getValue());
    company.setReceiveNotifications(display.isReceiveNotifications().getValue());

    // Do not retry on CREATE
    int retries = 3;
    if (company.getId() == null) {
      retries = 0;
    }
    new RPCCall<CompanyDTO>() {
      @Override
      protected void callService(AsyncCallback<CompanyDTO> cb) {
        if (company.getId() != null)
          rpcService.updateCompany(company, cb);
        else
          rpcService.createCompany(company, cb);
      }

      @Override
      public void onSuccess(CompanyDTO result) {
        if(result!=null){
          eventBus.fireEvent(new EntityUpdatedEvent(result));
          eventBus.fireEvent(new UserMessageEvent(null, InfoTypes.Info, 0));         
        }else {
          eventBus.fireEvent(new UserMessageEvent(null, InfoTypes.Error, 0));
        }
      }

      @Override
      public void onFailure(Throwable caught) {
        Window.alert(errorMessages.rpcError(caught.getMessage()));
      }
    }.retry(retries);
  }

}
TOP

Related Classes of com.casamind.adware.client.presenter.CompanyEditPresenter$Display

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.