Exemplo n.º 1
0
        public List <SavedSearch> GetSavedSearch(string itemTypeName)
        {
            List <SavedSearch> resultList;

            if (!cachedSavedSearches.TryGetValue(itemTypeName, out resultList))
            {
                resultList = new List <SavedSearch>();
                dynamic savedSearches = innovatorInstance.newItem();
                savedSearches.loadAML(string.Format(@"
				<AML>
					<Item type=""SavedSearch"" action=""get"">
						<itname>{0}</itname>
						<auto_saved>0</auto_saved>
					</Item>
				</AML>"                , itemTypeName));
                savedSearches = savedSearches.apply();

                var searchesCount = savedSearches.getItemCount();
                for (int i = 0; i < searchesCount; i++)
                {
                    var search = new SavedSearch();

                    var savedSearch = savedSearches.getItemByIndex(i);
                    search.SearchId   = savedSearch.getID();
                    search.SearchName = savedSearch.getProperty("label");
                    search.ItemName   = itemTypeName;

                    string criteria = savedSearch.getProperty("criteria");
                    if (!string.IsNullOrEmpty(criteria))
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(criteria);
                        var properties = doc.FirstChild.ChildNodes;
                        foreach (XmlNode prop in properties)
                        {
                            var propertyInfo = new PropertyInfo();
                            propertyInfo.PropertyName  = prop.Name;
                            propertyInfo.PropertyValue = prop.InnerText;
                            search.SavedSearchProperties.Add(propertyInfo);
                        }
                    }

                    resultList.Add(search);
                }
                cachedSavedSearches.Add(itemTypeName, resultList);
            }

            return(resultList);
        }
Exemplo n.º 2
0
        public List <ItemSearchResult> RunSearch(string itemType, SavedSearch search)
        {
            var itemForSearch = innovatorInstance.newItem(itemType, "get");

            foreach (var prop in search.SavedSearchProperties)
            {
                if (!string.IsNullOrEmpty(prop.PropertyValue) && !string.Equals(prop.PropertyValue, "Indeterminate"))
                {
                    if (prop.DataType == PropertyDataType.Item)
                    {
                        itemForSearch.setProperty(prop.PropertyName, CreateItemSearchProperty(prop.DataSource, prop.PropertyValue));
                    }
                    else if (prop.DataType == PropertyDataType.Date)
                    {
                        DateTime date;
                        if (DateTime.TryParse(prop.PropertyValue, out date))
                        {
                            string value = date.ToString("yyyy-MM-dd");

                            itemForSearch.setProperty(prop.PropertyName, $"{value}T00:00:00 and {value}T23:59:59");
                            itemForSearch.setPropertyAttribute(prop.PropertyName, "condition", "between");
                        }
                    }
                    else
                    {
                        itemForSearch.setProperty(prop.PropertyName, prop.PropertyValue);
                        if (prop.PropertyValue.Contains("*") || prop.PropertyValue.Contains("%"))
                        {
                            itemForSearch.setPropertyAttribute(prop.PropertyName, "condition", "like");
                        }
                    }
                }
            }

            itemForSearch.setAttribute("page", search.Page.ToString());
            if (!string.IsNullOrEmpty(search.PageSize))
            {
                itemForSearch.setAttribute("pagesize", search.PageSize.ToString());
            }

            var codes = new Dictionary <string, string>()
            {
                { "&gt;", ">" }, { "&lt;", "<" },
            };

            string parsedString = itemForSearch.ToString();

            parsedString = codes.Aggregate(parsedString, (current, code) => current.Replace(code.Key, code.Value));

            itemForSearch.loadAML(parsedString);

            UpdateSearch(itemForSearch, itemType, search.SavedSearchProperties);

            itemForSearch = itemForSearch.apply();
            //itemForSearch.IsErrorItem(true);
            List <dynamic> itemForSearchList = new List <dynamic>();
            var            itemsCount        = itemForSearch.getItemCount();

            for (int i = 0; i < itemsCount; i++)
            {
                var currentItem = itemForSearch.getItemByIndex(i);
                itemForSearchList.Add(currentItem);
            }
            UpdateFoundedItems(itemForSearchList);
            var resultSearchList = new List <ItemSearchResult>();

            for (int i = 0; i < itemForSearchList.Count; i++)
            {
                var currentItem = itemForSearchList[i];
                ItemSearchResult searchResult = new ItemSearchResult();
                foreach (var item in search.SavedSearchProperties)
                {
                    var value = currentItem.getProperty(item.PropertyName);
                    ItemSearchPropertyInfo foundedPropertyInfo;
                    if (item.PropertyName == "locked_by_id")
                    {
                        var lockedPropertyInfo = new LockedByPropertyInfo();
                        lockedPropertyInfo.IsLocked     = !string.IsNullOrEmpty(value);
                        lockedPropertyInfo.IsLockedByMe = string.Equals(value, innovatorInstance.getUserID());
                        lockedPropertyInfo.PropertyName = item.PropertyName;
                        foundedPropertyInfo             = lockedPropertyInfo;
                    }
                    else if (item.DataType == PropertyDataType.ColorList)
                    {
                        var colorProp = item as ColorListPropertyInfo;
                        foundedPropertyInfo = new ColorListPropertyInfo()
                        {
                            PropertyName  = item.PropertyName,
                            PropertyValue = value,
                            DataType      = item.DataType,
                            ColorSource   = colorProp.ColorSource
                        };
                    }
                    else if (item.DataType == PropertyDataType.Item)
                    {
                        var label = currentItem.getPropertyAttribute(item.PropertyName, "keyed_name", string.Empty);

                        foundedPropertyInfo = new ItemSearchPropertyInfo()
                        {
                            PropertyName  = item.PropertyName,
                            PropertyValue = value,
                            Label         = label,
                            DataType      = item.DataType
                        };
                    }
                    else
                    {
                        foundedPropertyInfo = new ItemSearchPropertyInfo()
                        {
                            PropertyName  = item.PropertyName,
                            PropertyValue = value,
                            DataType      = item.DataType
                        };
                    }

                    searchResult.FoundedItems.Add(foundedPropertyInfo);
                }

                resultSearchList.Add(searchResult);
            }

            if (resultSearchList.Count > 0)
            {
                var firstItem = itemForSearch.getItemByIndex(0);
                var page      = firstItem.getAttribute("page", "1");
                var pageMax   = firstItem.getAttribute("pagemax", "1");

                int value;
                search.Page    = int.TryParse(page, out value) ? value : 1;
                search.PageMax = int.TryParse(pageMax, out value) ? value : 1;
            }
            else
            {
                search.Page    = 1;
                search.PageMax = 1;
            }

            return(resultSearchList);
        }