@@ -538,77 +538,98 @@ ClassMethod CreateAddonClass(Class As %Dictionary.CacheClassname) As %Status
538538 quit classObj .%Save ()
539539}
540540
541+ /// Add new widgets and edit existing ones.
541542ClassMethod AddWidget (sWidget As %String , sDashboard As %String , key As %String ) As %Status
542543{
543544 set st = $$$OK
544- set dExist =##class (%DeepSee.Dashboard.Utils ).%DashboardExists (sDashboard ) // check if dashboard exist
545- If (dExist = 1 )
545+ set dExist =##class (%DeepSee.Dashboard.Utils ).%DashboardExists (sDashboard )
546+
547+ // Check if dashboard exists
548+ If (dExist '= 1 )
546549 {
547- Set tDash =##class (%DeepSee.Dashboard.Utils ).%OpenDashboard (sDashboard , .st )
548- set tWidgets = ##class (%DeepSee.Dashboard.Widget ).%New ()
549- set tWidgets .name = sWidget .name
550- quit :sWidget .name =" "
551- set tWidgets .title = sWidget .title
552- set tWidgets .dataSource = sWidget .dataSource
553-
554- // logic to work around weird line in ##classMDX2JSON.Dashboard.WidgetToProxyObject:
555- // "set obj.type = Widget.subtype"
556- if (sWidget .type = " pivot" )
557- {
558- set tWidgets .type = " pivot"
559- set tWidgets .subtype = " pivot"
550+ Quit $$$ERROR($$$GeneralError," Dashboard " _ sDashboard _ " does not exists" )
551+ }
552+ Set tDash =##class (%DeepSee.Dashboard.Utils ).%OpenDashboard (sDashboard , .st )
553+
554+ // Check if widget with name is exists
555+ set isExists = 0
556+ for i =1 :1 :tDash .widgets .Count ()
557+ {
558+ if (tDash .widgets .GetAt (i ).name = sWidget .name )
559+ {
560+ set isExists = 1
560561 }
561- elseif (sWidget .type '= " pivot" )
562- {
563- set tWidgets .type = " pivot"
564- set tWidgets .subtype = sWidget .type
565- }
562+ }
563+
564+ // Adding new widget
565+ if ($LENGTH (key ) = 0 ) { // if key received empty then we expect new widget if not then else logic is working
566+ // Check if we trying to create widget with exists name
567+ if (isExists = 1 ) {
568+ return $$$ERROR($$$GeneralError," Widget " _ sWidget .name _ " already exists" )
569+ }
570+
571+ set tWidgets = ##class (%DeepSee.Dashboard.Widget ).%New ()
572+ do ..UpdateWidget (tWidgets , sWidget )
566573
567- set count = 0 // indicates wether or not the same name as key exists
574+ $$$Insert(tDash .widgets , tWidgets )
575+ do tDash .%Save ()
576+
577+ } else {
578+ // Edit exists widget
568579 for i =1 :1 :tDash .widgets .Count ()
569580 {
570- if (tDash .widgets .GetAt (i ).name = sWidget .name )
571- {
572- set count = 1
581+
582+ set dWidgets = tDash .widgets .GetAt (i )
583+
584+ // Check if we trying to change widget name with exists one
585+ if ((dWidgets .name '= key ) && (dWidgets .name = sWidget .name )) {
586+ return $$$ERROR($$$GeneralError," Widget " _ sWidget .name _ " already exists" )
587+
573588 }
574- }
575589
576-
577- if (($LENGTH (key ) = 0 ) & (count = 0 )) // if key is empty and count = 1 widget is not insert
578- {
579- $$$Insert(tDash .widgets , tWidgets )
580- do tDash .%Save ()
581-
582- }elseif ($LENGTH (key ) > 0 )
583- {
584-
585- for i =1 :1 :tDash .widgets .Count ()
590+ if (dWidgets .name = key )
586591 {
587- if (tDash .widgets .GetAt (i ).name = key )
588- {
589- set tDash .widgets .GetAt (i ).name = sWidget .name
590- set tDash .widgets .GetAt (i ).title = sWidget .title
591- set tDash .widgets .GetAt (i ).dataSource = sWidget .dataSource
592-
593- if (sWidget .type = " pivot" )
594- {
595- set tDash .widgets .GetAt (i ).type = " pivot"
596- set tDash .widgets .GetAt (i ).subtype = " pivot"
597- }
598- elseif (sWidget .type '= " pivot" )
599- {
600- set tDash .widgets .GetAt (i ).type = " pivot"
601- set tDash .widgets .GetAt (i ).subtype = sWidget .type
602- }
603- do tDash .%Save ()
604-
605- }
592+ set changed = dWidgets
606593 }
607594 }
595+ if (changed '= " " ) {
596+ do ..UpdateWidget (changed , sWidget )
597+ do tDash .%Save ()
598+ }
599+ }
600+
601+ return st
602+ }
608603
604+ /// the collection of necessary parameters for building widget
605+ ClassMethod UpdateWidget (widgetToUpdate As %DeepSee .Dashboard .Widget , data As %ZEN .proxyObject )
606+ {
607+ set widgetToUpdate .name = data .name
608+ set widgetToUpdate .title = data .title
609+ set widgetToUpdate .dataSource = data .dataSource
610+ set widgetToUpdate .dataLink = data .dataLink
611+
612+ if (data .displayInfo '= " " ) {
613+ set widgetToUpdate .top = data .displayInfo .top
614+ set widgetToUpdate .left = data .displayInfo .left
615+ set widgetToUpdate .width = data .displayInfo .width
616+ set widgetToUpdate .height = data .displayInfo .height
617+ set widgetToUpdate .homeColL = data .displayInfo .topCol
618+ set widgetToUpdate .homeRowL = data .displayInfo .leftRow
619+ set widgetToUpdate .colSpanL = data .displayInfo .colWidth
620+ set widgetToUpdate .rowSpanL = data .displayInfo .rowHeight
621+ }
609622
610- }
611- return st
623+ if (data .type = " pivot" )
624+ {
625+ set widgetToUpdate .type = " pivot"
626+ set widgetToUpdate .subtype = " pivot"
627+ }
628+ elseif (data .type '= " pivot" )
629+ {
630+ set widgetToUpdate .type = " pivot"
631+ set widgetToUpdate .subtype = data .type
632+ }
612633}
613634
614635}
0 commit comments