@@ -538,78 +538,85 @@ 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
561+ }
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" )
560569 }
561- elseif (sWidget .type '= " pivot" )
562- {
563- set tWidgets .type = " pivot"
564- set tWidgets .subtype = sWidget .type
565- }
566570
567- set count = 0 // indicates wether or not the same name as key exists
571+ set tWidgets = ##class (%DeepSee.Dashboard.Widget ).%New ()
572+ do ..UpdateWidget (tWidgets , sWidget )
573+
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
573- return $$$ERROR()
581+ set dWidgets = tDash .widgets .GetAt (i )
582+
583+ // Check if we trying to change widget name with exists one
584+ if ((dWidgets .name '= key ) && (dWidgets .name = sWidget .name )) {
585+ return $$$ERROR($$$GeneralError," Widget " _ sWidget .name _ " already exists" )
574586 }
575- }
576587
577-
578- if (($LENGTH (key ) = 0 ) & (count = 0 )) // if key is empty and count = 1 widget is not insert
579- {
580- $$$Insert(tDash .widgets , tWidgets )
581- do tDash .%Save ()
582-
583- }elseif ($LENGTH (key ) > 0 )
584- {
585-
586- for i =1 :1 :tDash .widgets .Count ()
588+ if (dWidgets .name = key )
587589 {
588- if (tDash .widgets .GetAt (i ).name = key )
589- {
590- set tDash .widgets .GetAt (i ).name = sWidget .name
591- set tDash .widgets .GetAt (i ).title = sWidget .title
592- set tDash .widgets .GetAt (i ).dataSource = sWidget .dataSource
593-
594- if (sWidget .type = " pivot" )
595- {
596- set tDash .widgets .GetAt (i ).type = " pivot"
597- set tDash .widgets .GetAt (i ).subtype = " pivot"
598- }
599- elseif (sWidget .type '= " pivot" )
600- {
601- set tDash .widgets .GetAt (i ).type = " pivot"
602- set tDash .widgets .GetAt (i ).subtype = sWidget .type
603- }
604- do tDash .%Save ()
605-
606- }
590+ set changed = dWidgets
607591 }
608592 }
593+ if (changed '= " " ) {
594+ do ..UpdateWidget (changed , sWidget )
595+ do tDash .%Save ()
596+ }
597+ }
598+
599+ return st
600+ }
609601
602+ /// the collection of necessary parameters for building widget
603+ ClassMethod UpdateWidget (widgetToUpdate As %DeepSee .Dashboard .Widget , data As %ZEN .proxyObject )
604+ {
605+ set widgetToUpdate .name = data .name
606+ set widgetToUpdate .title = data .title
607+ set widgetToUpdate .dataSource = data .dataSource
608+ set widgetToUpdate .dataLink = data .dataLink
610609
611- }
612- return st
610+ if (data .type = " pivot" )
611+ {
612+ set widgetToUpdate .type = " pivot"
613+ set widgetToUpdate .subtype = " pivot"
614+ }
615+ elseif (data .type '= " pivot" )
616+ {
617+ set widgetToUpdate .type = " pivot"
618+ set widgetToUpdate .subtype = data .type
619+ }
613620}
614621
615622}
0 commit comments