Refactored the code to fix the sonar issues.

Signed-off-by: Gaurav <gaurav.sahay@in.bosch.com>
This commit is contained in:
Gaurav
2016-08-17 17:36:45 +02:00
parent 75905472c8
commit fafaf54e37
13 changed files with 238 additions and 241 deletions

View File

@@ -26,6 +26,7 @@ import org.eclipse.hawkbit.ui.utils.I18N;
import org.eclipse.hawkbit.ui.utils.SPUIComponentIdProvider; import org.eclipse.hawkbit.ui.utils.SPUIComponentIdProvider;
import org.eclipse.hawkbit.ui.utils.SPUIDefinitions; import org.eclipse.hawkbit.ui.utils.SPUIDefinitions;
import org.eclipse.hawkbit.ui.utils.SPUILabelDefinitions; import org.eclipse.hawkbit.ui.utils.SPUILabelDefinitions;
import org.eclipse.hawkbit.ui.utils.SpringContextHelper;
import org.eclipse.hawkbit.ui.utils.UINotification; import org.eclipse.hawkbit.ui.utils.UINotification;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.addons.lazyquerycontainer.BeanQueryFactory; import org.vaadin.addons.lazyquerycontainer.BeanQueryFactory;
@@ -184,6 +185,7 @@ public class SoftwareModuleAddUpdateWindow extends CustomComponent implements Se
window = SPUIWindowDecorator.getWindow(i18n.get("upload.caption.add.new.swmodule"), null, window = SPUIWindowDecorator.getWindow(i18n.get("upload.caption.add.new.swmodule"), null,
SPUIDefinitions.CREATE_UPDATE_WINDOW, this, event -> saveOrUpdate(), null, null, formLayout, i18n); SPUIDefinitions.CREATE_UPDATE_WINDOW, this, event -> saveOrUpdate(), null, null, formLayout, i18n);
window.getButtonsLayout().removeStyleName("actionButtonsMargin"); window.getButtonsLayout().removeStyleName("actionButtonsMargin");
window.setCloseListener(() -> !isDuplicate());
nameTextField.setEnabled(!editSwModule); nameTextField.setEnabled(!editSwModule);
versionTextField.setEnabled(!editSwModule); versionTextField.setEnabled(!editSwModule);
@@ -199,21 +201,30 @@ public class SoftwareModuleAddUpdateWindow extends CustomComponent implements Se
final String description = HawkbitCommonUtil.trimAndNullIfEmpty(descTextArea.getValue()); final String description = HawkbitCommonUtil.trimAndNullIfEmpty(descTextArea.getValue());
final String type = typeComboBox.getValue() != null ? typeComboBox.getValue().toString() : null; final String type = typeComboBox.getValue() != null ? typeComboBox.getValue().toString() : null;
if (HawkbitCommonUtil.isDuplicate(name, version, type)) { if (isDuplicate()) {
uiNotifcation.displayValidationError( uiNotifcation.displayValidationError(
i18n.get("message.duplicate.softwaremodule", new Object[] { name, version })); i18n.get("message.duplicate.softwaremodule", new Object[] { name, version }));
window.setIsDuplicate(Boolean.TRUE); return;
} else {
final SoftwareModule newBaseSoftwareModule = HawkbitCommonUtil.addNewBaseSoftware(entityFactory, name,
version, vendor, softwareManagement.findSoftwareModuleTypeByName(type), description);
window.setIsDuplicate(Boolean.FALSE);
if (newBaseSoftwareModule != null) {
/* display success message */
uiNotifcation.displaySuccess(i18n.get("message.save.success",
new Object[] { newBaseSoftwareModule.getName() + ":" + newBaseSoftwareModule.getVersion() }));
eventBus.publish(this, new SoftwareModuleEvent(BaseEntityEventType.NEW_ENTITY, newBaseSoftwareModule));
}
} }
final SoftwareModule newBaseSoftwareModule = HawkbitCommonUtil.addNewBaseSoftware(entityFactory, name, version,
vendor, softwareManagement.findSoftwareModuleTypeByName(type), description);
if (newBaseSoftwareModule != null) {
/* display success message */
uiNotifcation.displaySuccess(i18n.get("message.save.success",
new Object[] { newBaseSoftwareModule.getName() + ":" + newBaseSoftwareModule.getVersion() }));
eventBus.publish(this, new SoftwareModuleEvent(BaseEntityEventType.NEW_ENTITY, newBaseSoftwareModule));
}
}
private boolean isDuplicate() {
final String name = nameTextField.getValue();
final String version = versionTextField.getValue();
final String type = typeComboBox.getValue() != null ? typeComboBox.getValue().toString() : null;
final SoftwareManagement swMgmtService = SpringContextHelper.getBean(SoftwareManagement.class);
final SoftwareModule swModule = swMgmtService.findSoftwareModuleByNameAndVersion(name, version,
swMgmtService.findSoftwareModuleTypeByName(type));
return swModule != null;
} }
/** /**

View File

@@ -44,7 +44,7 @@ import com.vaadin.ui.themes.ValoTheme;
*/ */
@SpringComponent @SpringComponent
@ViewScope @ViewScope
public class CreateUpdateSoftwareTypeLayout extends CreateUpdateTypeLayout { public class CreateUpdateSoftwareTypeLayout extends CreateUpdateTypeLayout<SoftwareModuleType> {
private static final long serialVersionUID = -5169398523815919367L; private static final long serialVersionUID = -5169398523815919367L;
private static final Logger LOG = LoggerFactory.getLogger(CreateUpdateSoftwareTypeLayout.class); private static final Logger LOG = LoggerFactory.getLogger(CreateUpdateSoftwareTypeLayout.class);
@@ -191,19 +191,30 @@ public class CreateUpdateSoftwareTypeLayout extends CreateUpdateTypeLayout {
@Override @Override
protected void save(final ClickEvent event) { protected void save(final ClickEvent event) {
final SoftwareModuleType existingSMTypeByKey = swTypeManagementService
.findSoftwareModuleTypeByKey(typeKey.getValue());
final SoftwareModuleType existingSMTypeByName = swTypeManagementService
.findSoftwareModuleTypeByName(tagName.getValue());
if (optiongroup.getValue().equals(createTypeStr)) { if (optiongroup.getValue().equals(createTypeStr)) {
if (!checkIsDuplicateByKey(existingSMTypeByKey) && !checkIsDuplicate(existingSMTypeByName)) { if (!isDuplicate()) {
createNewSWModuleType(); createNewSWModuleType();
} }
} else { } else {
updateSWModuleType(existingSMTypeByName); updateSWModuleType(findEntityByName());
} }
} }
@Override
protected SoftwareModuleType findEntityByKey() {
return swTypeManagementService.findSoftwareModuleTypeByKey(typeKey.getValue());
}
@Override
protected SoftwareModuleType findEntityByName() {
return swTypeManagementService.findSoftwareModuleTypeByName(tagName.getValue());
}
@Override
protected String getDuplicateKeyErrorMessage(final SoftwareModuleType existingType) {
return i18n.get("message.type.key.swmodule.duplicate.check", new Object[] { existingType.getKey() });
}
private void createNewSWModuleType() { private void createNewSWModuleType() {
int assignNumber = 0; int assignNumber = 0;
final String colorPicked = ColorPickerHelper.getColorPickedString(getColorPickerLayout().getSelPreview()); final String colorPicked = ColorPickerHelper.getColorPickedString(getColorPickerLayout().getSelPreview());

View File

@@ -10,7 +10,6 @@ package org.eclipse.hawkbit.ui.common;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@@ -51,6 +50,7 @@ import com.vaadin.ui.AbstractLayout;
import com.vaadin.ui.AbstractOrderedLayout; import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.Alignment; import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button; import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox; import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component; import com.vaadin.ui.Component;
@@ -70,7 +70,7 @@ import com.vaadin.ui.themes.ValoTheme;
* corner and a save and cancel button at the bottom. Is not intended to reuse. * corner and a save and cancel button at the bottom. Is not intended to reuse.
* *
*/ */
public class CommonDialogWindow extends Window implements Serializable { public class CommonDialogWindow extends Window {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -94,15 +94,15 @@ public class CommonDialogWindow extends Window implements Serializable {
private final ClickListener cancelButtonClickListener; private final ClickListener cancelButtonClickListener;
private final ClickListener closeClickListener = event -> close(); private final ClickListener closeClickListener = event -> onCloseEvent(event);
private final transient Map<Component, Object> orginalValues; private final transient Map<Component, Object> orginalValues;
private final List<AbstractField<?>> allComponents; private final List<AbstractField<?>> allComponents;
private final I18N i18n; private final I18N i18n;
private boolean isDuplicate; private transient CommonDialogWindowCloseListener closeListener;
/** /**
* Constructor. * Constructor.
@@ -137,17 +137,29 @@ public class CommonDialogWindow extends Window implements Serializable {
init(); init();
} }
public void setCloseListener(final CommonDialogWindowCloseListener closeListener) {
this.closeListener = closeListener;
}
private void onCloseEvent(final ClickEvent clickEvent) {
if (!clickEvent.getButton().equals(saveButton)) {
close();
return;
}
if (closeListener != null && !closeListener.canWindowClose()) {
return;
}
close();
}
@Override @Override
public void close() { public void close() {
if(!isDuplicate){ super.close();
super.close(); orginalValues.clear();
orginalValues.clear(); removeListeners();
removeListeners(); allComponents.clear();
allComponents.clear(); this.saveButton.setEnabled(false);
this.saveButton.setEnabled(false);
}else{
isDuplicate = Boolean.FALSE;
}
} }
private void removeListeners() { private void removeListeners() {
@@ -217,7 +229,7 @@ public class CommonDialogWindow extends Window implements Serializable {
setModal(true); setModal(true);
addStyleName("fontsize"); addStyleName("fontsize");
setOrginaleValues(); setOrginaleValues();
addListeners(); addComponenetListeners();
} }
/** /**
@@ -236,12 +248,6 @@ public class CommonDialogWindow extends Window implements Serializable {
saveButton.setEnabled(isSaveButtonEnabledAfterValueChange(null, null)); saveButton.setEnabled(isSaveButtonEnabledAfterValueChange(null, null));
} }
protected void addListeners() {
addComponenetListeners();
addCloseListenerForSaveButton();
addCloseListenerForCancelButton();
}
protected void addCloseListenerForSaveButton() { protected void addCloseListenerForSaveButton() {
saveButton.addClickListener(closeClickListener); saveButton.addClickListener(closeClickListener);
} }
@@ -438,6 +444,7 @@ public class CommonDialogWindow extends Window implements Serializable {
FontAwesome.TIMES, SPUIButtonStyleNoBorderWithIcon.class); FontAwesome.TIMES, SPUIButtonStyleNoBorderWithIcon.class);
cancelButton.setSizeUndefined(); cancelButton.setSizeUndefined();
cancelButton.addStyleName("default-color"); cancelButton.addStyleName("default-color");
addCloseListenerForCancelButton();
if (cancelButtonClickListener != null) { if (cancelButtonClickListener != null) {
cancelButton.addClickListener(cancelButtonClickListener); cancelButton.addClickListener(cancelButtonClickListener);
} }
@@ -452,6 +459,7 @@ public class CommonDialogWindow extends Window implements Serializable {
FontAwesome.SAVE, SPUIButtonStyleNoBorderWithIcon.class); FontAwesome.SAVE, SPUIButtonStyleNoBorderWithIcon.class);
saveButton.setSizeUndefined(); saveButton.setSizeUndefined();
saveButton.addStyleName("default-color"); saveButton.addStyleName("default-color");
addCloseListenerForSaveButton();
saveButton.addClickListener(saveButtonClickListener); saveButton.addClickListener(saveButtonClickListener);
saveButton.setEnabled(false); saveButton.setEnabled(false);
buttonsLayout.addComponent(saveButton); buttonsLayout.addComponent(saveButton);
@@ -531,20 +539,16 @@ public class CommonDialogWindow extends Window implements Serializable {
} }
/** /**
* Boolean.TRUE/Boolean.FALSE based on the entity saved in the window already exists. * Called before the save happens.
* @return isDuplicate *
*/ */
public boolean getIsDuplicate() { @FunctionalInterface
return isDuplicate; public interface CommonDialogWindowCloseListener {
/**
* @return true/false based on the dialog window to be closed or not.
*/
boolean canWindowClose();
} }
/**
* Sets Boolean.TRUE/Boolean.FALSE based on the entity saved in the window already exists.
* @param isDuplicate
*/
public void setIsDuplicate(final boolean isDuplicate) {
this.isDuplicate = isDuplicate;
}
} }

View File

@@ -1,33 +0,0 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.ui.common;
import org.eclipse.hawkbit.ui.utils.I18N;
import com.vaadin.ui.AbstractLayout;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
public class CustomCommonDialogWindow extends CommonDialogWindow {
private static final long serialVersionUID = -4453608850403359992L;
public CustomCommonDialogWindow(final String caption, final Component content, final String helpLink,
final ClickListener saveButtonClickListener, final ClickListener cancelButtonClickListener,
final AbstractLayout layout, final I18N i18n) {
super(caption, content, helpLink, saveButtonClickListener, cancelButtonClickListener, layout, i18n);
}
@Override
protected void addListeners() {
addComponenetListeners();
addCloseListenerForCancelButton();
}
}

View File

@@ -9,7 +9,6 @@
package org.eclipse.hawkbit.ui.decorators; package org.eclipse.hawkbit.ui.decorators;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow; import org.eclipse.hawkbit.ui.common.CommonDialogWindow;
import org.eclipse.hawkbit.ui.common.CustomCommonDialogWindow;
import org.eclipse.hawkbit.ui.utils.I18N; import org.eclipse.hawkbit.ui.utils.I18N;
import org.eclipse.hawkbit.ui.utils.SPUIDefinitions; import org.eclipse.hawkbit.ui.utils.SPUIDefinitions;
import org.eclipse.hawkbit.ui.utils.SPUIStyleDefinitions; import org.eclipse.hawkbit.ui.utils.SPUIStyleDefinitions;
@@ -48,33 +47,31 @@ public final class SPUIWindowDecorator {
public static CommonDialogWindow getWindow(final String caption, final String id, final String type, public static CommonDialogWindow getWindow(final String caption, final String id, final String type,
final Component content, final ClickListener saveButtonClickListener, final Component content, final ClickListener saveButtonClickListener,
final ClickListener cancelButtonClickListener, final String helpLink, final AbstractLayout layout, final ClickListener cancelButtonClickListener, final String helpLink, final AbstractLayout layout,
final I18N i18n) { final I18N i18n) {
CommonDialogWindow window = null; final CommonDialogWindow window = new CommonDialogWindow(caption, content, helpLink, saveButtonClickListener,
if (SPUIDefinitions.CUSTOM_METADATA_WINDOW.equals(type)) { cancelButtonClickListener, layout, i18n);
window = new CustomCommonDialogWindow(caption, content, helpLink, saveButtonClickListener,
cancelButtonClickListener, layout, i18n);
window.setDraggable(true);
window.setClosable(true);
} else {
window = new CommonDialogWindow(caption, content, helpLink, saveButtonClickListener,
cancelButtonClickListener, layout, i18n);
if (null != id) {
window.setId(id);
}
if (SPUIDefinitions.CONFIRMATION_WINDOW.equals(type)) {
window.setDraggable(false);
window.setClosable(true);
window.addStyleName(SPUIStyleDefinitions.CONFIRMATION_WINDOW_CAPTION);
} else if (SPUIDefinitions.CREATE_UPDATE_WINDOW.equals(type)) { if (SPUIDefinitions.CUSTOM_METADATA_WINDOW.equals(type)) {
window.setDraggable(true); window.setCloseListener(() -> false);
window.setClosable(true); window.setDraggable(true);
} window.setClosable(true);
} } else {
return window; if (null != id) {
} window.setId(id);
}
if (SPUIDefinitions.CONFIRMATION_WINDOW.equals(type)) {
window.setDraggable(false);
window.setClosable(true);
window.addStyleName(SPUIStyleDefinitions.CONFIRMATION_WINDOW_CAPTION);
} else if (SPUIDefinitions.CREATE_UPDATE_WINDOW.equals(type)) {
window.setDraggable(true);
window.setClosable(true);
}
}
return window;
}
/** /**
* Decorates window based on type. * Decorates window based on type.
* *

View File

@@ -53,7 +53,7 @@ import com.vaadin.ui.themes.ValoTheme;
*/ */
@SpringComponent @SpringComponent
@ViewScope @ViewScope
public class CreateUpdateDistSetTypeLayout extends CreateUpdateTypeLayout { public class CreateUpdateDistSetTypeLayout extends CreateUpdateTypeLayout<DistributionSetType> {
private static final long serialVersionUID = -5169398523815877767L; private static final long serialVersionUID = -5169398523815877767L;
private static final String DIST_TYPE_NAME = "name"; private static final String DIST_TYPE_NAME = "name";
@@ -566,19 +566,30 @@ public class CreateUpdateDistSetTypeLayout extends CreateUpdateTypeLayout {
@Override @Override
protected void save(final ClickEvent event) { protected void save(final ClickEvent event) {
final DistributionSetType existingDistTypeByKey = distributionSetManagement
.findDistributionSetTypeByKey(typeKey.getValue());
final DistributionSetType existingDistTypeByName = distributionSetManagement
.findDistributionSetTypeByName(tagName.getValue());
if (optiongroup.getValue().equals(createTypeStr)) { if (optiongroup.getValue().equals(createTypeStr)) {
if (!checkIsDuplicateByKey(existingDistTypeByKey) && !checkIsDuplicate(existingDistTypeByName)) { if (!isDuplicate()) {
createNewDistributionSetType(); createNewDistributionSetType();
} }
} else { } else {
updateDistributionSetType(existingDistTypeByKey); updateDistributionSetType(findEntityByKey());
} }
} }
@Override
protected DistributionSetType findEntityByKey() {
return distributionSetManagement.findDistributionSetTypeByKey(typeKey.getValue());
}
@Override
protected DistributionSetType findEntityByName() {
return distributionSetManagement.findDistributionSetTypeByName(tagName.getValue());
}
@Override
protected String getDuplicateKeyErrorMessage(final DistributionSetType existingType) {
return i18n.get("message.type.key.duplicate.check", new Object[] { existingType.getKey() });
}
@Override @Override
protected String getWindowCaption() { protected String getWindowCaption() {
return i18n.get("caption.add.type"); return i18n.get("caption.add.type");

View File

@@ -13,6 +13,7 @@ import javax.annotation.PreDestroy;
import org.eclipse.hawkbit.repository.SpPermissionChecker; import org.eclipse.hawkbit.repository.SpPermissionChecker;
import org.eclipse.hawkbit.repository.TagManagement; import org.eclipse.hawkbit.repository.TagManagement;
import org.eclipse.hawkbit.repository.model.DistributionSetTag; import org.eclipse.hawkbit.repository.model.DistributionSetTag;
import org.eclipse.hawkbit.repository.model.NamedEntity;
import org.eclipse.hawkbit.repository.model.Tag; import org.eclipse.hawkbit.repository.model.Tag;
import org.eclipse.hawkbit.repository.model.TargetTag; import org.eclipse.hawkbit.repository.model.TargetTag;
import org.eclipse.hawkbit.ui.colorpicker.ColorPickerConstants; import org.eclipse.hawkbit.ui.colorpicker.ColorPickerConstants;
@@ -55,7 +56,7 @@ import com.vaadin.ui.themes.ValoTheme;
/** /**
* Abstract class for create/update target tag layout. * Abstract class for create/update target tag layout.
*/ */
public abstract class AbstractCreateUpdateTagLayout extends CustomComponent public abstract class AbstractCreateUpdateTagLayout<E extends NamedEntity> extends CustomComponent
implements ColorChangeListener, ColorSelector { implements ColorChangeListener, ColorSelector {
private static final long serialVersionUID = 4229177824620576456L; private static final long serialVersionUID = 4229177824620576456L;
private static final String TAG_NAME_DYNAMIC_STYLE = "new-tag-name"; private static final String TAG_NAME_DYNAMIC_STYLE = "new-tag-name";
@@ -464,6 +465,7 @@ public abstract class AbstractCreateUpdateTagLayout extends CustomComponent
reset(); reset();
window = SPUIWindowDecorator.getWindow(getWindowCaption(), null, SPUIDefinitions.CREATE_UPDATE_WINDOW, this, window = SPUIWindowDecorator.getWindow(getWindowCaption(), null, SPUIDefinitions.CREATE_UPDATE_WINDOW, this,
this::save, this::discard, null, mainLayout, i18n); this::save, this::discard, null, mainLayout, i18n);
window.setCloseListener(() -> !isDuplicate());
return window; return window;
} }
@@ -560,16 +562,23 @@ public abstract class AbstractCreateUpdateTagLayout extends CustomComponent
getPreviewButtonColor(previewColor); getPreviewButtonColor(previewColor);
} }
protected Boolean checkIsDuplicate(final Tag existingTag) { private boolean isDuplicateByName() {
if (existingTag != null) { final E existingType = findEntityByName();
displayValidationError(i18n.get("message.tag.duplicate.check", new Object[] { existingTag.getName() })); if (existingType != null) {
window.setIsDuplicate(Boolean.TRUE); uiNotification.displayValidationError(
return Boolean.TRUE; i18n.get("message.tag.duplicate.check", new Object[] { existingType.getName() }));
return true;
} }
window.setIsDuplicate(Boolean.FALSE);
return Boolean.FALSE; return false;
} }
protected boolean isDuplicate() {
return isDuplicateByName();
}
protected abstract E findEntityByName();
public String getColorPicked() { public String getColorPicked() {
return colorPicked; return colorPicked;
} }

View File

@@ -8,9 +8,7 @@
*/ */
package org.eclipse.hawkbit.ui.layouts; package org.eclipse.hawkbit.ui.layouts;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.NamedEntity; import org.eclipse.hawkbit.repository.model.NamedEntity;
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
import org.eclipse.hawkbit.ui.colorpicker.ColorPickerConstants; import org.eclipse.hawkbit.ui.colorpicker.ColorPickerConstants;
import org.eclipse.hawkbit.ui.colorpicker.ColorPickerHelper; import org.eclipse.hawkbit.ui.colorpicker.ColorPickerHelper;
import org.eclipse.hawkbit.ui.components.SPUIComponentProvider; import org.eclipse.hawkbit.ui.components.SPUIComponentProvider;
@@ -34,7 +32,7 @@ import com.vaadin.ui.themes.ValoTheme;
* Superclass defining common properties and methods for creating/updating * Superclass defining common properties and methods for creating/updating
* types. * types.
*/ */
public abstract class CreateUpdateTypeLayout extends AbstractCreateUpdateTagLayout { public abstract class CreateUpdateTypeLayout<E extends NamedEntity> extends AbstractCreateUpdateTagLayout {
private static final long serialVersionUID = 5732904956185988397L; private static final long serialVersionUID = 5732904956185988397L;
@@ -255,37 +253,27 @@ public abstract class CreateUpdateTypeLayout extends AbstractCreateUpdateTagLayo
createDynamicStyleForComponents(tagName, typeKey, tagDesc, event.getColor().getCSS()); createDynamicStyleForComponents(tagName, typeKey, tagDesc, event.getColor().getCSS());
} }
protected Boolean checkIsDuplicate(final NamedEntity existingType) { private boolean isDuplicateByKey() {
final E existingType = findEntityByKey();
if (existingType != null) { if (existingType != null) {
uiNotification.displayValidationError( uiNotification.displayValidationError(getDuplicateKeyErrorMessage(existingType));
i18n.get("message.tag.duplicate.check", new Object[] { existingType.getName() })); return true;
window.setIsDuplicate(Boolean.TRUE);
return Boolean.TRUE;
} }
window.setIsDuplicate(Boolean.FALSE);
return Boolean.FALSE; return false;
} }
protected Boolean checkIsDuplicateByKey(final NamedEntity existingType) { @Override
protected boolean isDuplicate() {
if (existingType != null) { return isDuplicateByKey() || super.isDuplicate();
if (existingType instanceof DistributionSetType) {
uiNotification.displayValidationError(i18n.get("message.type.key.duplicate.check",
new Object[] { ((DistributionSetType) existingType).getKey() }));
window.setIsDuplicate(Boolean.TRUE);
return Boolean.TRUE;
} else if (existingType instanceof SoftwareModuleType) {
uiNotification.displayValidationError(i18n.get("message.type.key.swmodule.duplicate.check",
new Object[] { ((SoftwareModuleType) existingType).getKey() }));
window.setIsDuplicate(Boolean.TRUE);
return Boolean.TRUE;
}
}
window.setIsDuplicate(Boolean.FALSE);
return Boolean.FALSE;
} }
protected abstract E findEntityByKey();
protected abstract String getDuplicateKeyErrorMessage(E existingType);
@Override @Override
protected void save(final ClickEvent event) { protected void save(final ClickEvent event) {
// is implemented in the inherited class // is implemented in the inherited class

View File

@@ -186,29 +186,30 @@ public class DistributionAddUpdateWindowLayout extends CustomComponent {
* Update Distribution. * Update Distribution.
*/ */
private void updateDistribution() { private void updateDistribution() {
if (isDuplicate()) {
return;
}
final String name = HawkbitCommonUtil.trimAndNullIfEmpty(distNameTextField.getValue()); final String name = HawkbitCommonUtil.trimAndNullIfEmpty(distNameTextField.getValue());
final String version = HawkbitCommonUtil.trimAndNullIfEmpty(distVersionTextField.getValue()); final String version = HawkbitCommonUtil.trimAndNullIfEmpty(distVersionTextField.getValue());
final String distSetTypeName = HawkbitCommonUtil final String distSetTypeName = HawkbitCommonUtil
.trimAndNullIfEmpty((String) distsetTypeNameComboBox.getValue()); .trimAndNullIfEmpty((String) distsetTypeNameComboBox.getValue());
final DistributionSet currentDS = distributionSetManagement.findDistributionSetByIdWithDetails(editDistId);
final String desc = HawkbitCommonUtil.trimAndNullIfEmpty(descTextArea.getValue());
final boolean isMigStepReq = reqMigStepCheckbox.getValue();
if (duplicateCheck(name, version)) { /* identify the changes */
final DistributionSet currentDS = distributionSetManagement.findDistributionSetByIdWithDetails(editDistId); setDistributionValues(currentDS, name, version, distSetTypeName, desc, isMigStepReq);
final String desc = HawkbitCommonUtil.trimAndNullIfEmpty(descTextArea.getValue()); try {
final boolean isMigStepReq = reqMigStepCheckbox.getValue(); distributionSetManagement.updateDistributionSet(currentDS);
notificationMessage.displaySuccess(i18n.get("message.new.dist.save.success",
/* identify the changes */ new Object[] { currentDS.getName(), currentDS.getVersion() }));
setDistributionValues(currentDS, name, version, distSetTypeName, desc, isMigStepReq); // update table row+details layout
try { eventBus.publish(this, new DistributionTableEvent(BaseEntityEventType.UPDATED_ENTITY, currentDS));
distributionSetManagement.updateDistributionSet(currentDS); } catch (final EntityAlreadyExistsException entityAlreadyExistsException) {
notificationMessage.displaySuccess(i18n.get("message.new.dist.save.success", LOG.error("Update distribution failed {}", entityAlreadyExistsException);
new Object[] { currentDS.getName(), currentDS.getVersion() })); notificationMessage.displayValidationError(
// update table row+details layout i18n.get("message.distribution.no.update", currentDS.getName() + ":" + currentDS.getVersion()));
eventBus.publish(this, new DistributionTableEvent(BaseEntityEventType.UPDATED_ENTITY, currentDS));
} catch (final EntityAlreadyExistsException entityAlreadyExistsException) {
LOG.error("Update distribution failed {}", entityAlreadyExistsException);
notificationMessage.displayValidationError(
i18n.get("message.distribution.no.update", currentDS.getName() + ":" + currentDS.getVersion()));
}
} }
} }
@@ -217,24 +218,26 @@ public class DistributionAddUpdateWindowLayout extends CustomComponent {
*/ */
private void addNewDistribution() { private void addNewDistribution() {
editDistribution = Boolean.FALSE; editDistribution = Boolean.FALSE;
if (isDuplicate()) {
return;
}
final String name = HawkbitCommonUtil.trimAndNullIfEmpty(distNameTextField.getValue()); final String name = HawkbitCommonUtil.trimAndNullIfEmpty(distNameTextField.getValue());
final String version = HawkbitCommonUtil.trimAndNullIfEmpty(distVersionTextField.getValue()); final String version = HawkbitCommonUtil.trimAndNullIfEmpty(distVersionTextField.getValue());
final String distSetTypeName = HawkbitCommonUtil final String distSetTypeName = HawkbitCommonUtil
.trimAndNullIfEmpty((String) distsetTypeNameComboBox.getValue()); .trimAndNullIfEmpty((String) distsetTypeNameComboBox.getValue());
if (duplicateCheck(name, version)) { final String desc = HawkbitCommonUtil.trimAndNullIfEmpty(descTextArea.getValue());
final String desc = HawkbitCommonUtil.trimAndNullIfEmpty(descTextArea.getValue()); final boolean isMigStepReq = reqMigStepCheckbox.getValue();
final boolean isMigStepReq = reqMigStepCheckbox.getValue(); DistributionSet newDist = entityFactory.generateDistributionSet();
DistributionSet newDist = entityFactory.generateDistributionSet();
setDistributionValues(newDist, name, version, distSetTypeName, desc, isMigStepReq); setDistributionValues(newDist, name, version, distSetTypeName, desc, isMigStepReq);
newDist = distributionSetManagement.createDistributionSet(newDist); newDist = distributionSetManagement.createDistributionSet(newDist);
notificationMessage.displaySuccess(i18n.get("message.new.dist.save.success", notificationMessage.displaySuccess(
new Object[] { newDist.getName(), newDist.getVersion() })); i18n.get("message.new.dist.save.success", new Object[] { newDist.getName(), newDist.getVersion() }));
eventBus.publish(this, new DistributionTableEvent(BaseEntityEventType.NEW_ENTITY, newDist)); eventBus.publish(this, new DistributionTableEvent(BaseEntityEventType.NEW_ENTITY, newDist));
}
} }
/** /**
@@ -260,7 +263,10 @@ public class DistributionAddUpdateWindowLayout extends CustomComponent {
distributionSet.setRequiredMigrationStep(isMigStepReq); distributionSet.setRequiredMigrationStep(isMigStepReq);
} }
private boolean duplicateCheck(final String name, final String version) { private boolean isDuplicate() {
final String name = distNameTextField.getValue();
final String version = distVersionTextField.getValue();
final DistributionSet existingDs = distributionSetManagement.findDistributionSetByNameAndVersion(name, version); final DistributionSet existingDs = distributionSetManagement.findDistributionSetByNameAndVersion(name, version);
/* /*
* Distribution should not exists with the same name & version. Display * Distribution should not exists with the same name & version. Display
@@ -273,13 +279,12 @@ public class DistributionAddUpdateWindowLayout extends CustomComponent {
distVersionTextField.addStyleName("v-textfield-error"); distVersionTextField.addStyleName("v-textfield-error");
notificationMessage.displayValidationError( notificationMessage.displayValidationError(
i18n.get("message.duplicate.dist", new Object[] { existingDs.getName(), existingDs.getVersion() })); i18n.get("message.duplicate.dist", new Object[] { existingDs.getName(), existingDs.getVersion() }));
window.setIsDuplicate(Boolean.TRUE);
return false;
} else {
window.setIsDuplicate(Boolean.FALSE);
return true; return true;
} }
return false;
} }
/** /**
@@ -294,7 +299,6 @@ public class DistributionAddUpdateWindowLayout extends CustomComponent {
distsetTypeNameComboBox.removeStyleName(SPUIStyleDefinitions.SP_COMBOFIELD_ERROR); distsetTypeNameComboBox.removeStyleName(SPUIStyleDefinitions.SP_COMBOFIELD_ERROR);
descTextArea.clear(); descTextArea.clear();
reqMigStepCheckbox.clear(); reqMigStepCheckbox.clear();
} }
private void populateValuesOfDistribution(final Long editDistId) { private void populateValuesOfDistribution(final Long editDistId) {
@@ -322,6 +326,12 @@ public class DistributionAddUpdateWindowLayout extends CustomComponent {
} }
} }
/**
* Returns the dialog window for the distributions.
*
* @param editDistId
* @return window
*/
public CommonDialogWindow getWindow(final Long editDistId) { public CommonDialogWindow getWindow(final Long editDistId) {
eventBus.publish(this, DragEvent.HIDE_DROP_HINT); eventBus.publish(this, DragEvent.HIDE_DROP_HINT);
resetComponents(); resetComponents();
@@ -330,6 +340,7 @@ public class DistributionAddUpdateWindowLayout extends CustomComponent {
window = SPUIWindowDecorator.getWindow(i18n.get("caption.add.new.dist"), null, window = SPUIWindowDecorator.getWindow(i18n.get("caption.add.new.dist"), null,
SPUIDefinitions.CREATE_UPDATE_WINDOW, this, event -> saveDistribution(), null, null, formLayout, i18n); SPUIDefinitions.CREATE_UPDATE_WINDOW, this, event -> saveDistribution(), null, null, formLayout, i18n);
window.getButtonsLayout().removeStyleName("actionButtonsMargin"); window.getButtonsLayout().removeStyleName("actionButtonsMargin");
window.setCloseListener(() -> !isDuplicate());
return window; return window;
} }

View File

@@ -36,7 +36,7 @@ import com.vaadin.ui.UI;
*/ */
@SpringComponent @SpringComponent
@ViewScope @ViewScope
public class CreateUpdateDistributionTagLayoutWindow extends AbstractCreateUpdateTagLayout { public class CreateUpdateDistributionTagLayoutWindow extends AbstractCreateUpdateTagLayout<DistributionSetTag> {
private static final long serialVersionUID = 444276149954167545L; private static final long serialVersionUID = 444276149954167545L;
@@ -85,16 +85,20 @@ public class CreateUpdateDistributionTagLayoutWindow extends AbstractCreateUpdat
*/ */
@Override @Override
public void save(final ClickEvent event) { public void save(final ClickEvent event) {
final DistributionSetTag existingDistTag = tagManagement.findDistributionSetTag(tagName.getValue());
if (optiongroup.getValue().equals(createTagStr)) { if (optiongroup.getValue().equals(createTagStr)) {
if (!checkIsDuplicate(existingDistTag)) { if (!isDuplicate()) {
createNewTag(); createNewTag();
} }
} else { } else {
updateExistingTag(existingDistTag); updateExistingTag(findEntityByName());
} }
} }
@Override
protected DistributionSetTag findEntityByName() {
return tagManagement.findDistributionSetTag(tagName.getValue());
}
/** /**
* Create new tag. * Create new tag.
*/ */

View File

@@ -143,31 +143,33 @@ public class TargetAddUpdateWindowLayout extends CustomComponent {
} }
private void addNewTarget() { private void addNewTarget() {
final String newControlllerId = HawkbitCommonUtil.trimAndNullIfEmpty(controllerIDTextField.getValue()); if (isDuplicate()) {
if (duplicateCheck(newControlllerId)) { return;
final String newName = HawkbitCommonUtil.trimAndNullIfEmpty(nameTextField.getValue());
final String newDesc = HawkbitCommonUtil.trimAndNullIfEmpty(descTextArea.getValue());
/* create new target entity */
Target newTarget = entityFactory.generateTarget(newControlllerId);
/* set values to the new target entity */
setTargetValues(newTarget, newName, newDesc);
/* save new target */
newTarget = targetManagement.createTarget(newTarget);
final TargetTable targetTable = SpringContextHelper.getBean(TargetTable.class);
final Set<TargetIdName> s = new HashSet<>();
s.add(newTarget.getTargetIdName());
targetTable.setValue(s);
/* display success msg */
uINotification.displaySuccess(i18n.get("message.save.success", new Object[] { newTarget.getName() }));
} }
final String newControlllerId = HawkbitCommonUtil.trimAndNullIfEmpty(controllerIDTextField.getValue());
final String newName = HawkbitCommonUtil.trimAndNullIfEmpty(nameTextField.getValue());
final String newDesc = HawkbitCommonUtil.trimAndNullIfEmpty(descTextArea.getValue());
/* create new target entity */
Target newTarget = entityFactory.generateTarget(newControlllerId);
/* set values to the new target entity */
setTargetValues(newTarget, newName, newDesc);
/* save new target */
newTarget = targetManagement.createTarget(newTarget);
final TargetTable targetTable = SpringContextHelper.getBean(TargetTable.class);
final Set<TargetIdName> s = new HashSet<>();
s.add(newTarget.getTargetIdName());
targetTable.setValue(s);
/* display success msg */
uINotification.displaySuccess(i18n.get("message.save.success", new Object[] { newTarget.getName() }));
} }
public Window getWindow() { public Window getWindow() {
eventBus.publish(this, DragEvent.HIDE_DROP_HINT); eventBus.publish(this, DragEvent.HIDE_DROP_HINT);
window = SPUIWindowDecorator.getWindow(i18n.get("caption.add.new.target"), null, window = SPUIWindowDecorator.getWindow(i18n.get("caption.add.new.target"), null,
SPUIDefinitions.CREATE_UPDATE_WINDOW, this, event -> saveTargetListner(), null, null, formLayout, i18n); SPUIDefinitions.CREATE_UPDATE_WINDOW, this, event -> saveTargetListner(), null, null, formLayout, i18n);
window.setCloseListener(() -> !isDuplicate());
return window; return window;
} }
@@ -196,17 +198,17 @@ public class TargetAddUpdateWindowLayout extends CustomComponent {
target.setDescription(description); target.setDescription(description);
} }
private boolean duplicateCheck(final String newControlllerId) { private boolean isDuplicate() {
final String newControlllerId = controllerIDTextField.getValue();
final Target existingTarget = targetManagement.findTargetByControllerID(newControlllerId.trim()); final Target existingTarget = targetManagement.findTargetByControllerID(newControlllerId.trim());
if (existingTarget != null) { if (existingTarget != null) {
uINotification.displayValidationError( uINotification.displayValidationError(
i18n.get("message.target.duplicate.check", new Object[] { newControlllerId })); i18n.get("message.target.duplicate.check", new Object[] { newControlllerId }));
window.setIsDuplicate(Boolean.TRUE);
return false;
} else {
window.setIsDuplicate(Boolean.FALSE);
return true; return true;
} else {
return false;
} }
} }
/** /**

View File

@@ -34,7 +34,7 @@ import com.vaadin.ui.Button.ClickEvent;
*/ */
@SpringComponent @SpringComponent
@ViewScope @ViewScope
public class CreateUpdateTargetTagLayoutWindow extends AbstractCreateUpdateTagLayout { public class CreateUpdateTargetTagLayoutWindow extends AbstractCreateUpdateTagLayout<TargetTag> {
private static final long serialVersionUID = 2446682350481560235L; private static final long serialVersionUID = 2446682350481560235L;
@@ -102,16 +102,20 @@ public class CreateUpdateTargetTagLayoutWindow extends AbstractCreateUpdateTagLa
@Override @Override
public void save(final ClickEvent event) { public void save(final ClickEvent event) {
final TargetTag existingTag = tagManagement.findTargetTag(tagName.getValue());
if (optiongroup.getValue().equals(createTagStr)) { if (optiongroup.getValue().equals(createTagStr)) {
if (!checkIsDuplicate(existingTag)) { if (!isDuplicate()) {
createNewTag(); createNewTag();
} }
} else { } else {
updateExistingTag(existingTag); updateExistingTag(findEntityByName());
} }
} }
@Override
protected TargetTag findEntityByName() {
return tagManagement.findTargetTag(tagName.getValue());
}
/** /**
* Create new tag. * Create new tag.
*/ */

View File

@@ -706,28 +706,6 @@ public final class HawkbitCommonUtil {
} }
} }
/**
* Duplicate check - Unique Key.
*
* @param name
* as string
* @param version
* as string
* @param type
* key as string
* @return boolean as flag
*/
public static boolean isDuplicate(final String name, final String version, final String type) {
final SoftwareManagement swMgmtService = SpringContextHelper.getBean(SoftwareManagement.class);
final SoftwareModule swModule = swMgmtService.findSoftwareModuleByNameAndVersion(name, version,
swMgmtService.findSoftwareModuleTypeByName(type));
boolean duplicate = false;
if (swModule != null) {
duplicate = true;
}
return duplicate;
}
/** /**
* Add new base software module. * Add new base software module.
* *