EDT:Tutorial: RUI With DataBase Lesson 9 Code, 0.8.0

From Eclipsepedia

Jump to: navigation, search

Access a database with EGL Rich UI > Resources

Finished code for PaymentLib.egl after lesson 9

The following code is the text of the PaymentLib.egl file after lesson 9.

 package handlers;
 
 import org.eclipse.edt.rui.mvc.Controller;
 import org.eclipse.edt.rui.mvc.FormField;
 import org.eclipse.edt.rui.mvc.FormManager;
 import org.eclipse.edt.rui.mvc.MVC;
 import org.eclipse.edt.rui.widgets.DataGrid;
 import org.eclipse.edt.rui.widgets.DataGridColumn;
 import org.eclipse.edt.rui.widgets.DataGridLib;
 import org.eclipse.edt.rui.widgets.Div;
 import org.eclipse.edt.rui.widgets.GridLayout;
 import org.eclipse.edt.rui.widgets.GridLayoutData;
 import org.eclipse.edt.rui.widgets.GridLayoutLib;
 import org.eclipse.edt.rui.widgets.TextLabel;
 import dojo.widgets.DojoButton;
 import dojo.widgets.DojoCheckBox;
 import dojo.widgets.DojoComboBox;
 import dojo.widgets.DojoCurrencyTextBox;
 import dojo.widgets.DojoDateTextBox;
 import dojo.widgets.DojoLib;
 import dojo.widgets.DojoTextField;
 import dojo.widgets.DojoTitlePane;
 import eglx.persistence.sql.column;
 import eglx.ui.rui.Event;
 import eglx.ui.rui.RUIHandler;
 import eglx.ui.rui.Widget;
 import eglx.ui.rui.view;
 import eglx.ui.rui.widget;
 import libraries.PaymentLib;
 import records.paymentRec;
 import services.SQLService;
 
 handler PaymentFileMaintenance type RUIhandler{
    initialUI =[ui], onConstructionFunction = start, 
    cssFile = "css/PaymentClient.css", title = "PaymentFileMaintenance"}
 
    dbService SQLService? ; 		// EDT 0.8.0
    dedicatedServiceBinding HttpProxy; // EDT 0.8.0
    
 
    ui GridLayout{columns = 2, rows = 2, cellPadding = 4, 
                  children =
                     [detailButtonLayout, editPane, buttonLayout, allPayments_ui ]
                 };
 
    allPayments paymentRec[];
 
    allPayments_ui DataGrid{
       layoutData = new GridLayoutData{
                        row = 2, column = 1,
                        verticalAlignment = GridLayoutLib.VALIGN_TOP},
       selectionListeners ::= cellClicked,
       columns =[
          new DataGridColumn{name = "category", displayName = "Type", 
                             width = 90, formatters = [ formatCategory ]},
          new DataGridColumn{name = "description", displayName = "Description", 
                             width = 120},
          new DataGridColumn{name = "amount", displayName = "Amount due", 
                             width = 90, alignment = DataGridLib.ALIGN_RIGHT}
       ], 
       data = allPayments as any[],
       selectionMode = DataGridLib.SINGLE_SELECTION};
 
    buttonLayout GridLayout{ 
       layoutData = new GridLayoutData{ row = 1, column = 1 }, 
       cellPadding = 4, rows = 1, columns = 3,
       children = [ sampleButton, deleteButton, addButton ] };
 
    addButton DojoButton{ 
       layoutData = new GridLayoutData{ row = 1, column = 1 },
       text = "Add", onClick ::= addRow };
 
    deleteButton DojoButton{ 
       layoutData = new GridLayoutData{ row = 1, column = 2 },
       text = "Delete", onClick ::= deleteRow };
 
    sampleButton DojoButton{ 
       layoutData = new GridLayoutData{ row = 1, column = 3 },
       text = "Sample", onClick ::= sampleData };
 
    selectedPayment paymentRec;
 
    editPane DojoTitlePane{ 
       layoutData = new GridLayoutData{ row = 2, column = 2,
                                        verticalAlignment = GridLayoutLib.VALIGN_TOP },
       title = "Payment record",
       isOpen=true, duration=1000, width = "350", 
       children = 
          [ new Div {children = [ selectedPayment_ui ]}]
    };
 
    selectedPayment_ui GridLayout { 
       rows = 9, columns = 2, cellPadding = 4, 
       children = [ selectedPayment_paymentId_nameLabel,
                    selectedPayment_paymentId_field,
                    selectedPayment_category_nameLabel,
                    selectedPayment_category_comboBox,
                    selectedPayment_description_nameLabel,
                    selectedPayment_description_field,
                    selectedPayment_amount_nameLabel,
                    selectedPayment_amount_textBox,
                    selectedPayment_fixedPayment_nameLabel,
                    selectedPayment_fixedPayment_checkBox,
                    selectedPayment_dueDate_nameLabel,
                    selectedPayment_dueDate_textBox,
                    selectedPayment_payeeName_nameLabel,
                    selectedPayment_payeeName_field,
                    selectedPayment_payeeAddress1_nameLabel,
                    selectedPayment_payeeAddress1_field,
                    selectedPayment_payeeAddress2_nameLabel,
                    selectedPayment_payeeAddress2_field ] };
 
    selectedPayment_paymentId_nameLabel TextLabel { 
       text="Key:" , 
       layoutData = new GridLayoutData { row = 1, column = 1} };
 
    selectedPayment_paymentId_field DojoTextField {
       layoutData = new GridLayoutData { row = 1, column = 2},
       readOnly = true };
 
    selectedPayment_paymentId_controller Controller { 
       @MVC {model = selectedPayment.paymentId, 
             view = selectedPayment_paymentId_field}, 
             validStateSetter = handleValidStateChange_selectedPayment};
 
    selectedPayment_paymentId_formField FormField { 
       controller = selectedPayment_paymentId_controller, 
       nameLabel = selectedPayment_paymentId_nameLabel};
 
    selectedPayment_category_nameLabel TextLabel { 
       text="Category:", 
       layoutData = new GridLayoutData { row = 2, column = 1} };
 
    selectedPayment_category_comboBox DojoComboBox { 
       values = PaymentLib.categories,
       layoutData = new GridLayoutData { row = 2, column = 2} };
 
    selectedPayment_category_controller Controller { 
       @MVC {model = selectedPayment.category, 
             view = selectedPayment_category_comboBox}, 
       validStateSetter = handleValidStateChange_selectedPayment};
 
    selectedPayment_category_formField FormField { 
       controller = selectedPayment_category_controller, 
       nameLabel = selectedPayment_category_nameLabel};
 
    selectedPayment_description_nameLabel TextLabel { 
       text="Description:" , 
       layoutData = new GridLayoutData { row = 3, column = 1} };
 
    selectedPayment_description_field DojoTextField {
       layoutData = new GridLayoutData { row = 3, column = 2} };
 
    selectedPayment_description_controller Controller { 
       @MVC {model = selectedPayment.description, 
             view = selectedPayment_description_field}, 
       validStateSetter = handleValidStateChange_selectedPayment};
 
    selectedPayment_description_formField FormField { 
       controller = selectedPayment_description_controller, 
       nameLabel = selectedPayment_description_nameLabel};
 
    selectedPayment_amount_nameLabel TextLabel { 
       text="Amount:", 
       layoutData = new GridLayoutData { row = 4, column = 1} };
 
    selectedPayment_amount_textBox DojoCurrencyTextBox { 
       currency = "USD", value = selectedPayment.amount, width = 166, 
       errorMessage="Amount is not valid.", 
       layoutData = new GridLayoutData { row = 4, column = 2} };
 
    selectedPayment_amount_controller Controller { 
       @MVC {model = selectedPayment.amount, 
             view = selectedPayment_amount_textBox}, 
 
    validStateSetter = handleValidStateChange_selectedPayment};
 
    selectedPayment_amount_formField FormField { 
       controller = selectedPayment_amount_controller, 
       nameLabel = selectedPayment_amount_nameLabel};
 
    selectedPayment_fixedPayment_nameLabel TextLabel { 
       text="Fixed pmt:" , 
       layoutData = new GridLayoutData { row = 5, column = 1} };
 
    selectedPayment_fixedPayment_checkBox DojoCheckBox { 
       layoutData = new GridLayoutData { row = 5, column = 2} };
 
    selectedPayment_fixedPayment_controller Controller { 
       @MVC {model = selectedPayment.fixedPayment, 
             view = selectedPayment_fixedPayment_checkBox}, 
       validStateSetter = handleValidStateChange_selectedPayment};
 
    selectedPayment_fixedPayment_formField FormField { 
       controller = selectedPayment_fixedPayment_controller, 
       nameLabel = selectedPayment_fixedPayment_nameLabel};
 
    selectedPayment_dueDate_nameLabel TextLabel { 
       text="Due date:", 
       layoutData = new GridLayoutData { row = 6, column = 1} };
 
    selectedPayment_dueDate_textBox DojoDateTextBox { 
       formatLength = DojoLib.DATEBOX_FORMAT_LONG, 
       value = selectedPayment.dueDate, 
       layoutData = new GridLayoutData { row = 6, column = 2} };
 
    selectedPayment_dueDate_controller Controller { 
       @MVC {model = selectedPayment.dueDate, 
             view = selectedPayment_dueDate_textBox}, 
       validStateSetter = handleValidStateChange_selectedPayment};
 
    selectedPayment_dueDate_formField FormField { 
       controller = selectedPayment_dueDate_controller, 
       nameLabel = selectedPayment_dueDate_nameLabel};
 
    selectedPayment_payeeName_nameLabel TextLabel { 
       text="Payee:", 
       layoutData = new GridLayoutData { row = 7, column = 1} };
 
    selectedPayment_payeeName_field DojoTextField {
       layoutData = new GridLayoutData { row = 7, column = 2} };
    
    selectedPayment_payeeName_controller Controller { 
       @MVC {model = selectedPayment.payeeName, 
             view = selectedPayment_payeeName_field},
       validStateSetter = handleValidStateChange_selectedPayment};
 
    selectedPayment_payeeName_formField FormField { 
       controller = selectedPayment_payeeName_controller,
       nameLabel = selectedPayment_payeeName_nameLabel};
 
    selectedPayment_payeeAddress1_nameLabel TextLabel { 
       text="Address 1:" , 
       layoutData = new GridLayoutData { row = 8, column = 1} };
 
    selectedPayment_payeeAddress1_field DojoTextField {
       layoutData = new GridLayoutData { row = 8, column = 2} };
 
    selectedPayment_payeeAddress1_controller Controller { 
       @MVC {model = selectedPayment.payeeAddress1, 
             view = selectedPayment_payeeAddress1_field},
       validStateSetter = handleValidStateChange_selectedPayment};
 
    selectedPayment_payeeAddress1_formField FormField { 
       controller = selectedPayment_payeeAddress1_controller, 
       nameLabel = selectedPayment_payeeAddress1_nameLabel};
 
    selectedPayment_payeeAddress2_nameLabel TextLabel { 
       text="Address 2:" , 
       layoutData = new GridLayoutData { row = 9, column = 1} };
 
    selectedPayment_payeeAddress2_field DojoTextField {
       layoutData = new GridLayoutData { row = 9, column = 2} };
 
    selectedPayment_payeeAddress2_controller Controller { 
       @MVC {model = selectedPayment.payeeAddress2, 
             view = selectedPayment_payeeAddress2_field}, 
       validStateSetter = handleValidStateChange_selectedPayment};
 
    selectedPayment_payeeAddress2_formField FormField { 
       controller = selectedPayment_payeeAddress2_controller, 
       nameLabel = selectedPayment_payeeAddress2_nameLabel};
 
    selectedPayment_form FormManager {
       entries = [ selectedPayment_paymentId_formField,
                   selectedPayment_category_formField,
                   selectedPayment_description_formField,
                   selectedPayment_amount_formField,
                   selectedPayment_fixedPayment_formField,
                   selectedPayment_dueDate_formField,
                   selectedPayment_payeeName_formField,
                   selectedPayment_payeeAddress1_formField,
                   selectedPayment_payeeAddress2_formField ] };
 
    detailButtonLayout GridLayout{ 
       layoutData = new GridLayoutData{ row = 1, column = 2 }, 
       cellPadding = 4, rows = 1, columns = 2,
       children = [ saveButton, clearButton ] };
 
    clearButton DojoButton{ 
       layoutData = new GridLayoutData{ row = 1, column = 1 }, 
       text = "Clear", onClick ::= clearAllFields };
 
    saveButton DojoButton{ 
       layoutData = new GridLayoutData{ row = 1, column = 2 }, 
       text = "Save", onClick ::= selectedPayment_form_Submit };
 
    function start()
