package com.casamind.adware.client.presenter;
import java.util.ArrayList;
import java.util.List;
import com.casamind.adware.client.Adware;
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.CompanySummaryDTO;
import com.casamind.adware.shared.model.EntitySummary;
import com.casamind.adware.shared.model.ProductSummaryDTO;
import com.casamind.adware.shared.model.PublisherDTO;
import com.casamind.adware.shared.model.SlotSummaryDTO;
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 PublisherEditPresenter implements Presenter {
private PublisherDTO publisher;
private List<CompanySummaryDTO> compList;
private final DataServiceAsync rpcService;
private final SimpleEventBus eventBus;
private final Display display;
private final ErrorMessages errorMessages = GWT.create(ErrorMessages.class);
public interface Display {
HasValue<String> getLogin();
HasValue<Boolean> isReceiveNewsLetter();
HasValue<Boolean> isReceiveNotifications();
HasValue<String> getFirstname();
HasValue<String> getEmail();
HasValue<String> getPhone();
HasValue<String> getLastname();
HasClickHandlers getSaveButton();
HasClickHandlers getDeleteButton();
HasClickHandlers getCancelButton();
HasClickHandlers getNavigationLink();
HasData<ProductSummaryDTO> getProductGrid();
HasData<SlotSummaryDTO> getSlotGrid();
ListBox getServicesList();
ListBox getCompanyList();
Column<ProductSummaryDTO, String> getProductClickableColumn();
Column<SlotSummaryDTO, String> getSlotClickableColumn();
ListDataProvider<ProductSummaryDTO> getProductDataProvider();
ListDataProvider<SlotSummaryDTO> getSlotDataProvider();
void addProductDataDisplay(HasData<ProductSummaryDTO> display);
void addSlotDataDisplay(HasData<SlotSummaryDTO> display);
void hideNavigationLink();
Widget asWidget();
}
public PublisherEditPresenter(DataServiceAsync rpcService, SimpleEventBus eventBus, Display display) {
this.rpcService = rpcService;
this.eventBus = eventBus;
this.display = display;
this.publisher = new PublisherDTO();
this.compList = new ArrayList<CompanySummaryDTO>();
bind();
}
public PublisherEditPresenter(final DataServiceAsync rpcService, SimpleEventBus eventBus, Display display, final Long id) {
this(rpcService, eventBus, display);
if (id == null) {
if (Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Company) {
this.display.getCompanyList().addItem(Adware.get().getCurrentCompany().getDisplayName());
this.compList.add(Adware.get().getCurrentCompany());
} else {
// the user is Admin
populateCompanyList(null);
}
this.publisher.setAccessLevel(AccessLevels.Publisher);
this.display.getProductGrid().setRowCount(0);
this.display.getSlotGrid().setRowCount(0);
this.display.isReceiveNewsLetter().setValue(true);
this.display.isReceiveNotifications().setValue(true);
} else {
this.display.hideNavigationLink();
new RPCCall<PublisherDTO>() {
@Override
protected void callService(AsyncCallback<PublisherDTO> cb) {
rpcService.getPublisher(id, cb);
}
@Override
public void onSuccess(PublisherDTO result) {
if (result != null) {
publisher = result;
PublisherEditPresenter.this.display.getLogin().setValue(publisher.getLogin());
PublisherEditPresenter.this.display.getFirstname().setValue(publisher.getFirstname());
PublisherEditPresenter.this.display.getLastname().setValue(publisher.getLastname());
PublisherEditPresenter.this.display.getEmail().setValue(publisher.getEmail());
PublisherEditPresenter.this.display.getPhone().setValue(publisher.getPhone());
PublisherEditPresenter.this.display.isReceiveNewsLetter().setValue(publisher.isReceiveNewsLetter());
PublisherEditPresenter.this.display.isReceiveNotifications().setValue(publisher.isReceiveNotifications());
String authProvider = publisher.getService();
if (authProvider.equals(AuthTypes.GOOGLE))
PublisherEditPresenter.this.display.getServicesList().setSelectedIndex(0);
else if (authProvider.equals(AuthTypes.FACEBOOK))
PublisherEditPresenter.this.display.getServicesList().setSelectedIndex(1);
else if (authProvider.equals(AuthTypes.TWITTER))
PublisherEditPresenter.this.display.getServicesList().setSelectedIndex(2);
selectItemsInLists(publisher.getCompany().getId());
populateCellTables();
} else {
Window.alert(errorMessages.unknownError());
}
}
@Override
public void onFailure(Throwable caught) {
Window.alert(errorMessages.rpcError(caught.getMessage()));
}
}.retry(3);
}
}
private void selectItemsInLists(Long companyId) {
if (Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Publisher || Adware.get().getCurrentUser().getAccessLevel() == AccessLevels.Company) {
this.compList.add(Adware.get().getCurrentCompany());
this.display.getCompanyList().addItem(Adware.get().getCurrentCompany().getDisplayName());
} else {
setSelectedItems(publisher.getCompany().getId());
}
int index = indexOf(PublisherEditPresenter.this.compList, companyId);
if (index != -1) {
PublisherEditPresenter.this.display.getCompanyList().setSelectedIndex(index);
}
}
private void setSelectedItems(Long companyId) {
populateCompanyList(companyId);
}
private void populateCompanyList(final Long companyId) {
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) {
PublisherEditPresenter.this.display.getCompanyList().addItem(entity.getDisplayName());
PublisherEditPresenter.this.compList.add((CompanySummaryDTO) entity);
}
if (PublisherEditPresenter.this.display.getCompanyList().getItemCount() > 0) {
if (companyId == null) {
PublisherEditPresenter.this.display.getCompanyList().setSelectedIndex(0);
} else {
int compIndex = indexOf(PublisherEditPresenter.this.compList, companyId);
if (compIndex != -1) {
PublisherEditPresenter.this.display.getCompanyList().setSelectedIndex(compIndex);
}
}
}
}
@Override
public void onFailure(Throwable caught) {
Window.alert(errorMessages.rpcError(caught.getMessage()));
}
}.retry(3);
}
private void populateCellTables() {
for (ProductSummaryDTO entity : this.publisher.getProducts()) {
this.display.getProductDataProvider().getList().add(entity);
}
for (SlotSummaryDTO entity : this.publisher.getSlots()) {
this.display.getSlotDataProvider().getList().add(entity);
}
this.display.addProductDataDisplay(this.display.getProductGrid());
this.display.addSlotDataDisplay(this.display.getSlotGrid());
}
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.Product));
}
});
this.display.getProductClickableColumn().setFieldUpdater(new FieldUpdater<ProductSummaryDTO, String>() {
public void update(int index, ProductSummaryDTO object, String value) {
eventBus.fireEvent(new EntityEditEvent(object, EntityTypes.Product));
}
});
this.display.getSlotClickableColumn().setFieldUpdater(new FieldUpdater<SlotSummaryDTO, String>() {
public void update(int index, SlotSummaryDTO object, String value) {
eventBus.fireEvent(new EntityEditEvent(object, EntityTypes.Slot));
}
});
}
private void doDelete() {
if (publisher == null || publisher.getId() == null)
return;
if (Window.confirm(errorMessages.warningDeleteEntity(publisher.getDisplayName()))) {
deleteEntity(publisher);
}
}
private void deleteEntity(final PublisherDTO dto) {
new RPCCall<Boolean>() {
@Override
protected void callService(AsyncCallback<Boolean> cb) {
rpcService.deletePublisher(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.toString()));
}
}
}.retry(3);
}
private void doSave() {
publisher.setCompany(compList.get(display.getCompanyList().getSelectedIndex()));
publisher.setLogin(display.getLogin().getValue().trim());
publisher.setService(display.getServicesList().getItemText(display.getServicesList().getSelectedIndex()));
publisher.setLastname(display.getLastname().getValue().trim());
publisher.setFirstname(display.getFirstname().getValue().trim());
publisher.setPhone(display.getPhone().getValue().trim());
publisher.setEmail(display.getEmail().getValue().trim());
publisher.setReceiveNewsLetter(display.isReceiveNewsLetter().getValue());
publisher.setReceiveNotifications(display.isReceiveNotifications().getValue());
// Do not retry on CREATE
int retries = 3;
if (publisher.getId() == null) {
retries = 0;
}
new RPCCall<PublisherDTO>() {
@Override
protected void callService(AsyncCallback<PublisherDTO> cb) {
if (publisher.getId() != null)
rpcService.updatePublisher(publisher, cb);
else
rpcService.createPublisher(publisher, cb);
}
@Override
public void onSuccess(PublisherDTO 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);
}
private int indexOf(List<CompanySummaryDTO> list, Long id) {
for (EntitySummary entity : list) {
if (entity.getId().longValue() == id.longValue()) {
return list.indexOf(entity);
}
}
return -1;
}
@Override
public void go(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()));
}
}