Пример #1
0
        /// <summary>
        /// 根据模板Id 和 doc 获得标题和内容
        /// </summary>
        public static bool ResolverTemplate(string channel, string templateId, Dictionary <string, object> param, out string title, out string content, out string category)
        {
            title = content = category = string.Empty;

            DictSchema schema = (from q in DictSchema.CreateContext(true)
                                 where q.Type == "msg" && q.Name == "template" && q.Title == templateId
                                 select q).FirstOrDefault();

            if (schema == null)
            {
                LogManager.GetLogger <NoticeFactory>().ErrorFormat("指定的模板:{0} 不存在。", templateId);
                return(false);
            }

            category = schema.Category;

            ITemplateEngine te = ServiceLocator.Instance.Resolve <ITemplateEngine>();

            using (StringWriter sw = new StringWriter())
            {
                string template = schema[channel + "_title"];
                if (string.IsNullOrEmpty(template))
                {
                    template = schema["default_title"];
                }

                if (string.IsNullOrWhiteSpace(template))
                {
                    title = string.Empty;
                }
                else
                {
                    te.Process(param, string.Empty, sw, template);

                    title = sw.GetStringBuilder().ToString();
                }
            }

            using (StringWriter sw = new StringWriter())
            {
                string template = schema[channel + "_content"];
                if (string.IsNullOrEmpty(template))
                {
                    template = schema["default_content"];
                }

                if (string.IsNullOrWhiteSpace(template))
                {
                    content = string.Empty;
                }
                else
                {
                    te.Process(param, string.Empty, sw, template);

                    content = sw.GetStringBuilder().ToString();
                }
            }

            return(true);
        }
Пример #2
0
        private void Refresh()
        {
            lock (_synclock)
            {
                if (HttpContext.Current.Cache[kCACHE_KEY] == null)
                {
                    qc_dict.Clear();

                    List <string> filenames = new List <string>();

                    foreach (IArea site in ServiceLocator.Instance.Resolve <IHost>().AllAreas)
                    {
                        string        dir;
                        string        filename;
                        List <string> files = new List <string>();

                        if (site.AreaKey == AreaConfig.Instance.AreaKey)// default site
                        {
                            dir = ServerUtil.MapPath("~/App_Data");
                        }
                        else
                        {
                            dir = ServerUtil.MapPath(site.VirtualPath);
                        }

                        filename = Path.Combine(dir, "query.config");

                        if (!Directory.Exists(dir))
                        {
                            continue;
                        }

                        if (File.Exists(filename))
                        {
                            files.Add(filename);
                        }

                        // add filename like "query.post.config"
                        files.AddRange(Directory.GetFiles(dir, "query.*.config", SearchOption.TopDirectoryOnly));

                        filenames.AddRange(files);

                        // prase xml
                        foreach (var item in files)
                        {
                            _logger.Debug("begin parse query file: {0}.", item);

                            XmlDocument doc = new XmlDocument();
                            doc.Load(item);

                            foreach (XmlNode node in doc.DocumentElement.SelectNodes("//query"))
                            {
                                string id = XmlUtil.GetStringAttribute(node, "id", string.Empty);
                                if (string.IsNullOrEmpty(id))
                                {
                                    continue;
                                }

                                id = string.Format(FORMAT, site.AreaKey, id.ToLower());

                                _logger.Debug("RESULT: query id:{0}", id);

                                Qc qc = new Qc()
                                {
                                    Id    = id,
                                    Field = XmlUtil.GetStringAttribute(node, "field", string.Empty),
                                    AllowedOrderbyColumns = XmlUtil.GetStringAttribute(node, "allowedOrderbyColumns", string.Empty),
                                    Orderby  = XmlUtil.GetStringAttribute(node, "orderby", string.Empty),
                                    Where    = node.InnerText.Trim(),
                                    PageSize = XmlUtil.GetIntAttribute(node, "pageSize", -1)
                                };

                                if (string.IsNullOrEmpty(qc.Field))
                                {
                                    // 如果field是设置在单独的节点
                                    XmlNode n = node.SelectSingleNode("field");
                                    if (n != null)
                                    {
                                        qc.Field = n.InnerText.Trim();

                                        n = node.SelectSingleNode("where");
                                        if (n != null)
                                        {
                                            qc.Where = n.InnerText.Trim();
                                        }
                                    }
                                }

                                qc_dict[id] = qc;

                                foreach (XmlAttribute attr in node.Attributes)
                                {
                                    qc_dict[id][attr.Name] = attr.Value;
                                }
                            }

                            _logger.Debug("end parse query file: {0}.", item);
                        }
                    }

                    // load from database
                    foreach (var item in (from q in DictSchema.CreateContext()
                                          where q.Type == "dynamic_query" && q.IsValid == true
                                          select q).ToList())
                    {
                        if (string.IsNullOrEmpty(item.Name))
                        {
                            continue;
                        }

                        string id = string.Format(FORMAT,
                                                  string.IsNullOrEmpty(item.ParentId) ? "default" : item.ParentId,
                                                  item.Name.ToLowerInvariant());

                        var qc = qc_dict[id] = new Qc()
                        {
                            Id    = id,
                            Field = item.Prop1,
                            AllowedOrderbyColumns = item.Prop2,
                            Orderby  = item.Prop3,
                            Where    = item.Prop4,
                            PageSize = item.Prop5.ToInt(-1)
                        };

                        foreach (string key in item.ExtAttrs.Keys)
                        {
                            qc[key] = item[key];
                        }
                    }

                    CacheDependency fileDependency = new CacheDependency(filenames.ToArray());
                    HttpContext.Current.Cache.Insert(kCACHE_KEY, "dummyValue", fileDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                }
            }
        }