Arik Hadas has uploaded a new change for review.

Change subject: frontend: reduce duplicate code related to run-once capability 
- cont
......................................................................

frontend: reduce duplicate code related to run-once capability - cont

This patch is the last part in the effort to reduce the duplicate code
related to run-once capability. The creation of UICommands that are
related to run-once capability was moved into the run-once models, and
the event-handling code that is tightly related to the run-once model
was also moved into the run-once models.

Change-Id: I774524ca7bd4af35d8c3140e2ce23f369d727e28
Signed-off-by: Arik Hadas <[email protected]>
---
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/Model.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/IUserPortalListModel.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/UserPortalListModel.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/RunOnceModel.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/UserPortalRunOnceModel.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/VmListModel.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/WebadminRunOnceModel.java
7 files changed, 82 insertions(+), 65 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/04/13904/1

diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/Model.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/Model.java
index 2f12a4f..890c6b9 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/Model.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/Model.java
@@ -22,6 +22,8 @@
 public class Model extends PropertyChangeNotifier implements IEventListener, 
ICommandTarget, IProvidePropertyChangedEvent
 {
 
+    public static final String CANCEL_COMMAND = "Cancel"; //$NON-NLS-1$
+
     private Event privatePropertyChangedEvent;
 
     @Override
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/IUserPortalListModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/IUserPortalListModel.java
index c9b1992..cd6f187 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/IUserPortalListModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/IUserPortalListModel.java
@@ -16,6 +16,7 @@
 import org.ovirt.engine.ui.uicommonweb.models.ConsoleProtocol;
 import org.ovirt.engine.ui.uicommonweb.models.HasConsoleModel;
 import org.ovirt.engine.ui.uicommonweb.models.ListWithDetailsModel;
+import org.ovirt.engine.ui.uicommonweb.models.Model;
 import org.ovirt.engine.ui.uicompat.ConstantsManager;
 import org.ovirt.engine.ui.uicompat.PropertyChangedEventArgs;
 
@@ -107,7 +108,7 @@
             editConsole();
         } else if (StringHelper.stringsEqual(command.getName(), 
"OnEditConsoleSave")) { //$NON-NLS-1$
             onEditConsoleSave();
-        } else if (StringHelper.stringsEqual(command.getName(), "Cancel")) 
{//$NON-NLS-1$
+        } else if (StringHelper.stringsEqual(command.getName(), 
Model.CANCEL_COMMAND)) {
             cancel();
         }
     }
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/UserPortalListModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/UserPortalListModel.java
index b5b97f7..e46bd25 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/UserPortalListModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/UserPortalListModel.java
@@ -505,7 +505,7 @@
         }
         else if (StringHelper.stringsEqual(command.getName(), "OnRunOnce")) 
//$NON-NLS-1$
         {
-            OnRunOnce();
+            cancel();
         }
         else if (StringHelper.stringsEqual(command.getName(), "OnChangeCD")) 
//$NON-NLS-1$
         {
@@ -689,40 +689,11 @@
         VM vm = (VM) selectedItem.getEntity();
 
         RunOnceModel model = new UserPortalRunOnceModel(vm,
-                
getCustomPropertiesKeysList().get(vm.getVdsGroupCompatibilityVersion()));
+                
getCustomPropertiesKeysList().get(vm.getVdsGroupCompatibilityVersion()),
+                this);
         setWindow(model);
 
         model.init();
-
-        model.getCommands().add(new UICommand("OnRunOnce", this) //$NON-NLS-1$
-           .setTitle(ConstantsManager.getInstance().getConstants().ok())
-           .setIsDefault(true));
-
-        model.getCommands().add(new UICommand("Cancel", this) //$NON-NLS-1$
-           .setTitle(ConstantsManager.getInstance().getConstants().cancel())
-           .setIsCancel(true));
-    }
-
-    private void OnRunOnce()
-    {
-        RunOnceModel model = (RunOnceModel) getWindow();
-
-        if (!model.Validate())
-        {
-            return;
-        }
-
-        getWindow().StartProgress(null);
-
-        Frontend.RunAction(VdcActionType.RunVmOnce, 
model.createRunVmOnceParams(),
-                new IFrontendActionAsyncCallback() {
-                    @Override
-                    public void Executed(FrontendActionAsyncResult result) {
-                        stopProgress(result.getState());
-                        cancel();
-                    }
-                }, this);
-
     }
 
     private void UpdateActionAvailability()
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/RunOnceModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/RunOnceModel.java
index 589a097..ca96ea8 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/RunOnceModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/RunOnceModel.java
@@ -17,7 +17,9 @@
 import org.ovirt.engine.ui.frontend.AsyncQuery;
 import org.ovirt.engine.ui.frontend.Frontend;
 import org.ovirt.engine.ui.frontend.INewAsyncCallback;