//     allPayments_ui.data =[
//            new paymentRec{category = 1, description = "test01", 
//                           amount = 100.00, payeeName = "Someone"},
//            new paymentRec{category = 2, description = "test02", amount = 200.00},
//            new paymentRec{category = 3, description = "test03", amount = 300.00}];
      	readFromTable();
    end
 
    function cellClicked(myGrid DataGrid in)
       selectedPayment = allPayments_ui.getSelection()[1] as paymentRec;
       selectedPayment_form.publish();
       selectedPayment_category_comboBox.value = 
          PaymentLib.getCategoryDesc(selectedPayment.category);
    end
 
    function readFromTable()
       call dbService.getAllPayments()         	
       	using dedicatedServiceBinding
       	returning to updateAll
               onException serviceExceptionHandler;
    end
 
    function updateAll(retResult paymentRec[] in)
       allPayments = retResult;
       allPayments_ui.data = allPayments as any[];
    end
 
    function addRow(event Event in)
       call dbService.addPayment(new paymentRec) 
       	using dedicatedServiceBinding
       	returning to recordAdded
          	onException serviceExceptionHandler;
    end
 
    function recordAdded()
       readFromTable();
    end
 
    function deleteRow(event Event in)
 
       for(i INT from 1 to allPayments.getSize())
          if(allPayments[i].paymentID == selectedPayment.paymentID)
             allPayments.removeElement(i);
             exit for;
          end
       end
 
       call dbService.deletePayment(selectedPayment) 
       	using dedicatedServiceBinding
          	returning to recordRevised
          	onException serviceExceptionHandler;
    end
 
    function recordRevised()
       allPayments_ui.data = allPayments as any[];
    end   
 
    function sampleData(event Event in)
       call dbService.createDefaultTable() 
               using dedicatedServiceBinding
       	returning to updateAll
               onException serviceExceptionHandler;
    end
 
    function selectedPayment_form_Submit(event Event in)
       selectedPayment_category_comboBox.value = 
          PaymentLib.getCategoryNum(selectedPayment_category_comboBox.value);
 
       if (selectedPayment_form.isValid())
          selectedPayment_form.commit();
          selectedPayment_category_comboBox.value = 
             PaymentLib.getCategoryDesc(selectedPayment_category_comboBox.value);
 
          // update allPayments with new version of selectedPayment
          for(i INT from 1 to allPayments.getSize())
 
             if(allPayments[i].paymentID == selectedPayment.paymentID)
                allPayments[i] = selectedPayment;
                exit for;
             end
          end
 
          call dbService.editPayment(selectedPayment)
          	using dedicatedServiceBinding
             	returning to recordRevised
             	onException serviceExceptionHandler;
       end
    end
 
    function selectedPayment_form_Publish(event Event in)
       selectedPayment_form.publish();
       selectedPayment_form_Validate();
    end
 
    function selectedPayment_form_Validate()
       selectedPayment_form.isValid();
    end
 
    function handleValidStateChange_selectedPayment(view Widget in, valid boolean in)
 
       for (n int from selectedPayment_form.entries.getSize() to 1 decrement by 1)
          entry FormField = selectedPayment_form.entries[n];
 
          if(entry.controller.view == view)
 
             if(valid)
                // TODO: handle valid value
             else
                msg String? = entry.controller.getErrorMessage();
                // TODO: handle invalid value
             end
          end
       end
    end
 
    function clearAllFields(event Event in)
       saveID INT = selectedPayment.paymentID;  // retain the key
       selectedPayment = new PaymentRec{};
       selectedPayment.paymentID = saveID;
       selectedPayment_form.publish();
    end
    
    function formatCategory(class string inOut, value string inOut, rowData any in)
       value = PaymentLib.getCategoryDesc(value as INT);
    end
    
    function serviceExceptionHandler(ex anyException)
         try
             throw ex;
         onException(serviceEx ServiceInvocationException)
             log("Failure:  service invocation, exception = " +
                             serviceEx.message);
             log("Detail 1:  " + serviceEx.detail1);
             log("Detail 2:  " + serviceEx.detail2);
             log("Detail 3:  " + serviceEx.detail3);
         onException(anyex anyException)
             log("Failure:  service invocation, exception = " + anyex.message);
         end
         throw ex;
    end
 
    logActive boolean = true;
 
    function log(text string in)
         if(logActive)
             sysLib.writeStderr(text);
         end
    end
    
 end

Related tasks

Lesson 9: Complete the code that supports the user interface