From 6cc64639bd04ae561d5c79febd8d2ef3eebc03f0 Mon Sep 17 00:00:00 2001 From: Jonathan Philip Knoblauch Date: Fri, 11 Mar 2016 11:19:43 +0100 Subject: [PATCH] Fix for sonar issue: Methods should not have too many lines Signed-off-by: Jonathan Philip Knoblauch --- .../hawkbit/simulator/ui/GenerateDialog.java | 125 +++++++----- .../hawkbit/simulator/ui/SimulatorView.java | 189 ++++++++++-------- .../repository/DeploymentManagement.java | 61 +++--- 3 files changed, 208 insertions(+), 167 deletions(-) diff --git a/examples/hawkbit-device-simulator/src/main/java/org/eclipse/hawkbit/simulator/ui/GenerateDialog.java b/examples/hawkbit-device-simulator/src/main/java/org/eclipse/hawkbit/simulator/ui/GenerateDialog.java index e4509e082..00ee35d32 100644 --- a/examples/hawkbit-device-simulator/src/main/java/org/eclipse/hawkbit/simulator/ui/GenerateDialog.java +++ b/examples/hawkbit-device-simulator/src/main/java/org/eclipse/hawkbit/simulator/ui/GenerateDialog.java @@ -44,6 +44,17 @@ public class GenerateDialog extends Window { private final FormLayout formLayout = new FormLayout(); + private final TextField tf1; + private final TextField tf2; + private final TextField tf3; + private final TextField tf4; + private final TextField tf5; + private final TextField tf6; + + private final OptionGroup protocolGroup; + + private final Button buttonOk; + /** * Creates a new pop window for setting the configuration of simulating * devices. @@ -57,28 +68,28 @@ public class GenerateDialog extends Window { formLayout.setSpacing(true); formLayout.setMargin(true); - final TextField tf1 = new TextField("name prefix", "dmfSimulated"); + tf1 = new TextField("name prefix", "dmfSimulated"); tf1.setIcon(FontAwesome.INFO); tf1.setRequired(true); tf1.addValidator(new NullValidator("Must be given", false)); - final TextField tf2 = new TextField("amount", new ObjectProperty(10)); + tf2 = new TextField("amount", new ObjectProperty(10)); tf2.setIcon(FontAwesome.GEAR); tf2.setRequired(true); tf2.addValidator(new RangeValidator("Must be between 1 and 30000", Integer.class, 1, 30000)); - final TextField tf3 = new TextField("tenant", "default"); + tf3 = new TextField("tenant", "default"); tf3.setIcon(FontAwesome.USER); tf3.setRequired(true); tf3.addValidator(new NullValidator("Must be given", false)); - final TextField tf4 = new TextField("poll delay (sec)", new ObjectProperty(10)); + tf4 = new TextField("poll delay (sec)", new ObjectProperty(10)); tf4.setIcon(FontAwesome.CLOCK_O); tf4.setRequired(true); tf4.setVisible(false); tf4.addValidator(new RangeValidator("Must be between 1 and 60", Integer.class, 1, 60)); - final TextField tf5 = new TextField("base poll URL endpoint", "http://localhost:8080"); + tf5 = new TextField("base poll URL endpoint", "http://localhost:8080"); tf5.setColumns(50); tf5.setIcon(FontAwesome.FLAG_O); tf5.setRequired(true); @@ -86,57 +97,14 @@ public class GenerateDialog extends Window { tf5.addValidator(new RegexpValidator("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]", "is not an URL")); - final TextField tf6 = new TextField("gateway token", ""); + tf6 = new TextField("gateway token", ""); tf6.setColumns(50); tf6.setIcon(FontAwesome.FLAG_O); tf6.setRequired(true); tf6.setVisible(false); - final OptionGroup protocolGroup = new OptionGroup("Simulated Device Protocol"); - protocolGroup.addItem(Protocol.DMF_AMQP); - protocolGroup.addItem(Protocol.DDI_HTTP); - protocolGroup.setItemCaption(Protocol.DMF_AMQP, "Device Management Federation API (AMQP push)"); - protocolGroup.setItemCaption(Protocol.DDI_HTTP, "Direct Device Interface (HTTP poll)"); - protocolGroup.setNullSelectionAllowed(false); - protocolGroup.select(Protocol.DMF_AMQP); - protocolGroup.addValueChangeListener(new ValueChangeListener() { - private static final long serialVersionUID = 1L; - - @Override - public void valueChange(final ValueChangeEvent event) { - if (event.getProperty().getValue().equals(Protocol.DDI_HTTP)) { - tf4.setVisible(true); - tf5.setVisible(true); - tf6.setVisible(true); - } else { - tf4.setVisible(false); - tf5.setVisible(false); - tf6.setVisible(false); - } - } - }); - - final Button buttonOk = new Button("generate"); - buttonOk.setImmediate(true); - buttonOk.setIcon(FontAwesome.GEARS); - buttonOk.addClickListener(new ClickListener() { - private static final long serialVersionUID = 1L; - - @Override - public void buttonClick(final ClickEvent event) { - try { - callback.okButton(tf1.getValue(), tf3.getValue(), - Integer.valueOf(tf2.getValue().replace(".", "").replace(",", "")), - Integer.valueOf(tf4.getValue().replace(".", "")), new URL(tf5.getValue()), tf6.getValue(), - (Protocol) protocolGroup.getValue()); - } catch (final NumberFormatException e) { - LOGGER.info(e.getMessage(), e); - } catch (final MalformedURLException e) { - LOGGER.info(e.getMessage(), e); - } - GenerateDialog.this.close(); - } - }); + protocolGroup = createProtocolGroup(); + buttonOk = createOkButton(callback); tf1.addValueChangeListener(event -> checkValid(tf1, tf2, tf3, tf4, buttonOk)); tf2.addValueChangeListener(event -> checkValid(tf1, tf2, tf3, tf4, buttonOk)); @@ -231,4 +199,59 @@ public class GenerateDialog extends Window { void okButton(final String namePrefix, final String tenant, final int amount, final int pollDelay, final URL basePollURL, final String gatewayToken, final Protocol protocol); } + + private OptionGroup createProtocolGroup() { + + final OptionGroup protocolGroup = new OptionGroup("Simulated Device Protocol"); + protocolGroup.addItem(Protocol.DMF_AMQP); + protocolGroup.addItem(Protocol.DDI_HTTP); + protocolGroup.setItemCaption(Protocol.DMF_AMQP, "Device Management Federation API (AMQP push)"); + protocolGroup.setItemCaption(Protocol.DDI_HTTP, "Direct Device Interface (HTTP poll)"); + protocolGroup.setNullSelectionAllowed(false); + protocolGroup.select(Protocol.DMF_AMQP); + protocolGroup.addValueChangeListener(new ValueChangeListener() { + private static final long serialVersionUID = 1L; + + @Override + public void valueChange(final ValueChangeEvent event) { + if (event.getProperty().getValue().equals(Protocol.DDI_HTTP)) { + tf4.setVisible(true); + tf5.setVisible(true); + tf6.setVisible(true); + } else { + tf4.setVisible(false); + tf5.setVisible(false); + tf6.setVisible(false); + } + } + }); + return protocolGroup; + } + + private Button createOkButton(final GenerateDialogCallback callback) { + + final Button buttonOk = new Button("generate"); + buttonOk.setImmediate(true); + buttonOk.setIcon(FontAwesome.GEARS); + buttonOk.addClickListener(new ClickListener() { + private static final long serialVersionUID = 1L; + + @Override + public void buttonClick(final ClickEvent event) { + try { + callback.okButton(tf1.getValue(), tf3.getValue(), + Integer.valueOf(tf2.getValue().replace(".", "").replace(",", "")), + Integer.valueOf(tf4.getValue().replace(".", "")), new URL(tf5.getValue()), tf6.getValue(), + (Protocol) protocolGroup.getValue()); + } catch (final NumberFormatException e) { + LOGGER.info(e.getMessage(), e); + } catch (final MalformedURLException e) { + LOGGER.info(e.getMessage(), e); + } + GenerateDialog.this.close(); + } + }); + return buttonOk; + } + } diff --git a/examples/hawkbit-device-simulator/src/main/java/org/eclipse/hawkbit/simulator/ui/SimulatorView.java b/examples/hawkbit-device-simulator/src/main/java/org/eclipse/hawkbit/simulator/ui/SimulatorView.java index 25498cea7..b79d9e5ee 100644 --- a/examples/hawkbit-device-simulator/src/main/java/org/eclipse/hawkbit/simulator/ui/SimulatorView.java +++ b/examples/hawkbit-device-simulator/src/main/java/org/eclipse/hawkbit/simulator/ui/SimulatorView.java @@ -76,8 +76,8 @@ public class SimulatorView extends VerticalLayout implements View { private final Label caption = new Label("DMF/DDI Simulated Devices"); private final HorizontalLayout toolbar = new HorizontalLayout(); private final Grid grid = new Grid(); - private final ComboBox responseComboBox = new ComboBox("", Lists.newArrayList(ResponseStatus.SUCCESSFUL, - ResponseStatus.ERROR)); + private final ComboBox responseComboBox = new ComboBox("", + Lists.newArrayList(ResponseStatus.SUCCESSFUL, ResponseStatus.ERROR)); private BeanContainer beanContainer; @@ -97,6 +97,9 @@ public class SimulatorView extends VerticalLayout implements View { grid.setSizeFull(); grid.setCellStyleGenerator(new CellStyleGenerator() { + + private static final long serialVersionUID = 1L; + @Override public String getStyle(final CellReference cellReference) { return cellReference.getPropertyId().equals("status") ? "centeralign" : null; @@ -118,86 +121,8 @@ public class SimulatorView extends VerticalLayout implements View { grid.getColumn("swversion").setHeaderCaption("SW Version"); grid.getColumn("responseStatus").setHeaderCaption("Response Update Status"); grid.getColumn("progress").setRenderer(new ProgressBarRenderer()); - grid.getColumn("protocol").setConverter(new Converter() { - @Override - public Protocol convertToModel(final String value, final Class targetType, - final Locale locale) { - return null; - } - - @Override - public String convertToPresentation(final Protocol value, final Class targetType, - final Locale locale) { - switch (value) { - case DDI_HTTP: - return "DDI API (http)"; - case DMF_AMQP: - return "DMF API (amqp)"; - default: - return "unknown"; - } - } - - @Override - public Class getModelType() { - return Protocol.class; - } - - @Override - public Class getPresentationType() { - return String.class; - } - }); - grid.getColumn("status").setRenderer(new HtmlRenderer(), new Converter() { - private static final long serialVersionUID = 1L; - - @Override - public Status convertToModel(final String value, final Class targetType, - final Locale locale) { - return null; - } - - @Override - public String convertToPresentation(final Status value, final Class targetType, - final Locale locale) { - String style = null; - switch (value) { - case UNKNWON: - style = "&#x" - + Integer.toHexString(FontAwesome.QUESTION_CIRCLE.getCodepoint()) + ";"; - break; - case PEDNING: - style = "&#x" + Integer.toHexString(FontAwesome.REFRESH.getCodepoint()) - + ";"; - break; - case FINISH: - style = "&#x" - + Integer.toHexString(FontAwesome.CHECK_CIRCLE.getCodepoint()) + ";"; - break; - case ERROR: - style = "&#x" - + Integer.toHexString(FontAwesome.EXCLAMATION_CIRCLE.getCodepoint()) + ";"; - break; - default: - throw new IllegalStateException("unknown value"); - } - return style; - } - - @Override - public Class getModelType() { - return Status.class; - } - - @Override - public Class getPresentationType() { - return String.class; - } - }); + grid.getColumn("protocol").setConverter(getProtocolConverter()); + grid.getColumn("status").setRenderer(new HtmlRenderer(), getStatusConverter()); grid.removeColumn("tenant"); // grid combobox @@ -206,11 +131,13 @@ public class SimulatorView extends VerticalLayout implements View { responseComboBox.setNullSelectionAllowed(false); responseComboBox.setValue(ResponseStatus.SUCCESSFUL); responseComboBox.addValueChangeListener(new ValueChangeListener() { + + private static final long serialVersionUID = 1L; + @Override public void valueChange(final ValueChangeEvent event) { - beanContainer.getItemIds().forEach( - itemId -> beanContainer.getItem(itemId).getItemProperty("responseStatus") - .setValue(event.getProperty().getValue())); + beanContainer.getItemIds().forEach(itemId -> beanContainer.getItem(itemId) + .getItemProperty("responseStatus").setValue(event.getProperty().getValue())); } }); @@ -338,4 +265,96 @@ public class SimulatorView extends VerticalLayout implements View { } })); } + + private Converter getProtocolConverter() { + + return new Converter() { + + private static final long serialVersionUID = 1L; + + @Override + public Protocol convertToModel(final String value, final Class targetType, + final Locale locale) { + return null; + } + + @Override + public String convertToPresentation(final Protocol value, final Class targetType, + final Locale locale) { + switch (value) { + case DDI_HTTP: + return "DDI API (http)"; + case DMF_AMQP: + return "DMF API (amqp)"; + default: + return "unknown"; + } + } + + @Override + public Class getModelType() { + return Protocol.class; + } + + @Override + public Class getPresentationType() { + return String.class; + } + }; + + } + + private Converter getStatusConverter() { + return new Converter() { + private static final long serialVersionUID = 1L; + + @Override + public Status convertToModel(final String value, final Class targetType, + final Locale locale) { + return null; + } + + @Override + public String convertToPresentation(final Status value, final Class targetType, + final Locale locale) { + String style = null; + switch (value) { + case UNKNWON: + style = "&#x" + + Integer.toHexString(FontAwesome.QUESTION_CIRCLE.getCodepoint()) + ";"; + break; + case PEDNING: + style = "&#x" + Integer.toHexString(FontAwesome.REFRESH.getCodepoint()) + + ";"; + break; + case FINISH: + style = "&#x" + + Integer.toHexString(FontAwesome.CHECK_CIRCLE.getCodepoint()) + ";"; + break; + case ERROR: + style = "&#x" + + Integer.toHexString(FontAwesome.EXCLAMATION_CIRCLE.getCodepoint()) + ";"; + break; + default: + throw new IllegalStateException("unknown value"); + } + return style; + } + + @Override + public Class getModelType() { + return Status.class; + } + + @Override + public Class getPresentationType() { + return String.class; + } + }; + } + } diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/DeploymentManagement.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/DeploymentManagement.java index aad0839cd..11c3f60ae 100644 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/DeploymentManagement.java +++ b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/DeploymentManagement.java @@ -302,13 +302,10 @@ public class DeploymentManagement { .collect(Collectors.toMap(TargetWithActionType::getTargetId, Function.identity())); // split tIDs length into max entries in-statement because many database - // have constraint of - // max entries in in-statements e.g. Oracle with maximum 1000 elements, - // so we need to split - // the entries here and execute multiple statements - // we take the target only into account if the requested operation is no - // duplicate of a - // previous one + // have constraint of max entries in in-statements e.g. Oracle with + // maximum 1000 elements, so we need to split the entries here and + // execute multiple statements we take the target only into account if + // the requested operation is no duplicate of a previous one final List targets = Lists.partition(controllerIDs, Constants.MAX_ENTRIES_IN_STATEMENT).stream() .map(ids -> targetRepository .findAll(TargetSpecifications.hasControllerIdAndAssignedDistributionSetIdNot(ids, set.getId()))) @@ -326,10 +323,9 @@ public class DeploymentManagement { targets.stream().map(Target::getId).collect(Collectors.toList()), Constants.MAX_ENTRIES_IN_STATEMENT); // override all active actions and set them into canceling state, we - // need to remember which - // one we have been switched to canceling state because for targets - // which we have changed to - // canceling we don't want to publish the new action update event. + // need to remember which one we have been switched to canceling state + // because for targets which we have changed to canceling we don't want + // to publish the new action update event. final Set targetIdsCancellList = new HashSet<>(); targetIds.forEach(ids -> targetIdsCancellList.addAll(overrideObsoleteUpdateActions(ids))); @@ -349,27 +345,15 @@ public class DeploymentManagement { targetIds.forEach(tIds -> targetRepository.setAssignedDistributionSet(set, System.currentTimeMillis(), currentUser, tIds)); targetIds.forEach(tIds -> targetInfoRepository.setTargetUpdateStatus(TargetUpdateStatus.PENDING, tIds)); + final Map targetIdsToActions = actionRepository + .save(targets.stream().map(t -> createTargetAction(targetsWithActionMap, t, set, rollout, rolloutGroup)) + .collect(Collectors.toList())) + .stream().collect(Collectors.toMap(a -> a.getTarget().getControllerId(), Function.identity())); - final Map targetIdsToActions = actionRepository.save(targets.stream().map(t -> { - final Action tAction = new Action(); - final TargetWithActionType targetWithActionType = targetsWithActionMap.get(t.getControllerId()); - tAction.setActionType(targetWithActionType.getActionType()); - tAction.setForcedTime(targetWithActionType.getForceTime()); - tAction.setActive(true); - tAction.setStatus(Status.RUNNING); - tAction.setTarget(t); - tAction.setDistributionSet(set); - tAction.setRollout(rollout); - tAction.setRolloutGroup(rolloutGroup); - return tAction; - }).collect(Collectors.toList())).stream() - .collect(Collectors.toMap(a -> a.getTarget().getControllerId(), Function.identity())); - - // create initial action status when action is created so we - // remember the initial - // running status because we will change the status of the action itself - // and with this action - // status we have a nicer action history. + // create initial action status when action is created so we remember + // the initial running status because we will change the status + // of the action itself and with this action status we have a nicer + // action history. targetIdsToActions.values().forEach(action -> { final ActionStatus actionStatus = new ActionStatus(); actionStatus.setAction(action); @@ -405,6 +389,21 @@ public class DeploymentManagement { softwareModules)); } + private Action createTargetAction(final Map targetsWithActionMap, final Target t, + final DistributionSet set, final Rollout rollout, final RolloutGroup rolloutGroup) { + final Action tAction = new Action(); + final TargetWithActionType targetWithActionType = targetsWithActionMap.get(t.getControllerId()); + tAction.setActionType(targetWithActionType.getActionType()); + tAction.setForcedTime(targetWithActionType.getForceTime()); + tAction.setActive(true); + tAction.setStatus(Status.RUNNING); + tAction.setTarget(t); + tAction.setDistributionSet(set); + tAction.setRollout(rollout); + tAction.setRolloutGroup(rolloutGroup); + return tAction; + } + /** * Sends the {@link TargetAssignDistributionSetEvent} for a specific target * to the {@link EventBus}.