fr.ird.observe.client.spi.ClientDataContext Maven / Gradle / Ivy
package fr.ird.observe.client.spi;
/*
* #%L
* ObServe Toolkit :: Common Client
* %%
* Copyright (C) 2008 - 2017 IRD, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import com.google.common.collect.ImmutableList;
import fr.ird.observe.client.spi.context.DtoUIContext;
import fr.ird.observe.client.spi.context.OpenableDtoUIContext;
import fr.ird.observe.services.validation.ValidationDataContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ClientDataContext extends ValidationDataContext {
public static final String PROPERTY_OPEN_PROGRAM = "openProgram";
public static final String PROPERTY_OPEN_TRIP = "openTrip";
private static final String PROPERTY_OPEN_TRIP_SEINE = "openTripSeine";
private static final String PROPERTY_OPEN_TRIP_LONGLINE = "openTripLongline";
public static final String PROPERTY_OPEN_ROUTE = "openRoute";
public static final String PROPERTY_OPEN_ACTIVITY = "openActivity";
private static final String PROPERTY_OPEN_ACTIVITY_SEINE = "openActivitySeine";
private static final String PROPERTY_OPEN_ACTIVITY_LONGLINE = "openActivityLongline";
public static final String PROPERTY_OPEN_SET = "openSet";
private static final String PROPERTY_OPEN_SET_SEINE = "openSetSeine";
private static final String PROPERTY_OPEN_SET_LONGLINE = "openSetLongline";
private static final String PROPERTY_OPEN_PROGRAM_ID = "openProgramId";
private static final String PROPERTY_OPEN_TRIP_ID = "openTripId";
private static final String PROPERTY_OPEN_TRIP_SEINE_ID = "openTripSeineId";
private static final String PROPERTY_OPEN_TRIP_LONGLINE_ID = "openTripLonglineId";
private static final String PROPERTY_OPEN_ROUTE_ID = "openRouteId";
private static final String PROPERTY_OPEN_ACTIVITY_ID = "openActivityId";
private static final String PROPERTY_OPEN_ACTIVITY_SEINE_ID = "openActivitySeineId";
private static final String PROPERTY_OPEN_ACTIVITY_LONGLINE_ID = "openActivityLonglineId";
private static final String PROPERTY_OPEN_SET_ID = "openSetId";
private static final String PROPERTY_OPEN_SET_SEINE_ID = "openSetSeineId";
private static final String PROPERTY_OPEN_SET_LONGLINE_ID = "openSetLonglineId";
private static final String PROPERTY_ENABLED = "enabled";
private static final String PROPERTY_SELECTION_CHANGED = "selectionChanged";
/** Logger */
private static final Log log = LogFactory.getLog(ClientDataContext.class);
private static final long serialVersionUID = 1L;
private final ImmutableList types;
private final ImmutableList reverseTypes;
private String openProgramId;
private String openTripSeineId;
private String openTripLonglineId;
private String openRouteId;
private String openActivitySeineId;
private String openActivityLonglineId;
private String openSetSeineId;
private String openSetLonglineId;
protected boolean enabled;
private boolean selectionChanged;
public ClientDataContext(ImmutableList types) {
this.types = types;
LinkedList typeList = new LinkedList<>(types);
Collections.reverse(typeList);
this.reverseTypes = ImmutableList.copyOf(typeList);
}
private DtoUIContext acceptId(String s) {
DtoUIContext result = null;
for (DtoUIContext type : types) {
String[] entityFullClass = s.split("#")[0].split("\\.");
String typeName = type.getType().getSimpleName();
if (typeName.startsWith(entityFullClass[entityFullClass.length - 1])) {
result = type;
break;
}
}
return result;
}
public String getOpenProgramId() {
return openProgramId;
}
public void setOpenProgramId(String openProgramId) {
String oldValue = getOpenProgramId();
boolean oldValue2 = isOpenProgram();
this.openProgramId = openProgramId;
firePropertyChange(PROPERTY_OPEN_PROGRAM_ID, oldValue, this.openProgramId);
firePropertyChange(PROPERTY_OPEN_PROGRAM, oldValue2, isOpenProgram());
}
public String getOpenTripId() {
return openTripSeineId != null ? openTripSeineId : openTripLonglineId;
}
public String getOpenTripSeineId() {
return openTripSeineId;
}
public String getOpenTripLonglineId() {
return openTripLonglineId;
}
public void setOpenTripSeineId(String openTripId) {
String oldValue = getOpenTripId();
String oldValue2 = getOpenTripSeineId();
boolean oldValue3 = isOpenTrip();
boolean oldValue4 = isOpenTripSeine();
this.openTripSeineId = openTripId;
firePropertyChange(PROPERTY_OPEN_TRIP_ID, oldValue, this.openTripSeineId);
firePropertyChange(PROPERTY_OPEN_TRIP_SEINE_ID, oldValue2, this.openTripSeineId);
firePropertyChange(PROPERTY_OPEN_TRIP, oldValue3, isOpenTrip());
firePropertyChange(PROPERTY_OPEN_TRIP_SEINE, oldValue4, isOpenTripSeine());
}
public void setOpenTripLonglineId(String openTripId) {
String oldValue = getOpenTripId();
String oldValue2 = getOpenTripLonglineId();
boolean oldValue3 = isOpenTrip();
boolean oldValue4 = isOpenTripLongline();
this.openTripLonglineId = openTripId;
firePropertyChange(PROPERTY_OPEN_TRIP_ID, oldValue, this.openTripLonglineId);
firePropertyChange(PROPERTY_OPEN_TRIP_LONGLINE_ID, oldValue2, this.openTripLonglineId);
firePropertyChange(PROPERTY_OPEN_TRIP, oldValue3, isOpenTrip());
firePropertyChange(PROPERTY_OPEN_TRIP_LONGLINE, oldValue4, isOpenTripLongline());
}
public String getOpenRouteId() {
return openRouteId;
}
public void setOpenRouteId(String openRouteId) {
String oldValue = getOpenRouteId();
boolean oldValue2 = isOpenRoute();
this.openRouteId = openRouteId;
firePropertyChange(PROPERTY_OPEN_ROUTE_ID, oldValue, openRouteId);
firePropertyChange(PROPERTY_OPEN_ROUTE, oldValue2, isOpenRoute());
}
public String getOpenActivityId() {
return openActivitySeineId != null ? openActivitySeineId : openActivityLonglineId;
}
public String getOpenActivityLonglineId() {
return openActivityLonglineId;
}
public String getOpenActivitySeineId() {
return openActivitySeineId;
}
public void setOpenActivitySeineId(String openActivityId) {
String oldValue = getOpenActivityId();
String oldValue2 = getOpenActivitySeineId();
boolean oldValue3 = isOpenActivity();
boolean oldValue4 = isOpenActivitySeine();
this.openActivitySeineId = openActivityId;
firePropertyChange(PROPERTY_OPEN_ACTIVITY_ID, oldValue, this.openActivitySeineId);
firePropertyChange(PROPERTY_OPEN_ACTIVITY_SEINE_ID, oldValue2, this.openActivitySeineId);
firePropertyChange(PROPERTY_OPEN_ACTIVITY, oldValue3, isOpenActivity());
firePropertyChange(PROPERTY_OPEN_ACTIVITY_SEINE, oldValue4, isOpenActivitySeine());
}
public void setOpenActivityLonglineId(String openActivityId) {
String oldValue = getOpenActivityId();
String oldValue2 = getOpenActivityLonglineId();
boolean oldValue3 = isOpenActivity();
boolean oldValue4 = isOpenActivityLongline();
this.openActivityLonglineId = openActivityId;
firePropertyChange(PROPERTY_OPEN_ACTIVITY_ID, oldValue, this.openActivityLonglineId);
firePropertyChange(PROPERTY_OPEN_ACTIVITY_LONGLINE_ID, oldValue2, this.openActivityLonglineId);
firePropertyChange(PROPERTY_OPEN_ACTIVITY, oldValue3, isOpenActivity());
firePropertyChange(PROPERTY_OPEN_ACTIVITY_LONGLINE, oldValue4, isOpenActivityLongline());
}
private String getOpenSetId() {
return openSetSeineId != null ? openSetSeineId : openSetLonglineId;
}
public String getOpenSetLonglineId() {
return openSetSeineId;
}
public String getOpenSetSeineId() {
return openSetLonglineId;
}
public void setOpenSetSeineId(String openSetId) {
String oldValue = getOpenSetId();
String oldValue2 = getOpenSetSeineId();
boolean oldValue3 = isOpenSet();
boolean oldValue4 = isOpenSetSeine();
this.openSetSeineId = openSetId;
firePropertyChange(PROPERTY_OPEN_SET_ID, oldValue, this.openSetSeineId);
firePropertyChange(PROPERTY_OPEN_SET_SEINE_ID, oldValue2, this.openSetSeineId);
firePropertyChange(PROPERTY_OPEN_SET, oldValue3, isOpenSet());
firePropertyChange(PROPERTY_OPEN_SET_SEINE, oldValue4, isOpenSetSeine());
}
public void setOpenSetLonglineId(String openSetId) {
String oldValue = getOpenSetId();
String oldValue2 = getOpenSetLonglineId();
boolean oldValue3 = isOpenSet();
boolean oldValue4 = isOpenSetLongline();
this.openSetLonglineId = openSetId;
firePropertyChange(PROPERTY_OPEN_SET_ID, oldValue, this.openSetLonglineId);
firePropertyChange(PROPERTY_OPEN_SET_LONGLINE_ID, oldValue2, this.openSetLonglineId);
firePropertyChange(PROPERTY_OPEN_SET, oldValue3, isOpenSet());
firePropertyChange(PROPERTY_OPEN_SET_LONGLINE, oldValue4, isOpenSetLongline());
}
public boolean isEnabled() {
return enabled;
}
public boolean getEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
boolean oldValue = getEnabled();
this.enabled = enabled;
firePropertyChange(PROPERTY_ENABLED, oldValue, enabled);
}
private boolean isSelectionChanged() {
return selectionChanged;
}
private void setSelectionChanged() {
boolean oldValue = isSelectionChanged();
this.selectionChanged = true;
firePropertyChange(PROPERTY_SELECTION_CHANGED, oldValue, true);
}
private ImmutableList getTypes() {
return types;
}
private ImmutableList getReverseTypes() {
return reverseTypes;
}
public String getHigherOpenId() {
for (DtoUIContext type : getReverseTypes()) {
String result = type.getOpenId();
if (result != null) {
return result;
}
}
return null;
}
public String getHigherSelectedId() {
for (DtoUIContext type : getReverseTypes()) {
String result = type.getSelectedId();
if (result != null) {
return result;
}
}
return null;
}
public String[] getSelectedIds() {
List ids = new ArrayList<>();
for (DtoUIContext type : getTypes()) {
String result = type.getSelectedId();
if (result != null) {
ids.add(result);
}
}
return ids.toArray(new String[ids.size()]);
}
public String[] getOpenIds() {
List ids = new ArrayList<>();
for (DtoUIContext type : getTypes()) {
String result = type.getOpenId();
if (result != null) {
ids.add(result);
}
}
return ids.toArray(new String[ids.size()]);
}
public boolean isOpenProgram() {
return openProgramId != null;
}
public boolean isOpenTrip() {
return isOpenTripLongline() || isOpenTripSeine();
}
public boolean isOpenTripLongline() {
return openTripLonglineId != null;
}
public boolean isOpenTripSeine() {
return openTripSeineId != null;
}
public boolean isOpenRoute() {
return openRouteId != null;
}
public boolean isOpenActivity() {
return isOpenActivityLongline() || isOpenActivitySeine();
}
public boolean isOpenActivityLongline() {
return openActivityLonglineId != null;
}
public boolean isOpenActivitySeine() {
return openActivitySeineId != null;
}
public boolean isOpenSet() {
return isOpenSetLongline() || isOpenSetSeine();
}
private boolean isOpenSetLongline() {
return openSetLonglineId != null;
}
public boolean isOpenSetSeine() {
return openSetSeineId != null;
}
// public boolean isSelectedOpen(Class> type) {
//
// for (DtoUIContext> entityType : types) {
// if (entityType.acceptType(type)) {
//
// String openId = entityType.getOpenId(this);
// String selectedId = entityType.getSelectedId(this);
// return selectedId != null && openId != null && selectedId.equals(openId);
//
// }
// }
//
// return false;
// }
public void populateSelectedIds(String... selectedId) {
if (!getEnabled()) {
throw new IllegalStateException(this + " is not enabled");
}
List oldSelection = Arrays.asList(getSelectedIds());
if (log.isDebugEnabled()) {
log.debug("old selection = " + oldSelection);
}
resetSelect();
List realSelection = null;
if (selectedId != null && selectedId.length > 0) {
realSelection = new ArrayList<>();
for (String s : selectedId) {
if (s == null) {
// ce cas peut arriver lors de la creation d'un nouvel objet
continue;
}
DtoUIContext type = acceptId(s);
if (type == null) {
if (log.isWarnEnabled()) {
log.warn("Could not find type for id : " + s);
}
continue;
}
type.setSelectedId(s);
realSelection.add(s);
if (log.isDebugEnabled()) {
log.debug("add selectedId : " + s);
}
}
}
if (oldSelection.equals(realSelection)) {
// rien a change
if (log.isDebugEnabled()) {
log.debug("selection does not changed");
}
return;
}
if (log.isDebugEnabled()) {
log.debug("new selection = " + realSelection);
}
setSelectionChanged();
}
public void populateOpens(String... openIds) {
if (!getEnabled()) {
throw new IllegalStateException(this + " is not enabled");
}
List oldSelection = Arrays.asList(getOpenIds());
List realSelection = new ArrayList<>();
if (openIds != null && openIds.length > 0) {
for (String s : openIds) {
if (s == null) {
// ce cas peut arriver lors de la creation d'un nouvel objet
continue;
}
DtoUIContext type = acceptId(s);
if (type == null) {
if (log.isWarnEnabled()) {
log.warn("Could not find type for id : " + s);
}
continue;
}
if (!(type instanceof OpenableDtoUIContext)) {
// pas de traitement open
continue;
}
((OpenableDtoUIContext) type).setOpenId(this, s);
realSelection.add(s);
if (log.isDebugEnabled()) {
log.debug("add openId : " + s);
}
}
}
// if only the program is open, remove it
if (realSelection.size() == 1) {
setOpenProgramId(null);
realSelection.clear();
}
if (oldSelection.equals(realSelection)) {
// rien a change
if (log.isDebugEnabled()) {
log.debug("openIds selection does not changed");
}
return;
}
if (log.isInfoEnabled()) {
log.info("old openIds selection = " + oldSelection);
log.info("new openIds selection = " + realSelection);
}
for (String s : oldSelection) {
if (realSelection.contains(s)) {
// pas bouge rien a faire
continue;
}
DtoUIContext type = acceptId(s);
if (type == null) {
if (log.isWarnEnabled()) {
log.warn("Could not find type for id : " + s);
}
continue;
}
if (!(type instanceof OpenableDtoUIContext)) {
// pas de traitement open
continue;
}
String newId = type.getOpenId();
if (newId != null) {
if (log.isInfoEnabled()) {
log.info("will remove old obsolete open id : " + s);
}
// plus d'id pour ce type, on le supprime
((OpenableDtoUIContext) type).setOpenId(this, null);
}
}
}
@Override
public void reset() {
resetOpen();
resetSelect();
}
private void resetSelect() {
setSelectedProgramId(null);
setSelectedTripId(null);
setSelectedRouteId(null);
setSelectedActivityId(null);
setSelectedFloatingObjectId(null);
setSelectedSetId(null);
selectionChanged = false;
}
public void resetOpen() {
setOpenProgramId(null);
setOpenTripSeineId(null);
setOpenTripLonglineId(null);
setOpenRouteId(null);
setOpenActivitySeineId(null);
setOpenActivityLonglineId(null);
}
public boolean isSelectedRoute() {
return getSelectedRouteId() != null;
}
public boolean isSelectedSetSeine() {
return getSelectedSetSeineId() != null;
}
public boolean isSelectedSetLongline() {
return getSelectedSetLonglineId() != null;
}
public boolean isSelectedFloatingObject() {
return getSelectedFloatingObjectId() != null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy