Updated UI to use new database structure

* use new methods to store polling Configuration
* refactored DurationConfigField, added Builder

Signed-off-by: Nonnenmacher Fabian <fabian.nonnenmacher@bosch-si.com>
This commit is contained in:
Fabian Nonnenmacher
2016-01-26 16:24:07 +01:00
committed by Nonnenmacher Fabian
parent 07fce42469
commit 3f2c0d134a
3 changed files with 126 additions and 113 deletions

View File

@@ -15,7 +15,7 @@ import java.time.temporal.TemporalAccessor;
public class DurationHelper { public class DurationHelper {
/** /**
* Format of the String expected in configuration file and in the databse. * Format of the String expected in configuration file and in the database.
*/ */
public static final String DURATION_FORMAT = "HH:mm:ss"; public static final String DURATION_FORMAT = "HH:mm:ss";
@@ -27,7 +27,7 @@ public class DurationHelper {
* @return String in the duration format, specified at * @return String in the duration format, specified at
* {@link #DURATION_FORMAT} * {@link #DURATION_FORMAT}
*/ */
public String durationToFormattedString(Duration duration) { public String durationToFormattedString(final Duration duration) {
if (duration == null) { if (duration == null) {
return null; return null;
} }
@@ -44,7 +44,7 @@ public class DurationHelper {
* @throws DateTimeParseException * @throws DateTimeParseException
* when String is in wrong format * when String is in wrong format
*/ */
public Duration formattedStringToDuration(String formattedDuration) throws DateTimeParseException { public Duration formattedStringToDuration(final String formattedDuration) throws DateTimeParseException {
if (formattedDuration == null) { if (formattedDuration == null) {
return null; return null;
} }
@@ -64,7 +64,7 @@ public class DurationHelper {
* count of seconds * count of seconds
* @return duration * @return duration
*/ */
public Duration getDurationByTimeValues(long hours, long minutes, long seconds) { public Duration getDurationByTimeValues(final long hours, final long minutes, final long seconds) {
return Duration.ofHours(hours).plusMinutes(minutes).plusSeconds(seconds); return Duration.ofHours(hours).plusMinutes(minutes).plusSeconds(seconds);
} }
} }

View File

@@ -4,9 +4,12 @@ import java.time.Duration;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.eclipse.hawkbit.ControllerPollProperties;
import org.eclipse.hawkbit.repository.SystemManagement; import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.repository.model.TenantMetaData; import org.eclipse.hawkbit.repository.model.TenantConfiguration;
import org.eclipse.hawkbit.repository.model.helper.PollConfigurationHelper; import org.eclipse.hawkbit.repository.model.TenantConfigurationValue;
import org.eclipse.hawkbit.repository.model.helper.DurationHelper;
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
import org.eclipse.hawkbit.ui.tenantconfiguration.polling.DurationConfigField; import org.eclipse.hawkbit.ui.tenantconfiguration.polling.DurationConfigField;
import org.eclipse.hawkbit.ui.utils.I18N; import org.eclipse.hawkbit.ui.utils.I18N;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -34,19 +37,23 @@ public class PollingConfigurationView extends BaseConfigurationView
private I18N i18n; private I18N i18n;
@Autowired @Autowired
PollConfigurationHelper pollConfigurationHelper; private ControllerPollProperties controllerPollProperties;
@Autowired @Autowired
private transient SystemManagement systemManagement; private transient SystemManagement systemManagement;
@Autowired private DurationConfigField fieldPollTime = null;
private DurationConfigField fieldPollingTime; private DurationConfigField fieldPollingOverdueTime = null;
@Autowired private Duration minDuration;
private DurationConfigField fieldPollingOverdueTime; private Duration maxDuration;
private Duration globalPollTime;
private Duration globalOverdueTime;
private Duration tenantPollingTime; private Duration tenantPollTime = null;
private Duration tenantPollingOverdueTime; private Duration tenantOverdueTime = null;
private final DurationHelper durationHelper = new DurationHelper();
/** /**
* Initialize Authentication Configuration layout. * Initialize Authentication Configuration layout.
@@ -54,37 +61,44 @@ public class PollingConfigurationView extends BaseConfigurationView
@PostConstruct @PostConstruct
public void init() { public void init() {
minDuration = durationHelper.formattedStringToDuration(controllerPollProperties.getMinPollingTime());
maxDuration = durationHelper.formattedStringToDuration(controllerPollProperties.getMaxPollingTime());
globalPollTime = durationHelper.formattedStringToDuration(controllerPollProperties.getPollingTime());
globalOverdueTime = durationHelper.formattedStringToDuration(controllerPollProperties.getPollingOverdueTime());
final TenantConfigurationValue<String> pollTimeConfValue = systemManagement
.getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class);
if (!pollTimeConfValue.isGlobal()) {
tenantPollTime = durationHelper.formattedStringToDuration(pollTimeConfValue.getValue());
}
final TenantConfigurationValue<String> overdueTimeConfValue = systemManagement
.getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class);
if (!overdueTimeConfValue.isGlobal()) {
tenantOverdueTime = durationHelper.formattedStringToDuration(overdueTimeConfValue.getValue());
}
final Panel rootPanel = new Panel(); final Panel rootPanel = new Panel();
rootPanel.setSizeFull(); rootPanel.setSizeFull();
rootPanel.addStyleName("config-panel"); rootPanel.addStyleName("config-panel");
final VerticalLayout vLayout = new VerticalLayout(); final VerticalLayout vLayout = new VerticalLayout();
vLayout.setMargin(true); vLayout.setMargin(true);
// vLayout.setSizeFull();
final Label headerDisSetType = new Label(i18n.get("configuration.polling.title")); final Label headerDisSetType = new Label(i18n.get("configuration.polling.title"));
headerDisSetType.addStyleName("config-panel-header"); headerDisSetType.addStyleName("config-panel-header");
vLayout.addComponent(headerDisSetType); vLayout.addComponent(headerDisSetType);
final TenantMetaData tenantMetaData = systemManagement.getTenantMetadata(); fieldPollTime = DurationConfigField.builder().caption(i18n.get("configuration.polling.time"))
.checkBoxLabel(i18n.get("configuration.polling.custom.value")).range(minDuration, maxDuration)
.globalDuration(globalPollTime).tenantDuration(tenantPollTime).build();
fieldPollTime.addChangeListener(this);
vLayout.addComponent(fieldPollTime);
tenantPollingTime = tenantMetaData.getPollingTime(); fieldPollingOverdueTime = DurationConfigField.builder().caption(i18n.get("configuration.polling.overduetime"))
fieldPollingTime.setInitValues(i18n.get("configuration.polling.time"), tenantPollingTime, .checkBoxLabel(i18n.get("configuration.polling.custom.value")).range(minDuration, maxDuration)
pollConfigurationHelper.getGlobalPollTimeInterval()); .globalDuration(globalPollTime).tenantDuration(tenantOverdueTime).build();
fieldPollingTime.setAllowedRange(pollConfigurationHelper.getMinimumPollingInterval(),
pollConfigurationHelper.getMaximumPollingInterval());
fieldPollingTime.addChangeListener(this);
vLayout.addComponent(fieldPollingTime);
tenantPollingOverdueTime = tenantMetaData.getPollingOverdueTime();
fieldPollingOverdueTime.setInitValues(i18n.get("configuration.polling.overduetime"), tenantPollingOverdueTime,
pollConfigurationHelper.getGlobalOverduePollTimeInterval());
fieldPollingOverdueTime.setAllowedRange(pollConfigurationHelper.getMinimumPollingInterval(),
pollConfigurationHelper.getMaximumPollingInterval());
fieldPollingOverdueTime.addChangeListener(this); fieldPollingOverdueTime.addChangeListener(this);
vLayout.addComponent(fieldPollingOverdueTime); vLayout.addComponent(fieldPollingOverdueTime);
rootPanel.setContent(vLayout); rootPanel.setContent(vLayout);
@@ -95,36 +109,30 @@ public class PollingConfigurationView extends BaseConfigurationView
public void save() { public void save() {
// make sure values are only saved, when the value has been changed // make sure values are only saved, when the value has been changed
boolean hasChanged = false; if (!compareDurations(tenantPollTime, fieldPollTime.getValue())) {
tenantPollTime = fieldPollTime.getValue();
final TenantMetaData tenantMetaData = systemManagement.getTenantMetadata(); systemManagement.addOrUpdateConfiguration(
new TenantConfiguration(TenantConfigurationKey.POLLING_TIME_INTERVAL.getKeyName(),
if (!compareDurations(tenantPollingTime, fieldPollingTime.getValue())) { durationHelper.durationToFormattedString(tenantPollTime)));
tenantPollingTime = fieldPollingTime.getValue();
tenantMetaData.setPollingTime(fieldPollingTime.getValue());
hasChanged = true;
} }
if (!compareDurations(tenantPollingOverdueTime, fieldPollingOverdueTime.getValue())) { if (!compareDurations(tenantOverdueTime, fieldPollingOverdueTime.getValue())) {
tenantPollingOverdueTime = fieldPollingOverdueTime.getValue(); tenantOverdueTime = fieldPollingOverdueTime.getValue();
tenantMetaData.setPollingOverdueTime(fieldPollingOverdueTime.getValue()); systemManagement.addOrUpdateConfiguration(
hasChanged = true; new TenantConfiguration(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL.getKeyName(),
} durationHelper.durationToFormattedString(tenantOverdueTime)));
if (hasChanged) {
systemManagement.updateTenantMetadata(tenantMetaData);
} }
} }
@Override @Override
public void undo() { public void undo() {
fieldPollingTime.setValue(tenantPollingTime); fieldPollTime.setValue(tenantPollTime);
fieldPollingOverdueTime.setValue(tenantPollingOverdueTime); fieldPollingOverdueTime.setValue(tenantOverdueTime);
} }
@Override @Override
public boolean isUserInputValid() { public boolean isUserInputValid() {
return fieldPollingTime.isUserInputValid() && fieldPollingOverdueTime.isUserInputValid(); return fieldPollTime.isUserInputValid() && fieldPollingOverdueTime.isUserInputValid();
} }
@Override @Override
@@ -132,7 +140,7 @@ public class PollingConfigurationView extends BaseConfigurationView
notifyConfigurationChanged(); notifyConfigurationChanged();
} }
private boolean compareDurations(Duration d1, Duration d2) { private boolean compareDurations(final Duration d1, final Duration d2) {
if (d1 == null && d2 == null) { if (d1 == null && d2 == null) {
return true; return true;
} }

View File

@@ -4,20 +4,12 @@ import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.annotation.PostConstruct;
import javax.validation.constraints.NotNull;
import org.eclipse.hawkbit.ui.components.SPUIComponentProvider; import org.eclipse.hawkbit.ui.components.SPUIComponentProvider;
import org.eclipse.hawkbit.ui.tenantconfiguration.ConfigurationItem; import org.eclipse.hawkbit.ui.tenantconfiguration.ConfigurationItem;
import org.eclipse.hawkbit.ui.utils.I18N;
import org.eclipse.hawkbit.ui.utils.SPUILabelDefinitions; import org.eclipse.hawkbit.ui.utils.SPUILabelDefinitions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener; import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.ViewScope;
import com.vaadin.ui.Alignment; import com.vaadin.ui.Alignment;
import com.vaadin.ui.CheckBox; import com.vaadin.ui.CheckBox;
import com.vaadin.ui.GridLayout; import com.vaadin.ui.GridLayout;
@@ -29,68 +21,41 @@ import com.vaadin.ui.Label;
* duration in the DurationField or he can configure using the global duration * duration in the DurationField or he can configure using the global duration
* by changing the CheckBox. * by changing the CheckBox.
*/ */
@SpringComponent
@ViewScope
@Scope("prototype")
public class DurationConfigField extends GridLayout implements ValueChangeListener, ConfigurationItem { public class DurationConfigField extends GridLayout implements ValueChangeListener, ConfigurationItem {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final List<ConfigurationItemChangeListener> configurationChangeListeners = new ArrayList<>(); private final List<ConfigurationItemChangeListener> configurationChangeListeners = new ArrayList<>();
private CheckBox checkBox; private final CheckBox checkBox = new CheckBox();
private DurationField durationField; private final DurationField durationField = new DurationField();
private Duration globalDuration; private Duration globalDuration;
private final Label labelCustomValue;
@Autowired private DurationConfigField() {
private I18N i18n;
/**
* sets i18n
*
* @param i18n
*/
public void setI18n(I18N i18n) {
this.i18n = i18n;
}
public DurationConfigField() {
super(3, 2); super(3, 2);
}
/**
* Initialize Authentication Configuration layout.
*/
@PostConstruct
public void init() {
this.addStyleName("duration-config-field"); this.addStyleName("duration-config-field");
this.setSpacing(true); this.setSpacing(true);
this.setImmediate(true); this.setImmediate(true);
this.setColumnExpandRatio(1, 1.0F); this.setColumnExpandRatio(1, 1.0F);
// gridLayout.setSizeFull();
checkBox = new CheckBox();
this.addComponent(checkBox, 0, 0); this.addComponent(checkBox, 0, 0);
this.setComponentAlignment(checkBox, Alignment.MIDDLE_LEFT); this.setComponentAlignment(checkBox, Alignment.MIDDLE_LEFT);
Label customValue = SPUIComponentProvider.getLabel(i18n.get("configuration.polling.custom.value"), labelCustomValue = SPUIComponentProvider.getLabel("", SPUILabelDefinitions.SP_LABEL_SIMPLE);
SPUILabelDefinitions.SP_LABEL_SIMPLE); this.addComponent(labelCustomValue, 1, 0);
this.addComponent(customValue, 1, 0); this.setComponentAlignment(labelCustomValue, Alignment.MIDDLE_LEFT);
this.setComponentAlignment(customValue, Alignment.MIDDLE_LEFT);
durationField = new DurationField();
this.addComponent(durationField, 2, 0); this.addComponent(durationField, 2, 0);
this.setComponentAlignment(durationField, Alignment.MIDDLE_LEFT); this.setComponentAlignment(durationField, Alignment.MIDDLE_LEFT);
checkBox.addValueChangeListener(this); checkBox.addValueChangeListener(this);
durationField.addValueChangeListener(this);
} }
@Override @Override
public void valueChange(ValueChangeEvent event) { public void valueChange(final ValueChangeEvent event) {
if (event.getProperty() == checkBox) { if (event.getProperty() == checkBox) {
if (checkBox.getValue()) { if (checkBox.getValue()) {
durationField.setEnabled(true); durationField.setEnabled(true);
@@ -103,35 +68,25 @@ public class DurationConfigField extends GridLayout implements ValueChangeListen
} }
/** /**
* sets all mandatitory attributes for correct user interaction * has to be called before using, see Builder Implementation.
*
* @param caption
* the caption of the field
* *
* @param tenantDuration * @param tenantDuration
* tenant specific duration value * tenant specific duration value
* @param globalDuration * @param globalDuration
* duration value which is stored in the global configuration * duration value which is stored in the global configuration
*/ */
public void setInitValues(String caption, @NotNull Duration tenantDuration, @NotNull Duration globalDuration) { private void init(final Duration globalDuration, final Duration tenantDuration) {
this.setCaption(caption);
this.globalDuration = globalDuration; this.globalDuration = globalDuration;
this.setValue(tenantDuration); this.setValue(tenantDuration);
} }
/** private void setCheckboxLabel(final String label) {
* sets the allowed range of the duration values labelCustomValue.setValue(label);
* }
* @param minimumDuration
* minimum allowed duration value private void setAllowedRange(final Duration minimumDuration, final Duration maximumDuration) {
* @param maximumDuration
* maximum allowed duration value
*/
public void setAllowedRange(Duration minimumDuration, Duration maximumDuration) {
durationField.setMinimumDuration(minimumDuration); durationField.setMinimumDuration(minimumDuration);
durationField.setMaximumDuration(maximumDuration); durationField.setMaximumDuration(maximumDuration);
} }
/** /**
@@ -141,7 +96,7 @@ public class DurationConfigField extends GridLayout implements ValueChangeListen
* duration which will be set in to the duration field, when * duration which will be set in to the duration field, when
* {@code null} the global configuration will be used. * {@code null} the global configuration will be used.
*/ */
public void setValue(Duration tenantDuration) { public void setValue(final Duration tenantDuration) {
if (tenantDuration == null) { if (tenantDuration == null) {
// no tenant specific configuration // no tenant specific configuration
checkBox.setValue(false); checkBox.setValue(false);
@@ -178,4 +133,54 @@ public class DurationConfigField extends GridLayout implements ValueChangeListen
public void addChangeListener(final ConfigurationItemChangeListener listener) { public void addChangeListener(final ConfigurationItemChangeListener listener) {
configurationChangeListeners.add(listener); configurationChangeListeners.add(listener);
} }
public static DurationConfigFieldBuilder builder() {
return new DurationConfigFieldBuilder();
}
public static class DurationConfigFieldBuilder {
private final DurationConfigField field;
private Duration globalDuration = null;
private Duration tenantDuration = null;
private DurationConfigFieldBuilder() {
field = new DurationConfigField();
};
public DurationConfigFieldBuilder checkBoxLabel(final String label) {
field.setCheckboxLabel(label);
return this;
}
public DurationConfigFieldBuilder globalDuration(final Duration globalDuration) {
this.globalDuration = globalDuration;
return this;
}
public DurationConfigFieldBuilder caption(final String caption) {
field.setCaption(caption);
return this;
}
public DurationConfigFieldBuilder range(final Duration minDuration, final Duration maxDuration) {
field.setAllowedRange(minDuration, maxDuration);
return this;
}
public DurationConfigFieldBuilder tenantDuration(final Duration tenantDuration) {
this.tenantDuration = tenantDuration;
return this;
}
public DurationConfigField build() {
if (globalDuration == null) {
throw new IllegalStateException(
"Cannot build DurationConfigField without a value for global duration.");
}
field.init(globalDuration, tenantDuration);
return field;
}
};
} }