+import org.ovirt.engine.ui.uicommonweb.ICommandTarget;
 import org.ovirt.engine.ui.uicommonweb.Linq;
+import org.ovirt.engine.ui.uicommonweb.UICommand;
 import org.ovirt.engine.ui.uicommonweb.dataprovider.AsyncDataProvider;
 import org.ovirt.engine.ui.uicommonweb.models.EntityModel;
 import org.ovirt.engine.ui.uicommonweb.models.ListModel;
@@ -35,10 +37,18 @@
 @SuppressWarnings("unused")
 public abstract class RunOnceModel extends Model
 {
+
+    public static final String RUN_ONCE_COMMAND = "OnRunOnce"; //$NON-NLS-1$
+
     /** The VM that is about to run */
     protected final VM vm;
     /** Custom properties for the running */
     protected final ArrayList<String> customPropertiesKeysList;
+    /** Listener for events that are triggered by this model */
+    protected ICommandTarget commandTarget;
+
+    protected final UICommand runOnceCommand;
+    protected final UICommand cancelCommand;
 
     private EntityModel privateAttachFloppy;
 
@@ -439,10 +449,11 @@
         privateCustomPropertiesKeysList = value;
     }
 
-    public RunOnceModel(VM vm, ArrayList<String> customPropertiesKeysList)
+    public RunOnceModel(VM vm, ArrayList<String> customPropertiesKeysList, 
ICommandTarget commandTarget)
     {
         this.vm = vm;
         this.customPropertiesKeysList = customPropertiesKeysList;
+        this.commandTarget = commandTarget;
 
         setAttachFloppy(new EntityModel());
         getAttachFloppy().getEntityChangedEvent().addListener(this);
@@ -494,6 +505,16 @@
         setIsHostTabVisible(true);
 
         setIsCustomPropertiesSheetVisible(true);
+
+        runOnceCommand = new UICommand(RunOnceModel.RUN_ONCE_COMMAND, this)
+         .setTitle(ConstantsManager.getInstance().getConstants().ok())
+         .setIsDefault(true);
+
+        cancelCommand = new UICommand(Model.CANCEL_COMMAND, this)
+         .setTitle(ConstantsManager.getInstance().getConstants().cancel())
+         .setIsCancel(true);
+
+        getCommands().addAll(Arrays.asList(runOnceCommand, cancelCommand));
     }
 
     public void init() {
@@ -538,7 +559,7 @@
                 vncProtocol : qxlProtocol);
     }
 
-    public RunVmOnceParams createRunVmOnceParams() {
+    protected RunVmOnceParams createRunVmOnceParams() {
         RunVmOnceParams params = new RunVmOnceParams();
         params.setVmId(vm.getId());
         params.setBootSequence(getBootSequence().getSequence());
@@ -878,4 +899,19 @@
                 && customPropertyValidation;
     }
 
+    @Override
+    public void ExecuteCommand(UICommand command)
+    {
+        if (command == runOnceCommand)
+        {
+            if (Validate()) {
+                onRunOnce();
+            }
+        }
+        else if (command == cancelCommand)
+        {
+            commandTarget.ExecuteCommand(command);
+        }
+    }
+    protected abstract void onRunOnce();
 }
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/UserPortalRunOnceModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/UserPortalRunOnceModel.java
index 2e8c67c..59deebd 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/UserPortalRunOnceModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/UserPortalRunOnceModel.java
@@ -3,13 +3,18 @@
 import java.util.ArrayList;
 
 import org.ovirt.engine.core.common.action.RunVmOnceParams;
+import org.ovirt.engine.core.common.action.VdcActionType;
 import org.ovirt.engine.core.common.businessentities.VM;
+import org.ovirt.engine.ui.frontend.Frontend;
+import org.ovirt.engine.ui.uicommonweb.ICommandTarget;
 import org.ovirt.engine.ui.uicompat.ConstantsManager;
