package com.casamind.adware.client.presenter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.casamind.adware.client.Adware;
import com.casamind.adware.client.event.EntityEditEvent;
import com.casamind.adware.client.event.NavigationEvent;
import com.casamind.adware.client.helper.RPCCall;
import com.casamind.adware.client.resources.ErrorMessages;
import com.casamind.adware.client.resources.UIConstants;
import com.casamind.adware.client.service.DataServiceAsync;
import com.casamind.adware.client.view.ConsoleView;
import com.casamind.adware.shared.AccessLevels;
import com.casamind.adware.shared.EntityTypes;
import com.casamind.adware.shared.Pages;
import com.casamind.adware.shared.model.CompanySummaryDTO;
import com.casamind.adware.shared.model.EntitySummary;
import com.casamind.adware.shared.model.PublisherSummaryDTO;
import com.casamind.adware.shared.model.SlotSummaryDTO;
import com.casamind.adware.shared.model.UserAccountDTO;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
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.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 ConsolePresenter implements Presenter {
private UserAccountDTO user;
private List<EntitySummary> compList;
private List<EntitySummary> pubList;
private final DataServiceAsync rpcService;
private final SimpleEventBus eventBus;
private final Display display;
private final ErrorMessages errorMessages = GWT.create(ErrorMessages.class);
private final UIConstants uiConstants = GWT.create(UIConstants.class);
public interface Display {
HasClickHandlers getSearchButton();
HasData<SlotSummaryDTO> getSlotsGrid();
Column<SlotSummaryDTO, String> getSlotsClickableColumn();
ListDataProvider<SlotSummaryDTO> getSlotsDataProvider();
ListBox getPublisherList();
ListBox getCompanyList();
void addSlotsDataDisplay(HasData<SlotSummaryDTO> display);
void redrawSlotsGrid();
void setEnableSearchButton(boolean b);
HasClickHandlers getCalendarLink();
HasClickHandlers getOrderLink();
HasClickHandlers getStatisticsLink();
Boolean getIsConfigured();
Boolean getIsSequential();
Integer getStatus();
Widget asWidget();
Date getStartDate();
Date getEndDate();
}
public ConsolePresenter(DataServiceAsync rpcService, SimpleEventBus eventBus, Display display) {
this.rpcService = rpcService;
this.eventBus = eventBus;
this.display = display;
this.compList = new ArrayList<EntitySummary>();
this.pubList = new ArrayList<EntitySummary>();
bind();
}
public ConsolePresenter(final DataServiceAsync rpcService, SimpleEventBus eventBus, Display display, UserAccountDTO user) {
this(rpcService, eventBus, display);
this.user = user;
if (Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Publisher) {
this.pubList.add(Adware.get().getCurrentPublisher());
this.compList.add(Adware.get().getCurrentCompany());
this.display.getPublisherList().addItem(Adware.get().getCurrentPublisher().getDisplayName());
this.display.getCompanyList().addItem(Adware.get().getCurrentCompany().getDisplayName());
} else if (Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Company) {
this.display.getCompanyList().addItem(Adware.get().getCurrentCompany().getDisplayName());
this.compList.add(Adware.get().getCurrentCompany());
populatePublisherList(Adware.get().getCurrentCompany().getId(), null);
} else {
// the user is Admin
populateCompanyList(null, null);
}
this.display.getSlotsGrid().setRowCount(0);
}
private void populateCompanyList(final Long companyId, final Long publisherId) {
this.compList.add(new CompanySummaryDTO(new Long(0), "*"));
this.display.getCompanyList().addItem("*");
new RPCCall<List<EntitySummary>>() {
@Override
protected void callService(AsyncCallback<List<EntitySummary>> cb) {
rpcService.getCompanies(cb);
}
@Override
public void onSuccess(List<EntitySummary> result) {
for (EntitySummary entity : result) {
ConsolePresenter.this.display.getCompanyList().addItem(entity.getDisplayName());
ConsolePresenter.this.compList.add((CompanySummaryDTO) entity);
}
if (ConsolePresenter.this.display.getCompanyList().getItemCount() > 0) {
if (companyId == null) {
ConsolePresenter.this.display.getCompanyList().setSelectedIndex(0);
populatePublisherList(ConsolePresenter.this.compList.get(0).getId(), null);
} else {
int index = indexOf(ConsolePresenter.this.compList, companyId);
if (index != -1) {
ConsolePresenter.this.display.getCompanyList().setSelectedIndex(index);
populatePublisherList(companyId, publisherId);
}
}
}
}
@Override
public void onFailure(Throwable caught) {
Window.alert(errorMessages.rpcError(caught.getMessage()));
}
}.retry(3);
this.display.getCompanyList().addChangeHandler(new ChangeHandler() {
public void onChange(ChangeEvent event) {
int index = ConsolePresenter.this.display.getCompanyList().getSelectedIndex();
Long id = ConsolePresenter.this.compList.get(index).getId();
if (id == 0) {
display.getPublisherList().clear();
pubList.clear();
display.getPublisherList().addItem("*");
pubList.add(new PublisherSummaryDTO(new Long(0), "*", "", new Long(0)));
} else {
populatePublisherList(id, null);
}
}
});
}
private void populatePublisherList(final Long companyId, final Long publisherId) {
this.display.getPublisherList().clear();
this.pubList.clear();
display.getPublisherList().addItem("*");
pubList.add(new PublisherSummaryDTO(new Long(0), "*", "", new Long(0)));
new RPCCall<List<PublisherSummaryDTO>>() {
@Override
protected void callService(AsyncCallback<List<PublisherSummaryDTO>> cb) {
rpcService.getPublisherSummariesByCompanyId(companyId, cb);
}
@Override
public void onSuccess(List<PublisherSummaryDTO> result) {
for (PublisherSummaryDTO entity : result) {
ConsolePresenter.this.display.getPublisherList().addItem(entity.getDisplayName());
ConsolePresenter.this.pubList.add(entity);
}
if (ConsolePresenter.this.display.getPublisherList().getItemCount() > 0) {
if (publisherId == null) {
ConsolePresenter.this.display.getPublisherList().setSelectedIndex(0);
} else {
int index = indexOf(ConsolePresenter.this.pubList, publisherId);
if (index != -1) {
ConsolePresenter.this.display.getPublisherList().setSelectedIndex(index);
}
}
}
}
@Override
public void onFailure(Throwable caught) {
Window.alert(errorMessages.rpcError(caught.getMessage()));
}
}.retry(3);
}
private void populateCellTables(List<SlotSummaryDTO> list) {
for (SlotSummaryDTO entity : list) {
this.display.getSlotsDataProvider().getList().add(entity);
}
this.display.addSlotsDataDisplay(this.display.getSlotsGrid());
}
private void bind() {
this.display.getSearchButton().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
display.getSlotsDataProvider().getList().clear();
doSearch();
}
});
this.display.getStatisticsLink().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
eventBus.fireEvent(new NavigationEvent(Pages.Statistics));
}
});
this.display.getOrderLink().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
eventBus.fireEvent(new NavigationEvent(Pages.Order));
}
});
this.display.getCalendarLink().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
eventBus.fireEvent(new NavigationEvent(Pages.Calendar));
}
});
this.display.getSlotsClickableColumn().setFieldUpdater(new FieldUpdater<SlotSummaryDTO, String>() {
public void update(int index, SlotSummaryDTO object, String value) {
eventBus.fireEvent(new EntityEditEvent(object, EntityTypes.Slot));
}
});
}
private int indexOf(List<EntitySummary> list, Long id) {
for (EntitySummary entity : list) {
if (entity.getId().longValue() == id.longValue()) {
return list.indexOf(entity);
}
}
return -1;
}
private void doSearch() {
display.setEnableSearchButton(false);
new RPCCall<List<SlotSummaryDTO>>() {
@Override
protected void callService(AsyncCallback<List<SlotSummaryDTO>> cb) {
rpcService.querySlotSummaries(((EntitySummary) compList.get(display.getCompanyList().getSelectedIndex())).getId(), ((EntitySummary) pubList.get(display.getPublisherList().getSelectedIndex())).getId(), display.getStartDate(), display.getEndDate(), display.getIsConfigured(), display.getIsSequential(), display.getStatus(), cb);
}
@Override
public void onSuccess(List<SlotSummaryDTO> result) {
populateCellTables(result);
display.setEnableSearchButton(true);
}
@Override
public void onFailure(Throwable caught) {
Window.alert(errorMessages.rpcError(caught.getMessage()));
display.setEnableSearchButton(true);
}
}.retry(3);
}
@Override
public void go(DeckPanel container) {
for (int i = 0; i < container.getWidgetCount(); i++) {
if (!Pages.isHomePage(container.getWidget(i).getClass()))
container.remove(i);
}
for (int i = 0; i < container.getWidgetCount(); i++) {
if (container.getWidget(i).getClass().equals(ConsoleView.class)) {
container.showWidget(i);
return;
}
}
container.add(display.asWidget());
container.showWidget(container.getWidgetIndex(display.asWidget()));
}
}