Package com.casamind.adware.client

Source Code of com.casamind.adware.client.AppController

/**
* 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;

import com.casamind.adware.client.event.EntityAddEvent;
import com.casamind.adware.client.event.EntityAddEventHandler;
import com.casamind.adware.client.event.EntityDeletedEvent;
import com.casamind.adware.client.event.EntityDeletedEventHandler;
import com.casamind.adware.client.event.EntityEditCancelledEvent;
import com.casamind.adware.client.event.EntityEditCancelledEventHandler;
import com.casamind.adware.client.event.EntityEditEvent;
import com.casamind.adware.client.event.EntityEditEventHandler;
import com.casamind.adware.client.event.EntityUpdatedEvent;
import com.casamind.adware.client.event.EntityUpdatedEventHandler;
import com.casamind.adware.client.event.LogoutEvent;
import com.casamind.adware.client.event.LogoutEventHandler;
import com.casamind.adware.client.event.NavigationEvent;
import com.casamind.adware.client.event.NavigationEventHandler;
import com.casamind.adware.client.event.ProfileEditEvent;
import com.casamind.adware.client.event.ProfileEditEventHandler;
import com.casamind.adware.client.event.ShowEntityPopupEvent;
import com.casamind.adware.client.event.ShowEntityPopupEventHandler;
import com.casamind.adware.client.presenter.AdminSlotsPresenter;
import com.casamind.adware.client.presenter.CalendarPresenter;
import com.casamind.adware.client.presenter.CompanyEditPresenter;
import com.casamind.adware.client.presenter.ConsolePresenter;
import com.casamind.adware.client.presenter.EntityPopupPresenter;
import com.casamind.adware.client.presenter.OrderPresenter;
import com.casamind.adware.client.presenter.Presenter;
import com.casamind.adware.client.presenter.ProductEditPresenter;
import com.casamind.adware.client.presenter.PublisherEditPresenter;
import com.casamind.adware.client.presenter.ResourceEditPresenter;
import com.casamind.adware.client.presenter.SlotEditPresenter;
import com.casamind.adware.client.presenter.StatisticsPresenter;
import com.casamind.adware.client.service.DataServiceAsync;
import com.casamind.adware.client.service.MailServiceAsync;
import com.casamind.adware.client.view.AdminSlotsView;
import com.casamind.adware.client.view.CalendarView;
import com.casamind.adware.client.view.CompanyEditView;
import com.casamind.adware.client.view.ConsoleView;
import com.casamind.adware.client.view.EntityPopupView;
import com.casamind.adware.client.view.OrderView;
import com.casamind.adware.client.view.ProductEditView;
import com.casamind.adware.client.view.PublisherEditView;
import com.casamind.adware.client.view.ResourceEditView;
import com.casamind.adware.client.view.SlotEditView;
import com.casamind.adware.client.view.StatisticsView;
import com.casamind.adware.shared.AccessLevels;
import com.casamind.adware.shared.EntityTypes;
import com.casamind.adware.shared.Pages;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;

public class AppController implements ValueChangeHandler<String> {
  private final SimpleEventBus eventBus;
  private final DataServiceAsync dataModelService;
  private final MailServiceAsync mailService;
  private Presenter statisticsPresenter, consolePresenter, orderPresenter, adminSlotsPresenter, adminResourcesPresenter,
      calendarPresenter;
  private Long currentCompanyId;
  private Long currentPublisherId;
  private Long currentProductId;
  private Long currentSlotId;
  private Long currentResourceId;
  private int activePresenterIndex = -1;

  public AppController(DataServiceAsync dataModelService, MailServiceAsync mailService, SimpleEventBus eventBus) {
    this.eventBus = eventBus;
    this.mailService = mailService;
    this.dataModelService = dataModelService;
    bind();
  }

  private void bind() {
    History.addValueChangeHandler(this);

    eventBus.addHandler(NavigationEvent.TYPE, new NavigationEventHandler() {
      @Override
      public void onNavigation(NavigationEvent event) {
        switchHome(event.getDestination());
      }
    });

    eventBus.addHandler(ProfileEditEvent.TYPE, new ProfileEditEventHandler() {
      @Override
      public void onEditProfile(ProfileEditEvent event) {
        if (event.getEntityType() == EntityTypes.Company) {
          AppController.this.currentCompanyId = event.getEntity().getId();
          gotoEditCompany();
        } else if (event.getEntityType() == EntityTypes.Publisher) {
          AppController.this.currentPublisherId = event.getEntity().getId();
          gotoEditPublisher();
        } else if (Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Administrator) {
          gotoAdminSlots();
        }
      }
    });

    eventBus.addHandler(EntityAddEvent.TYPE, new EntityAddEventHandler() {
      @Override
      public void onAddEntity(EntityAddEvent event) {
        if (event.getEntityType() == EntityTypes.Company) {
          gotoAddNewCompany();
        } else if (event.getEntityType() == EntityTypes.Publisher) {
          gotoAddNewPublisher();
        } else if (event.getEntityType() == EntityTypes.Product) {
          gotoAddNewProduct();
        } else if (event.getEntityType() == EntityTypes.Resource) {
          gotoAddNewResource();
        } else {
          gotoAddNewEntity();
        }
      }
    });
    eventBus.addHandler(ShowEntityPopupEvent.TYPE, new ShowEntityPopupEventHandler() {
      @Override
      public void onShowEntityPopup(ShowEntityPopupEvent event) {
        EntityPopupPresenter EntityPopupPresenter = new EntityPopupPresenter(dataModelService, eventBus, new EntityPopupView(event.getEntity().getDisplayName(), event.getClickPoint()), event.getEntity());
        EntityPopupPresenter.go();
      }
    });

    eventBus.addHandler(EntityEditEvent.TYPE, new EntityEditEventHandler() {
      @Override
      public void onEditEntity(EntityEditEvent event) {
        if (event.getEntityType() == EntityTypes.Company) {
          AppController.this.currentCompanyId = event.getEntity().getId();
          gotoEditCompany();
        } else if (event.getEntityType() == EntityTypes.Publisher) {
          AppController.this.currentPublisherId = event.getEntity().getId();
          gotoEditPublisher();
        } else if (event.getEntityType() == EntityTypes.Product) {
          AppController.this.currentProductId = event.getEntity().getId();
          gotoEditProduct();
        } else if (event.getEntityType() == EntityTypes.Slot) {
          AppController.this.currentSlotId = event.getEntity().getId();
          gotoEditSlot();
        } else if (event.getEntityType() == EntityTypes.Resource) {
          AppController.this.currentResourceId = event.getEntity().getId();
          gotoEditResource();
        }
      }
    });

    eventBus.addHandler(EntityEditCancelledEvent.TYPE, new EntityEditCancelledEventHandler() {
      @Override
      public void onEditEntityCancelled(EntityEditCancelledEvent event) {
        doEditEntityCancelled();
      }
    });

    eventBus.addHandler(EntityUpdatedEvent.TYPE, new EntityUpdatedEventHandler() {
      @Override
      public void onEntityUpdated(EntityUpdatedEvent event) {
        doEntityUpdated();
      }
    });
    eventBus.addHandler(EntityDeletedEvent.TYPE, new EntityDeletedEventHandler() {
      @Override
      public void onEntityDeleted(EntityDeletedEvent event) {
        doEntityDeleted();
      }
    });

    eventBus.addHandler(LogoutEvent.TYPE, new LogoutEventHandler() {
      @Override
      public void onLogout(LogoutEvent event) {
        gotoLogout();
      }
    });
  }

  private void gotoAdminResources() {
    History.newItem("adminResources");
  }

  private void doAdminResources() {
    if (Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Administrator) {
      Window.alert("not implemented");
    }
  }

  private void gotoAdminSlots() {
    History.newItem("adminSlots");
  }

  private void doAdminSlots() {
    if (Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Administrator) {
      if (adminSlotsPresenter != null)
        adminSlotsPresenter.go(Adware.get().getMainPanel());
      else {
        Presenter presenter = new AdminSlotsPresenter(dataModelService, mailService, eventBus, new AdminSlotsView(), Adware.get().getCurrentUser());
        presenter.go(Adware.get().getMainPanel());
        adminSlotsPresenter = presenter;
      }
    }
  }

  private void gotoStatistics() {
    History.newItem("statistics");
  }

  private void doStatistics() {
    if (statisticsPresenter != null)
      statisticsPresenter.go(Adware.get().getMainPanel());
    else {
      Presenter presenter = new StatisticsPresenter(dataModelService, eventBus, new StatisticsView(), Adware.get().getCurrentUser());
      presenter.go(Adware.get().getMainPanel());
      statisticsPresenter = presenter;
    }
  }

  private void gotoCalendar() {
    History.newItem("calendar");
  }

  private void doCalendar() {
    if (calendarPresenter != null)
      calendarPresenter.go(Adware.get().getMainPanel());
    else {
      Presenter presenter = new CalendarPresenter(dataModelService, eventBus, new CalendarView(), Adware.get().getCurrentUser());
      presenter.go(Adware.get().getMainPanel());
      calendarPresenter = presenter;
    }
  }

  private void gotoConsole() {
    History.newItem("console");
  }

  private void doConsole() {
    if (consolePresenter != null)
      consolePresenter.go(Adware.get().getMainPanel());
    else {
      Presenter presenter = new ConsolePresenter(dataModelService, eventBus, new ConsoleView(), Adware.get().getCurrentUser());
      presenter.go(Adware.get().getMainPanel());
      consolePresenter = presenter;
    }
  }

  private void gotoOrder() {
    History.newItem("order");
  }

  private void doOrder() {
    if (orderPresenter != null)
      orderPresenter.go(Adware.get().getMainPanel());
    else {
      Presenter presenter = new OrderPresenter(dataModelService, eventBus, new OrderView(), Adware.get().getCurrentUser());
      presenter.go(Adware.get().getMainPanel());
      orderPresenter = presenter;
    }
  }

  private void gotoAddNewEntity() {
    History.newItem("add");
  }

  private void doAddNewEntity() {
    switch (Adware.get().getCurrentUser().getAccessLevel()) {
      case 1:
        doAddNewCompany();
        break;
      case 2:
        doAddNewPublisher();
        break;
      case 3:
        doAddNewProduct();
        break;
    }
  }

  private void doEditEntity() {
    switch (Adware.get().getCurrentUser().getAccessLevel()) {
      case 1:
        gotoEditCompany();
        break;
      case 2:
        gotoEditPublisher();
        break;
      case 3:
        gotoEditProduct();
        break;
      default:
        gotoHome();
        return;
    }

  }

  private void gotoEditCompany() {
    String nextToken = "editCompany";
    String currentToken = History.getToken();
    if (!currentToken.equals(nextToken)) {
      History.newItem(nextToken);
    } else {
      doEditCompany();
    }
  }

  private void doEditCompany() {
    if (Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Administrator || Adware.get().getCurrentUser().getId().longValue() == currentCompanyId.longValue()) {
      new CompanyEditPresenter(dataModelService, eventBus, new CompanyEditView(), currentCompanyId).go(Adware.get().getMainPanel());
    }
  }

  private void gotoEditPublisher() {
    String nextToken = "editPublisher";
    String currentToken = History.getToken();
    if (!currentToken.equals(nextToken)) {
      History.newItem(nextToken);
    } else {
      doEditPublisher();
    }
  }

  private void doEditPublisher() {
    if (Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Administrator || Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Company || Adware.get().getCurrentUser().getId().longValue() == currentPublisherId.longValue()) {
      new PublisherEditPresenter(dataModelService, eventBus, new PublisherEditView(), currentPublisherId).go(Adware.get().getMainPanel());
    }
  }

  private void gotoEditProduct() {
    String nextToken = "editProduct";
    String currentToken = History.getToken();
    if (!currentToken.equals(nextToken)) {
      History.newItem(nextToken);
    } else {
      doEditProduct();
    }
  }

  private void doEditProduct() {
    new ProductEditPresenter(dataModelService, eventBus, new ProductEditView(), currentProductId).go(Adware.get().getMainPanel());
  }

  private void gotoEditSlot() {
    String nextToken = "editSlot";
    String currentToken = History.getToken();
    if (!currentToken.equals(nextToken)) {
      History.newItem(nextToken);
    } else {
      doEditSlot();
    }
  }

  private void doEditResource() {
    new ResourceEditPresenter(dataModelService, eventBus, new ResourceEditView(), currentResourceId).go(Adware.get().getMainPanel());
  }

  private void gotoEditResource() {
    String nextToken = "editResource";
    String currentToken = History.getToken();
    if (!currentToken.equals(nextToken)) {
      History.newItem(nextToken);
    } else {
      doEditResource();
    }
  }

  private void doEditSlot() {
    new SlotEditPresenter(dataModelService, mailService, eventBus, new SlotEditView(), currentSlotId).go(Adware.get().getMainPanel());
  }


  private void gotoAddNewCompany() {
    History.newItem("addCompany");
  }

  private void doAddNewCompany() {
    if (Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Administrator) {
      new CompanyEditPresenter(dataModelService, eventBus, new CompanyEditView(), null).go(Adware.get().getMainPanel());
    }
  }

  private void gotoAddNewPublisher() {
    History.newItem("addPublisher");
  }

  private void doAddNewPublisher() {
    if (Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Administrator || Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Company) {
      new PublisherEditPresenter(dataModelService, eventBus, new PublisherEditView(), null).go(Adware.get().getMainPanel());
    }
  }

  private void gotoAddNewProduct() {
    History.newItem("addProduct");
  }

  private void doAddNewProduct() {
    new ProductEditPresenter(dataModelService, eventBus, new ProductEditView(), null).go(Adware.get().getMainPanel());
  }

  private void gotoAddNewResource() {
    History.newItem("addResource");
  }

  private void doAddNewResource() {
    new ResourceEditPresenter(dataModelService, eventBus, new ResourceEditView(), null).go(Adware.get().getMainPanel());
  }

  private void gotoLogout() {
    History.newItem("login");
  }

  private void doLogout() {
    Adware.get().showLoginView(Adware.get().getCurrentUser());
  }

  private void doEditEntityCancelled() {
    gotoHome();
  }

  private void doEntityDeleted() {
    gotoHome();
  }

  private void doEntityUpdated() {
    gotoHome();
  }

  public void switchHome(int destination) {
    if (destination == Pages.Statistics) {
      gotoStatistics();
    } else if (destination == Pages.Console) {
      gotoConsole();
    } else if (destination == Pages.Order) {
      gotoOrder();
    } else if (destination == Pages.Calendar) {
      gotoCalendar();
    } else {
      gotoHome();
    }
    this.activePresenterIndex = destination;
  }

  public void gotoHome() {
    if (this.activePresenterIndex == Pages.Statistics) {
      gotoStatistics();
    } else if (this.activePresenterIndex == Pages.Console) {
      gotoConsole();
    } else if (this.activePresenterIndex == Pages.Order) {
      gotoOrder();
    } else if (this.activePresenterIndex == Pages.Calendar) {
      gotoCalendar();
    } else {
      gotoConsole();
    }
  }

  public void go() {
    // gotoHome();
    navigate(History.getToken());
  }

  public void navigate(String token) {
    if (token != null) {
      if (token.equals("console")) {
        doConsole();
        return;
      } else if (token.equals("order")) {
        doOrder();
        return;
      } else if (token.equals("statistics")) {
        doStatistics();
        return;
      } else if (token.equals("calendar")) {
        doCalendar();
        return;
      } else if (token.equals("editSlot")) {
        doEditSlot();
        return;
      } else if (token.equals("editResource")) {
        doEditResource();
        return;
      } else if (token.equals("editProduct")) {
        doEditProduct();
        return;
      } else if (token.equals("editPublisher")) {
        doEditPublisher();
        return;
      } else if (token.equals("editCompany")) {
        doEditCompany();
        return;
      } else if (token.equals("add")) {
        doAddNewEntity();
        return;
      } else if (token.equals("addResource")) {
        doAddNewResource();
        return;
      } else if (token.equals("addProduct")) {
        doAddNewProduct();
        return;
      } else if (token.equals("addPublisher")) {
        doAddNewPublisher();
        return;
      } else if (token.equals("addCompany")) {
        doAddNewCompany();
        return;
      } else if (token.equals("adminSlots")) {
        doAdminSlots();
        return;
      } else if (token.equals("adminResources")) {
        doAdminResources();
        return;
      } else if (token.equals("login")) {
        doLogout();
        return;
      } else {
        gotoHome();
      }
    }
  }

  public void onValueChange(ValueChangeEvent<String> event) {
    String token = event.getValue();
    navigate(token);
  }

  // private MessageListPresenter getMessageListPresenter() {
  // if (messageListPresenter == null) {
  // GWT.log("AppController: Creating MessageListPresenter");
  // messageListPresenter = new MessageListPresenter(messagesService,
  // eventBus, new MessageListView());
  // }
  //
  // return messageListPresenter;
  // }

}
TOP

Related Classes of com.casamind.adware.client.AppController

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.