public static XisInteractionSpace CreateDetailOrRefManagerIS(EA.Package package, XisEntity entity,
            XisInteractionSpace previousIS, EA.Element useCase, bool isDetail, EA.Element be)
        {
            EA.Diagram diagram = XISMobileHelper.CreateDiagram(package, entity.Element.Name + "ManagerIS Diagram",
                "XIS-Mobile_Diagrams::InteractionSpaceViewModel");
            bool isFirstSubScreen = previousIS.IsMainScreen ? true : false;
            XisInteractionSpace managerIS = new XisInteractionSpace(repository, package, diagram, entity.Element.Name + "ManagerIS",
                "Manage " + entity.Element.Name, false, isFirstSubScreen);

            XisList list = new XisList(repository, diagram, managerIS, entity.Element.Name + "List");
            list.SetEntityName(entity.Element.Name);

            XisListItem item = new XisListItem(repository, diagram, list, list.Element.Name + "Item");
            if (entity.Element.Attributes.Count > 1)
            {
                EA.Attribute first = entity.Element.Attributes.GetAt(0);
                EA.Attribute second = entity.Element.Attributes.GetAt(1);
                XisLabel lbl1 = new XisLabel(repository, item, diagram, first.Name + "Lbl");
                lbl1.SetEntityAttributeName(entity.Element.Name + "." + first.Name);
                XisLabel lbl2 = new XisLabel(repository, item, diagram, second.Name + "Lbl");
                lbl2.SetEntityAttributeName(entity.Element.Name + "." + second.Name);
            }
            else if (entity.Element.Attributes.Count == 1)
            {
                EA.Attribute attr = entity.Element.Attributes.GetAt(0);
                XisLabel lbl = new XisLabel(repository, item, diagram, attr.Name + "Lbl");
                lbl.SetEntityAttributeName(entity.Element.Name + "." + attr.Name);
            }

            if ((isDetail && ContainsUpdateDetail(useCase))
                    || (!isDetail && ContainsUpdateReference(useCase)))
            {
                string actionName = "edit" + entity.Element.Name;
                item.SetOnTapAction(actionName);
            }
            if ((isDetail && ContainsReadDetail(useCase))
                || (!isDetail && ContainsReadReference(useCase)))
            {
                string actionName = "view" + entity.Element.Name;
                item.SetOnTapAction(actionName);
            }

            Dictionary<ActionType, XisMenuItem> detailModes = new Dictionary<ActionType, XisMenuItem>();

            #region Create Context Menu
            if (isDetail && (ContainsReadDetail(useCase) || ContainsUpdateDetail(useCase) || ContainsDeleteDetail(useCase))
                || !isDetail && (ContainsReadReference(useCase) || ContainsUpdateReference(useCase) || ContainsDeleteReference(useCase)))
            {
                XisMenu context = new XisMenu(repository, diagram, package, list.Element.Name + "ContextMenu", MenuType.ContextMenu);

                if (isDetail && ContainsReadDetail(useCase)
                    || !isDetail && ContainsReadReference(useCase))
                {
                    context.SetEntityName(entity.Element.Name);
                    string actionName = "view" + entity.Element.Name;
                    XisMenuItem contextItem = new XisMenuItem(repository, diagram, context,
                        "View" + entity.Element.Name + "Item", actionName);
                    contextItem.SetValue("View " + entity.Element.Name);
                    detailModes.Add(ActionType.Read, contextItem);
                }

                if (isDetail && ContainsUpdateDetail(useCase)
                    || !isDetail && ContainsUpdateReference(useCase))
                {
                    context.SetEntityName(entity.Element.Name);
                    string actionName = "edit" + entity.Element.Name;
                    XisMenuItem contextItem = new XisMenuItem(repository, diagram, context,
                        "Edit" + entity.Element.Name + "Item", actionName);
                    contextItem.SetValue("Edit " + entity.Element.Name);
                    detailModes.Add(ActionType.Update, contextItem);
                }

                if (isDetail && ContainsUpdateDetail(useCase)
                    || !isDetail && ContainsUpdateReference(useCase))
                {
                    context.SetEntityName(entity.Element.Name);
                    string actionName = "delete" + entity.Element.Name;
                    XisMenuItem contextItem = new XisMenuItem(repository, diagram, context,
                        "Delete" + entity.Element.Name + "Item", actionName);
                    contextItem.SetValue("Delete " + entity.Element.Name);
                    XISMobileHelper.CreateXisAction(repository, contextItem.Element, actionName, ActionType.Delete);
                }
                managerIS.ContextMenu = context;
            }
            #endregion

            #region Create Options Menu
            XisMenu menu = new XisMenu(repository, diagram, managerIS, managerIS.Element.Name + "Menu", MenuType.OptionsMenu);

            if ((isDetail && (ContainsCreateDetail(useCase) || ContainsDeleteDetail(useCase)))
                || (!isDetail && (ContainsCreateReference(useCase) || ContainsDeleteReference(useCase))))
            {
                menu.SetEntityName(entity.Element.Name);
                string actionName = "create" + entity.Element.Name;
                XisMenuItem menuItem = new XisMenuItem(repository, diagram, menu,
                    "Create" + entity.Element.Name + "Item", actionName);
                menuItem.SetValue("Create " + entity.Element.Name);
                detailModes.Add(ActionType.Create, menuItem);
            }

            if (isDetail && (ContainsDeleteDetail(useCase))
                || !isDetail && (ContainsDeleteReference(useCase)))
            {
                menu.SetEntityName(entity.Element.Name);
                string actionName = "deleteAll" + entity.Element.Name + "s";
                XisMenuItem menuItem = new XisMenuItem(repository, diagram, menu,
                    "DeleteAll" + entity.Element.Name + "Item", actionName);
                menuItem.SetValue("Delete all " + entity.Element.Name + "s");
                XISMobileHelper.CreateXisAction(repository, menuItem.Element, actionName, ActionType.DeleteAll);
            }

            string actionBack = "back" + entity.Element.Name;
            XisMenuItem backMenuItem = new XisMenuItem(repository, diagram, menu,
                "Back" + entity.Element.Name + "Item", actionBack);
            backMenuItem.SetValue("Back");
            XISMobileHelper.CreateXisAction(repository, backMenuItem.Element, actionBack, ActionType.Cancel);
            CreateXisInteractionSpaceAssociation(actionBack, managerIS, previousIS);

            managerIS.Menu = menu;
            #endregion

            if (detailModes.Count > 0 || !string.IsNullOrEmpty(item.GetOnTapAction()))
            {
                XisInteractionSpace detailIS = CreateDetailOrRefEditorIS(package, entity, managerIS, useCase, isDetail, be);
                foreach (ActionType key in detailModes.Keys)
                {
                    XisMenuItem mItem = detailModes[key];
                    XISMobileHelper.CreateXisAction(repository, mItem.Element, mItem.GetOnTapAction(),
                        key, detailIS.Element.Name);
                    CreateXisInteractionSpaceAssociation(mItem.GetOnTapAction(), managerIS, detailIS);
                }

                if (!string.IsNullOrEmpty(item.GetOnTapAction()))
                {
                    XISMobileHelper.CreateXisAction(repository, item.Element, item.GetOnTapAction(),
                        ActionType.Update, detailIS.Element.Name);
                    CreateXisInteractionSpaceAssociation(item.GetOnTapAction(), managerIS, detailIS);
                }
            }

            ComputePositions(managerIS, diagram);

            if (managerIS.ContextMenu != null)
            {
                EA.DiagramObject obj = managerIS.GetDiagramObject(diagram);
                int center = (obj.top + obj.bottom) / -2;
                managerIS.ContextMenu.SetPosition(diagram, obj.right + 50, obj.right + 330, -obj.top, -obj.top + 70);
                ComputePositions(managerIS.ContextMenu, diagram);

                // Create XisIS-MenuAssociation
                EA.DiagramObject sourceObj = item.GetDiagramObject(diagram);
                EA.Connector c = item.Element.Connectors.AddNew("", "Association");
                c.ClientID = item.Element.ElementID;
                c.SupplierID = managerIS.ContextMenu.Element.ElementID;
                c.Direction = "Source -> Destination";
                c.Stereotype = "XisIS-MenuAssociation";
                c.Update();
                item.Element.Update();
                item.Element.Connectors.Refresh();
            }

            // Associate BE
            AssociateBEtoIS(diagram, managerIS, be);

            return managerIS;
        }
        public static void ProcessManagerUseCase(EA.Package package, XisEntity master,
            EA.Element useCase, EA.Element be, bool isStartingUC, List<EA.Element> useCases = null, String patternType = null)
        {
            // Create IS Diagram
            EA.Diagram listDiagram = XISMobileHelper.CreateDiagram(package, master.Element.Name + "ListIS Diagram",
                "XIS-Mobile_Diagrams::InteractionSpaceViewModel");
            XisInteractionSpace listIS = null;

            if (isStartingUC && patternType != null)
            {
                listIS = new XisInteractionSpace(repository, package, listDiagram,
                    master.Element.Name + "ListIS", "Manage " + master.Element.Name + "s");
            }
            else
            {
                listIS = new XisInteractionSpace(repository, package, listDiagram,
                    master.Element.Name + "ListIS", "Manage " + master.Element.Name + "s", isStartingUC, !isStartingUC);

                if (isStartingUC && patternType == null)
                {
                    homeIS = listIS;
                }
            }

            // List Creation
            XisList list = new XisList(repository, listDiagram, listIS, master.Element.Name + "List");
            list.SetEntityName(master.Element.Name);

            //if (ContainsSearch(operations))
            //{
            //    string searchBy = "";
            //    string tv = null;

            //    foreach (EA.Attribute attr in master.Element.Attributes)
            //    {
            //        tv = GetAttributeTag(attr.TaggedValues, "isKey").Value;
            //        if (!string.IsNullOrEmpty(tv) && tv.ToLower() == "true")
            //        {
            //            searchBy += master.Element.Name + "." + attr.Name + ";";
            //        }
            //    }

            //    if (string.IsNullOrEmpty(searchBy))
            //    {
            //        list.SetSearchBy(master.Element.Name + ".Id");
            //    }
            //    else
            //    {
            //        list.SetSearchBy(searchBy.Substring(0, searchBy.Length - 1));
            //    }
            //}

            XisListItem item = new XisListItem(repository, listDiagram, list, list.Element.Name + "Item");

            if (ContainsUpdateMaster(useCase))
            {
                string actionName = "edit" + master.Element.Name;
                item.SetOnTapAction(actionName);
            }
            else if (ContainsReadMaster(useCase))
            {
                string actionName = "view" + master.Element.Name;
                item.SetOnTapAction(actionName);
            }

            if (master.Element.Attributes.Count > 1)
            {
                EA.Attribute first = master.Element.Attributes.GetAt(0);
                EA.Attribute second = master.Element.Attributes.GetAt(1);
                XisLabel lbl1 = new XisLabel(repository, item, listDiagram, first.Name + "Lbl");
                lbl1.SetEntityAttributeName(master.Element.Name + "." + first.Name);
                XisLabel lbl2 = new XisLabel(repository, item, listDiagram, second.Name + "Lbl");
                lbl2.SetEntityAttributeName(master.Element.Name + "." + second.Name);
            }
            else if (master.Element.Attributes.Count == 1)
            {
                EA.Attribute attr = master.Element.Attributes.GetAt(0);
                item.SetValueFromExpression(master.Element.Name + "." + attr.Name);
            }

            // Read, Update, Create
            Dictionary<ActionType, XisMenuItem> detailModes = new Dictionary<ActionType, XisMenuItem>(3);

            #region Create Context Menu
            if (ContainsReadMaster(useCase) || ContainsUpdateMaster(useCase) || ContainsDeleteMaster(useCase))
            {
                XisMenu context = new XisMenu(repository, listDiagram, package, list.Element.Name + "ContextMenu", MenuType.ContextMenu);

                if (ContainsReadMaster(useCase))
                {
                    context.SetEntityName(master.Element.Name);
                    string actionName = "view" + master.Element.Name;
                    XisMenuItem contextItem = new XisMenuItem(repository, listDiagram, context,
                        "View" + master.Element.Name + "Item", actionName);
                    contextItem.SetValue("View " + master.Element.Name);
                    detailModes.Add(ActionType.Read, contextItem);
                }

                if (ContainsUpdateMaster(useCase))
                {
                    context.SetEntityName(master.Element.Name);
                    string actionName = "edit" + master.Element.Name;
                    XisMenuItem contextItem = new XisMenuItem(repository, listDiagram, context,
                        "Edit" + master.Element.Name + "Item", actionName);
                    contextItem.SetValue("Edit " + master.Element.Name);
                    detailModes.Add(ActionType.Update, contextItem);
                }

                if (ContainsDeleteMaster(useCase))
                {
                    context.SetEntityName(master.Element.Name);
                    string actionName = "delete" + master.Element.Name;
                    XisMenuItem contextItem = new XisMenuItem(repository, listDiagram, context,
                        "Delete" + master.Element.Name + "Item", actionName);
                    contextItem.SetValue("Delete " + master.Element.Name);
                    XISMobileHelper.CreateXisAction(repository, contextItem.Element, actionName, ActionType.Delete);
                }
                listIS.ContextMenu = context;
            }
            #endregion

            // Navigation between home UC and the others
            if (patternType != null)
            {
                AddToHomeISByPattern(useCase, listIS, patternType);
            }
            else if (isStartingUC)
            {
                if (useCases != null)
                {
                    AssociateFirstSubSpaces(listDiagram, useCases, listIS, be.ElementID, master.Element.Name);
                }
            }

            #region Create Options Menu
            if (ContainsCreateMaster(useCase) || ContainsDeleteMaster(useCase))
            {
                XisMenu menu = new XisMenu(repository, listDiagram, listIS, listIS.Element.Name + "Menu", MenuType.OptionsMenu);

                if (ContainsCreateMaster(useCase))
                {
                    menu.SetEntityName(master.Element.Name);
                    string actionName = "create" + master.Element.Name;
                    XisMenuItem menuItem = new XisMenuItem(repository, listDiagram, menu,
                        "Create" + master.Element.Name + "Item", actionName);
                    menuItem.SetValue("Create " + master.Element.Name);
                    detailModes.Add(ActionType.Create, menuItem);
                }

                if (ContainsDeleteMaster(useCase))
                {
                    menu.SetEntityName(master.Element.Name);
                    string actionName = "deleteAll" + master.Element.Name + "s";
                    XisMenuItem menuItem = new XisMenuItem(repository, listDiagram, menu,
                        "DeleteAll" + master.Element.Name + "Item", actionName);
                    menuItem.SetValue("Delete all " + master.Element.Name + "s");
                    XISMobileHelper.CreateXisAction(repository, menuItem.Element, actionName, ActionType.DeleteAll);
                }
                listIS.Menu = menu;
            }
            #endregion

            #region Check ServiceUC Extensions
            Dictionary<List<EA.Element>, bool> services = new Dictionary<List<EA.Element>, bool>();

            if (useCase.Connectors.Count > 0)
            {
                List<EA.Element> extends = new List<EA.Element>();
                Dictionary<EA.Element, bool> providers = new Dictionary<EA.Element, bool>();

                foreach (EA.Connector conn in useCase.Connectors)
                {
                    if (conn.Stereotype == "extend")
                    {
                        EA.Element el = repository.GetElementByID(conn.ClientID);
                        extends.Add(el);
                    }
                }

                foreach (EA.Element e in extends)
                {
                    bool hasBE = false;
                    EA.Element provider = null;

                    foreach (EA.Connector conn in e.Connectors)
                    {
                        if (conn.Stereotype == "XisServiceUC-BEAssociation")
                        {
                            hasBE = true;
                        }
                        else if (conn.Stereotype == "XisServiceUC-ProviderAssociation")
                        {
                            provider = repository.GetElementByID(conn.SupplierID);
                            providers.Add(provider, false);
                        }
                    }

                    if (provider != null && hasBE)
                    {
                        providers[provider] = true;
                    }
                }

                foreach (EA.Element p in providers.Keys)
                {
                    List<EA.Element> lst = new List<EA.Element>();

                    foreach (EA.Connector c in p.Connectors)
                    {
                        if (c.Stereotype == "XisProvider-ServiceRealization")
                        {
                            EA.Element el = repository.GetElementByID(c.SupplierID);
                            lst.Add(el);
                        }
                    }

                    if (lst.Count > 0)
                    {
                        services.Add(lst, providers[p]);
                    }
                }

                if (services.Count > 0)
                {
                    if (services.Values.Contains(true))
                    {
                        if (listIS.ContextMenu == null)
                        {
                            listIS.ContextMenu = new XisMenu(repository, listDiagram, package, list.Element.Name + "ContextMenu",
                                MenuType.ContextMenu);
                        }

                        foreach (List<EA.Element> lst in services.Keys)
                        {
                            if (services[lst])
                            {
                                foreach (EA.Element serv in lst)
                                {
                                    foreach (EA.Method method in serv.Methods)
                                    {
                                        if (method.Stereotype == "XisServiceMethod")
                                        {
                                            XisMenuItem menuItem = new XisMenuItem(repository, listDiagram, listIS.ContextMenu,
                                                method.Name, serv.Name + "." + method.Name);
                                            menuItem.SetValue(method.Name);
                                            XISMobileHelper.CreateXisAction(repository, menuItem.Element, menuItem.GetOnTapAction(),
                                                ActionType.WebService);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (services.Values.Contains(false))
                    {
                        if (listIS.Menu == null)
                        {
                            listIS.Menu = new XisMenu(repository, listDiagram, listIS, listIS.Element.Name + "Menu", MenuType.OptionsMenu);
                        }

                        foreach (List<EA.Element> lst in services.Keys)
                        {
                            if (!services[lst])
                            {
                                foreach (EA.Element serv in lst)
                                {
                                    foreach (EA.Method method in serv.Methods)
                                    {
                                        if (method.Stereotype == "XisServiceMethod")
                                        {
                                            XisMenuItem menuItem = new XisMenuItem(repository, listDiagram, listIS.Menu,
                                                method.Name, serv.Name + "." + method.Name);
                                            menuItem.SetValue(method.Name);
                                            XISMobileHelper.CreateXisAction(repository, menuItem.Element, menuItem.GetOnTapAction(),
                                                ActionType.WebService);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion

            if (detailModes.Count > 0 || !string.IsNullOrEmpty(item.GetOnTapAction()))
            {
                XisInteractionSpace detailIS = CreateMasterEditorIS(package, master, listIS, useCase, be, services);

                foreach (ActionType key in detailModes.Keys)
                {
                    XisMenuItem mItem = detailModes[key];
                    XISMobileHelper.CreateXisAction(repository, mItem.Element, mItem.GetOnTapAction(),
                        key, detailIS.Element.Name);
                    CreateXisInteractionSpaceAssociation(mItem.GetOnTapAction(), listIS, detailIS);
                }

                if (!string.IsNullOrEmpty(item.GetOnTapAction()))
                {
                    XISMobileHelper.CreateXisAction(repository, item.Element, item.GetOnTapAction(),
                        ActionType.Update, detailIS.Element.Name);
                    CreateXisInteractionSpaceAssociation(item.GetOnTapAction(), listIS, detailIS);
                }
            }

            if (homeIS != listIS)
            {
                if (listIS.Menu == null)
                {
                    listIS.Menu = new XisMenu(repository, listDiagram, listIS, listIS.Element.Name + "Menu", MenuType.OptionsMenu);
                }

                string actionBack = "backTo" + homeIS.Element.Name;
                XisMenuItem backMenuItem = new XisMenuItem(repository, listDiagram, listIS.Menu,
                    "BackTo" + homeIS.Element.Name + "Item", actionBack);
                backMenuItem.SetValue("Back");
                XISMobileHelper.CreateXisAction(repository, backMenuItem.Element, actionBack, ActionType.Cancel);
                CreateXisInteractionSpaceAssociation(actionBack, listIS, homeIS);
            }

            ComputePositions(listIS, listDiagram);

            if (listIS.ContextMenu != null)
            {
                EA.DiagramObject obj = listIS.GetDiagramObject(listDiagram);
                int center = (obj.top + obj.bottom) / -2;
                listIS.ContextMenu.SetPosition(listDiagram, obj.right + 50, obj.right + 330, -obj.top, -obj.top + 70);
                ComputePositions(listIS.ContextMenu, listDiagram);

                // Create XisIS-MenuAssociation
                EA.DiagramObject sourceObj = item.GetDiagramObject(listDiagram);
                EA.Connector c = item.Element.Connectors.AddNew("", "Association");
                c.ClientID = item.Element.ElementID;
                c.SupplierID = listIS.ContextMenu.Element.ElementID;
                c.Direction = "Source -> Destination";
                c.Stereotype = "XisIS-MenuAssociation";
                c.Update();
                item.Element.Update();
                item.Element.Connectors.Refresh();
            }

            // Associate BE
            AssociateBEtoIS(listDiagram, listIS, be);

            if (!isStartingUC)
            {
                // TODO: Link subspaces
                //CreateXisInteractionSpaceAssociation(repository, "goTo" + listIS.Element.Name, homeIS, listIS);
            }
        }