Skip to content

Commit

Permalink
Make constructor of JpaSoftwareModuleManagement and JpaSoftwareModule…
Browse files Browse the repository at this point in the history
…TypeManagement public to allow bean overrides (#913)

* set constructor to public
* fix typo
* add exception-message for localisation
* log exception

Signed-off-by: Nazife Basbaz <nazife.basbaz@bosch-si.com>
  • Loading branch information
nazifebasbaz authored and stefbehl committed Nov 15, 2019
1 parent e731585 commit ab009b2
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class JpaSoftwareModuleManagement implements SoftwareModuleManagement {

private final Database database;

JpaSoftwareModuleManagement(final EntityManager entityManager,
public JpaSoftwareModuleManagement(final EntityManager entityManager,
final DistributionSetRepository distributionSetRepository,
final SoftwareModuleRepository softwareModuleRepository,
final SoftwareModuleMetadataRepository softwareModuleMetadataRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class JpaSoftwareModuleTypeManagement implements SoftwareModuleTypeManage
private final NoCountPagingRepository criteriaNoCountDao;
private final Database database;

JpaSoftwareModuleTypeManagement(final DistributionSetTypeRepository distributionSetTypeRepository,
public JpaSoftwareModuleTypeManagement(final DistributionSetTypeRepository distributionSetTypeRepository,
final SoftwareModuleTypeRepository softwareModuleTypeRepository,
final VirtualPropertyReplacer virtualPropertyReplacer,
final SoftwareModuleRepository softwareModuleRepository, final NoCountPagingRepository criteriaNoCountDao,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ TargetManagement targetManagement(final EntityManager entityManager, final Quota

/**
* {@link JpaTargetFilterQueryManagement} bean.
*
*
* @param targetFilterQueryRepository
* holding {@link TargetFilterQuery} entities
* @param targetRepository
Expand Down Expand Up @@ -599,9 +599,9 @@ SoftwareModuleManagement softwareModuleManagement(final EntityManager entityMana
}

/**
* {@link JpaSoftwareModuleManagement} bean.
* {@link JpaSoftwareModuleTypeManagement} bean.
*
* @return a new {@link SoftwareModuleManagement}
* @return a new {@link SoftwareModuleTypeManagement}
*/
@Bean
@ConditionalOnMissingBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import java.util.Optional;

import javax.validation.ConstraintViolationException;

import org.eclipse.hawkbit.repository.EntityFactory;
import org.eclipse.hawkbit.repository.SoftwareModuleManagement;
import org.eclipse.hawkbit.repository.SoftwareModuleTypeManagement;
Expand All @@ -35,6 +37,8 @@
import org.eclipse.hawkbit.ui.utils.UIMessageIdProvider;
import org.eclipse.hawkbit.ui.utils.UINotification;
import org.eclipse.hawkbit.ui.utils.VaadinMessageSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vaadin.addons.lazyquerycontainer.BeanQueryFactory;
import org.vaadin.spring.events.EventBus;
import org.vaadin.spring.events.EventBus.UIEventBus;
Expand All @@ -53,38 +57,24 @@
*/
public class SoftwareModuleAddUpdateWindow extends CustomComponent {

private static final long serialVersionUID = 1L;
private static final Logger LOGGER = LoggerFactory.getLogger(SoftwareModuleAddUpdateWindow.class.getName());

private static final long serialVersionUID = 1L;
private final VaadinMessageSource i18n;

private final UINotification uiNotifcation;

private final transient EventBus.UIEventBus eventBus;

private final transient SoftwareModuleManagement softwareModuleManagement;

private final transient SoftwareModuleTypeManagement softwareModuleTypeManagement;

private final transient EntityFactory entityFactory;

private final AbstractTable<SoftwareModule> softwareModuleTable;
private TextField nameTextField;

private TextField versionTextField;

private TextField vendorTextField;

private ComboBox typeComboBox;

private TextArea descTextArea;

private Boolean editSwModule = Boolean.FALSE;

private Long baseSwModuleId;

private FormLayout formLayout;

private final AbstractTable<SoftwareModule> softwareModuleTable;

private Label softwareModuleType;

/**
Expand Down Expand Up @@ -118,101 +108,6 @@ public SoftwareModuleAddUpdateWindow(final VaadinMessageSource i18n, final UINot
createRequiredComponents();
}

/**
* Save or update the sw module.
*/
private final class SaveOnDialogCloseListener implements SaveDialogCloseListener {
@Override
public void saveOrUpdate() {
if (editSwModule) {
updateSwModule();
return;
}
addNewBaseSoftware();
}

@Override
public boolean canWindowSaveOrUpdate() {
return editSwModule || !isDuplicate();
}

private void addNewBaseSoftware() {
final String name = nameTextField.getValue();
final String version = versionTextField.getValue();
final String vendor = vendorTextField.getValue();
final String description = descTextArea.getValue();
final String type = typeComboBox.getValue() != null ? typeComboBox.getValue().toString() : null;

final SoftwareModuleType softwareModuleTypeByName = softwareModuleTypeManagement.getByName(type)
.orElseThrow(() -> new EntityNotFoundException(SoftwareModuleType.class, type));
final SoftwareModuleCreate softwareModule = entityFactory.softwareModule().create()
.type(softwareModuleTypeByName).name(name).version(version).description(description).vendor(vendor);

final SoftwareModule newSoftwareModule = softwareModuleManagement.create(softwareModule);
eventBus.publish(this, new SoftwareModuleEvent(BaseEntityEventType.ADD_ENTITY, newSoftwareModule));
uiNotifcation.displaySuccess(i18n.getMessage("message.save.success",
newSoftwareModule.getName() + ":" + newSoftwareModule.getVersion()));
softwareModuleTable.setValue(Sets.newHashSet(newSoftwareModule.getId()));
}

private boolean isDuplicate() {
final String name = nameTextField.getValue();
final String version = versionTextField.getValue();
final String type = typeComboBox.getValue() != null ? typeComboBox.getValue().toString() : null;

final Optional<Long> moduleType = softwareModuleTypeManagement.getByName(type)
.map(SoftwareModuleType::getId);
if (moduleType.isPresent() && softwareModuleManagement
.getByNameAndVersionAndType(name, version, moduleType.get()).isPresent()) {
uiNotifcation
.displayValidationError(i18n.getMessage("message.duplicate.softwaremodule", name, version));
return true;
}
return false;
}

/**
* updates a softwareModule
*/
private void updateSwModule() {
final SoftwareModule newSWModule = softwareModuleManagement.update(entityFactory.softwareModule()
.update(baseSwModuleId).description(descTextArea.getValue()).vendor(vendorTextField.getValue()));
if (newSWModule != null) {
uiNotifcation.displaySuccess(i18n.getMessage("message.save.success",
newSWModule.getName() + ":" + newSWModule.getVersion()));

eventBus.publish(this, new SoftwareModuleEvent(BaseEntityEventType.UPDATED_ENTITY, newSWModule));
}
}

}

/**
* Creates window for new software module.
*
* @return reference of {@link com.vaadin.ui.Window} to add new software
* module.
*/
public CommonDialogWindow createAddSoftwareModuleWindow() {
return createUpdateSoftwareModuleWindow(null);
}

/**
* Creates window for update software module.
*
* @param baseSwModuleId
* id of the software module to edit.
* @return reference of {@link com.vaadin.ui.Window} to update software
* module.
*/
public CommonDialogWindow createUpdateSoftwareModuleWindow(final Long baseSwModuleId) {
this.baseSwModuleId = baseSwModuleId;
resetComponents();
populateTypeNameCombo();
populateValuesOfSwModule();
return createWindow();
}

private void createRequiredComponents() {

nameTextField = createTextField("textfield.name", UIComponentIdProvider.SOFT_MODULE_NAME,
Expand Down Expand Up @@ -243,10 +138,30 @@ private TextField createTextField(final String in18Key, final String id, final i
.buildTextComponent();
}

private void populateTypeNameCombo() {
typeComboBox.setContainerDataSource(
HawkbitCommonUtil.createLazyQueryContainer(new BeanQueryFactory<>(SoftwareModuleTypeBeanQuery.class)));
typeComboBox.setItemCaptionPropertyId(SPUILabelDefinitions.VAR_NAME);
/**
* Creates window for new software module.
*
* @return reference of {@link com.vaadin.ui.Window} to add new software
* module.
*/
public CommonDialogWindow createAddSoftwareModuleWindow() {
return createUpdateSoftwareModuleWindow(null);
}

/**
* Creates window for update software module.
*
* @param baseSwModuleId
* id of the software module to edit.
* @return reference of {@link com.vaadin.ui.Window} to update software
* module.
*/
public CommonDialogWindow createUpdateSoftwareModuleWindow(final Long baseSwModuleId) {
this.baseSwModuleId = baseSwModuleId;
resetComponents();
populateTypeNameCombo();
populateValuesOfSwModule();
return createWindow();
}

private void resetComponents() {
Expand All @@ -260,6 +175,30 @@ private void resetComponents() {
editSwModule = Boolean.FALSE;
}

private void populateTypeNameCombo() {
typeComboBox.setContainerDataSource(
HawkbitCommonUtil.createLazyQueryContainer(new BeanQueryFactory<>(SoftwareModuleTypeBeanQuery.class)));
typeComboBox.setItemCaptionPropertyId(SPUILabelDefinitions.VAR_NAME);
}

/**
* fill the data of a softwareModule in the content of the window
*/
private void populateValuesOfSwModule() {
if (baseSwModuleId == null) {
return;
}
editSwModule = Boolean.TRUE;
softwareModuleManagement.get(baseSwModuleId).ifPresent(swModule -> {
nameTextField.setValue(swModule.getName());
versionTextField.setValue(swModule.getVersion());
vendorTextField.setValue(swModule.getVendor());
descTextArea.setValue(swModule.getDescription());
softwareModuleType = new LabelBuilder().name(swModule.getType().getName())
.caption(i18n.getMessage(UIMessageIdProvider.CAPTION_ARTIFACT_SOFTWARE_MODULE_TYPE)).buildLabel();
});
}

private CommonDialogWindow createWindow() {
final Label madatoryStarLabel = new Label("*");
madatoryStarLabel.setStyleName("v-caption v-required-field-indicator");
Expand Down Expand Up @@ -293,26 +232,86 @@ private CommonDialogWindow createWindow() {
return window;
}

public FormLayout getFormLayout() {
return formLayout;
}

/**
* fill the data of a softwareModule in the content of the window
* Save or update the sw module.
*/
private void populateValuesOfSwModule() {
if (baseSwModuleId == null) {
return;
private final class SaveOnDialogCloseListener implements SaveDialogCloseListener {
@Override
public boolean canWindowSaveOrUpdate() {
return editSwModule || !isDuplicate();
}

@Override
public void saveOrUpdate() {
if (editSwModule) {
updateSwModule();
return;
}
addNewBaseSoftware();
}

/**
* updates a softwareModule
*/
private void updateSwModule() {
final SoftwareModule newSWModule = softwareModuleManagement.update(entityFactory.softwareModule()
.update(baseSwModuleId).description(descTextArea.getValue()).vendor(vendorTextField.getValue()));
if (newSWModule != null) {
uiNotifcation.displaySuccess(i18n.getMessage("message.save.success",
newSWModule.getName() + ":" + newSWModule.getVersion()));

eventBus.publish(this, new SoftwareModuleEvent(BaseEntityEventType.UPDATED_ENTITY, newSWModule));
}
}

private void addNewBaseSoftware() {
final String name = nameTextField.getValue();
final String version = versionTextField.getValue();
final String vendor = vendorTextField.getValue();
final String description = descTextArea.getValue();
final String type = typeComboBox.getValue() != null ? typeComboBox.getValue().toString() : null;

final SoftwareModuleType softwareModuleTypeByName = softwareModuleTypeManagement.getByName(type)
.orElseThrow(() -> new EntityNotFoundException(SoftwareModuleType.class, type));
final SoftwareModuleCreate softwareModule = entityFactory.softwareModule().create()
.type(softwareModuleTypeByName).name(name).version(version).description(description).vendor(vendor);
final SoftwareModule newSoftwareModule;
try {
newSoftwareModule = softwareModuleManagement.create(softwareModule);
} catch (ConstraintViolationException ex) {
String message = "This SoftwareModuleName is not valid! "
+ "Please choose a SoftwareModuleName without the characters /, ?, #, blank, and quota chars"
+ ex.getMessage();
LOGGER.warn(message, ex);
uiNotifcation.displayValidationError(i18n.getMessage("message.save.fail", name + ":" + version));
return;
}
eventBus.publish(this, new SoftwareModuleEvent(BaseEntityEventType.ADD_ENTITY, newSoftwareModule));
uiNotifcation.displaySuccess(i18n.getMessage("message.save.success",
newSoftwareModule.getName() + ":" + newSoftwareModule.getVersion()));
softwareModuleTable.setValue(Sets.newHashSet(newSoftwareModule.getId()));
}

private boolean isDuplicate() {
final String name = nameTextField.getValue();
final String version = versionTextField.getValue();
final String type = typeComboBox.getValue() != null ? typeComboBox.getValue().toString() : null;

final Optional<Long> moduleType = softwareModuleTypeManagement.getByName(type)
.map(SoftwareModuleType::getId);
if (moduleType.isPresent() && softwareModuleManagement
.getByNameAndVersionAndType(name, version, moduleType.get()).isPresent()) {
uiNotifcation
.displayValidationError(i18n.getMessage("message.duplicate.softwaremodule", name, version));
return true;
}
return false;
}
editSwModule = Boolean.TRUE;
softwareModuleManagement.get(baseSwModuleId).ifPresent(swModule -> {
nameTextField.setValue(swModule.getName());
versionTextField.setValue(swModule.getVersion());
vendorTextField.setValue(swModule.getVendor());
descTextArea.setValue(swModule.getDescription());
softwareModuleType = new LabelBuilder().name(swModule.getType().getName())
.caption(i18n.getMessage(UIMessageIdProvider.CAPTION_ARTIFACT_SOFTWARE_MODULE_TYPE)).buildLabel();
});
}

public FormLayout getFormLayout() {
return formLayout;
}

}
1 change: 1 addition & 0 deletions hawkbit-ui/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ tooltip.in.time = In Time

# Notification messages prefix with - message
message.save.success = {0} saved successfully
message.save.fail = {0} could not be saved, possibly due to invalid user input
message.update.success = {0} updated successfully
message.delete.success = {0} deleted successfully
message.dist.pending.action = Target {0} is already assigned with distribution {1} . Pending for action
Expand Down

0 comments on commit ab009b2

Please sign in to comment.