+import org.ovirt.engine.ui.uicompat.FrontendActionAsyncResult;
+import org.ovirt.engine.ui.uicompat.IFrontendActionAsyncCallback;
 
 public class UserPortalRunOnceModel extends RunOnceModel {
 
-   public UserPortalRunOnceModel(VM vm, ArrayList<String> 
customPropertiesKeysList) {
-       super(vm, customPropertiesKeysList);
+   public UserPortalRunOnceModel(VM vm, ArrayList<String> 
customPropertiesKeysList, ICommandTarget commandTarget) {
+       super(vm, customPropertiesKeysList, commandTarget);
    }
 
    @Override
@@ -26,7 +31,7 @@
    }
 
    @Override
-   public RunVmOnceParams createRunVmOnceParams() {
+   protected RunVmOnceParams createRunVmOnceParams() {
        RunVmOnceParams params = super.createRunVmOnceParams();
        // Sysprep params
        if (getSysPrepDomainName().getSelectedItem() != null)
@@ -36,4 +41,18 @@
 
        return params;
    }
+
+   @Override
+   protected void onRunOnce() {
+       StartProgress(null);
+
+       Frontend.RunAction(VdcActionType.RunVmOnce, createRunVmOnceParams(),
+               new IFrontendActionAsyncCallback() {
+                   @Override
+                   public void Executed(FrontendActionAsyncResult result) {
+                       StopProgress();
+                       commandTarget.ExecuteCommand(runOnceCommand);
+                   }
+               }, this);
+   }
 }
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/VmListModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/VmListModel.java
index 13ab1f1..955cc6d 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/VmListModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/VmListModel.java
@@ -1347,32 +1347,11 @@
     {
         VM vm = (VM) getSelectedItem();
         RunOnceModel model = new WebadminRunOnceModel(vm,
-                
getCustomPropertiesKeysList().get(vm.getVdsGroupCompatibilityVersion()));
+                
getCustomPropertiesKeysList().get(vm.getVdsGroupCompatibilityVersion()),
+                this);
         setWindow(model);
 
         model.init();
-
-        model.getCommands().add(new UICommand("OnRunOnce", this) //$NON-NLS-1$
-           .setTitle(ConstantsManager.getInstance().getConstants().ok())
-           .setIsDefault(true));
-
-        model.getCommands().add(new UICommand("Cancel", this) //$NON-NLS-1$
-           .setTitle(ConstantsManager.getInstance().getConstants().cancel())
-           .setIsCancel(true));
-    }
-
-    private void OnRunOnce()
-    {
-        RunOnceModel model = (RunOnceModel) getWindow();
-
-        if (!model.Validate())
-        {
-            return;
-        }
-
-        Frontend.RunAction(VdcActionType.RunVmOnce, 
model.createRunVmOnceParams(), null, this);
-
-        Cancel();
     }
 
     private void NewTemplate()
@@ -2575,7 +2554,7 @@
         }
         else if (StringHelper.stringsEqual(command.getName(), "OnRunOnce")) 
//$NON-NLS-1$
         {
-            OnRunOnce();
+            Cancel();
         }
         else if (StringHelper.stringsEqual(command.getName(), 
"OnNewTemplate")) //$NON-NLS-1$
         {
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/WebadminRunOnceModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/WebadminRunOnceModel.java
index 098fda6..eac57d1 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/WebadminRunOnceModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/WebadminRunOnceModel.java
@@ -4,18 +4,21 @@
 import java.util.List;
 
 import org.ovirt.engine.core.common.action.RunVmOnceParams;
+import org.ovirt.engine.core.common.action.VdcActionType;
 import org.ovirt.engine.core.common.businessentities.VDS;
 import org.ovirt.engine.core.common.businessentities.VDSStatus;
 import org.ovirt.engine.core.common.businessentities.VM;
 import org.ovirt.engine.ui.frontend.AsyncQuery;
+import org.ovirt.engine.ui.frontend.Frontend;
 import org.ovirt.engine.ui.frontend.INewAsyncCallback;
+import org.ovirt.engine.ui.uicommonweb.ICommandTarget;
 import org.ovirt.engine.ui.uicommonweb.dataprovider.AsyncDataProvider;
 import org.ovirt.engine.ui.uicompat.ConstantsManager;
 
 public class WebadminRunOnceModel extends RunOnceModel {
 
-    public WebadminRunOnceModel(VM vm, ArrayList<String> 
customPropertiesKeysList) {
-        super(vm, customPropertiesKeysList);
+    public WebadminRunOnceModel(VM vm, ArrayList<String> 
customPropertiesKeysList, ICommandTarget commandTarget) {
+        super(vm, customPropertiesKeysList, commandTarget);
     }
 
     @Override
@@ -61,7 +64,7 @@
     }
 
     @Override
-    public RunVmOnceParams createRunVmOnceParams() {
+    protected RunVmOnceParams createRunVmOnceParams() {
         RunVmOnceParams params = super.createRunVmOnceParams();
 
         if ((Boolean) getIsAutoAssign().getEntity()) {
@@ -82,4 +85,10 @@
 
         return params;
     }
+
+    @Override
+    protected void onRunOnce() {
+        Frontend.RunAction(VdcActionType.RunVmOnce, createRunVmOnceParams(), 
null, this);
+        commandTarget.ExecuteCommand(runOnceCommand);
+    }
 }


--
To view, visit http://gerrit.ovirt.org/13904
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I774524ca7bd4af35d8c3140e2ce23f369d727e28
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Arik Hadas <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to