PageRenderTime 38ms CodeModel.GetById 28ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

/CMSModules/System/System_Deployment.aspx.cs

https://bitbucket.org/kudutest/kenticogit
C# | 244 lines | 186 code | 40 blank | 18 comment | 40 complexity | c38c48758e644e83ac85c137dd6270a8 MD5 | raw file
  1using System;
  2using System.Data;
  3using System.Text.RegularExpressions;
  4
  5using CMS.GlobalHelper;
  6using CMS.IO;
  7using CMS.PortalEngine;
  8using CMS.SettingsProvider;
  9using CMS.SiteProvider;
 10using CMS.UIControls;
 11
 12public partial class CMSModules_System_System_Deployment : SiteManagerPage
 13{
 14    private static Regex codeFileRegex = RegexHelper.GetRegex("CodeFile=\"([^\"]*)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);
 15    private static Regex inheritsRegex = RegexHelper.GetRegex("Inherits=\"([^\"]*)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);
 16
 17    protected void Page_Load(object sender, EventArgs e)
 18    {
 19        if (AzureHelper.IsRunningOnAzure)
 20        {
 21            lblInfo.Text = GetString("Deployment.AzureDisabled");
 22            btnSaveAll.Enabled = false;
 23        }
 24        
 25        lblInfo.Text = GetString("Deployment.SaveAllInfo");
 26        btnSaveAll.Text = GetString("Deployment.SaveAll");
 27        
 28    }
 29
 30
 31    protected void btnSaveAll_Click(object sender, EventArgs e)
 32    {
 33        try
 34        {
 35            // Save all the document transformations
 36            DataSet ds = DataClassInfoProvider.GetAllDataClass();
 37            if (!DataHelper.DataSourceIsEmpty(ds))
 38            {
 39                foreach (DataRow dr in ds.Tables[0].Rows)
 40                {
 41                    int classId = ValidationHelper.GetInteger(dr["ClassID"], 0);
 42                    string className = ValidationHelper.GetString(dr["ClassName"], "");
 43                    bool isDocumentType = ValidationHelper.GetBoolean(dr["ClassIsDocumentType"], false);
 44
 45                    if (isDocumentType)
 46                    {
 47                        ProcessTransformations(classId, className);
 48                    }
 49                }
 50            }
 51
 52            // Save all the custom table transformations
 53            ds = DataClassInfoProvider.GetCustomTableClasses(null, null, 0, "ClassID,ClassName,ClassIsCustomTable");
 54            if (!DataHelper.DataSourceIsEmpty(ds))
 55            {
 56                foreach (DataRow dr in ds.Tables[0].Rows)
 57                {
 58                    int classId = ValidationHelper.GetInteger(dr["ClassID"], 0);
 59                    string className = ValidationHelper.GetString(dr["ClassName"], "");
 60                    bool isCustomTable = ValidationHelper.GetBoolean(dr["ClassIsCustomTable"], false);
 61
 62                    if (isCustomTable)
 63                    {
 64                        ProcessTransformations(classId, className);
 65                    }
 66                }
 67            }
 68
 69            // Save all the layouts
 70            ds = LayoutInfoProvider.GetAllLayouts();
 71            if (!DataHelper.DataSourceIsEmpty(ds))
 72            {
 73                foreach (DataRow dr in ds.Tables[0].Rows)
 74                {
 75                    string layoutName = ValidationHelper.GetString(dr["LayoutCodeName"], "");
 76                    string layoutCode = ValidationHelper.GetString(dr["LayoutCode"], "");
 77
 78                    int checkedOutByUserId = ValidationHelper.GetInteger(dr["LayoutCheckedOutByUserID"], 0);
 79                    string checkedOutMachineName = ValidationHelper.GetString(dr["LayoutCheckedOutMachineName"], "");
 80
 81                    if ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower()))
 82                    {
 83                        string filename = LayoutInfoProvider.GetLayoutUrl(layoutName, null);
 84
 85                        // Prepare the code
 86                        string code = layoutCode;
 87                        code = LayoutInfoProvider.AddLayoutDirectives(code);
 88
 89                        SiteManagerFunctions.SaveCodeFile(filename, code);
 90                    }
 91                }
 92            }
 93
 94            // Save all the page template layouts
 95            ds = PageTemplateInfoProvider.GetAllTemplates();
 96            if (!DataHelper.DataSourceIsEmpty(ds))
 97            {
 98                foreach (DataRow dr in ds.Tables[0].Rows)
 99                {
100                    string templateName = ValidationHelper.GetString(dr["PageTemplateCodeName"], "");
101                    string templateLayout = ValidationHelper.GetString(dr["PageTemplateLayout"], "");
102                    bool isReusable = ValidationHelper.GetBoolean(dr["PageTemplateIsReusable"], false);
103
104                    int checkedOutByUserId = ValidationHelper.GetInteger(dr["PageTemplateLayoutCheckedOutByUserID"], 0);
105                    string checkedOutMachineName = ValidationHelper.GetString(dr["PageTemplateLayoutCheckedOutMachineName"], "");
106                    bool isPortalTemplate = ValidationHelper.GetBoolean(dr["PageTemplateIsPortal"], false);
107                    string pageTemplateType = ValidationHelper.GetString(dr["PageTemplateType"], "");
108                    bool isDashboard = pageTemplateType.Equals(PageTemplateInfoProvider.GetPageTemplateTypeCode(PageTemplateTypeEnum.Dashboard));
109
110                    if ((isPortalTemplate || isDashboard) && !String.IsNullOrEmpty(templateLayout) && ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower())))
111                    {
112                        string filename = null;
113                        if (isReusable)
114                        {
115                            filename = PageTemplateInfoProvider.GetLayoutUrl(templateName, null);
116                        }
117                        else
118                        {
119                            filename = PageTemplateInfoProvider.GetAdhocLayoutUrl(templateName, null);
120                        }
121
122                        // Prepare the code
123                        string code = templateLayout;
124                        code = LayoutInfoProvider.AddLayoutDirectives(code);
125
126                        SiteManagerFunctions.SaveCodeFile(filename, code);
127                    }
128                }
129            }
130
131            // Save all the web part layouts
132            ds = WebPartLayoutInfoProvider.GetWebPartLayouts(null, null);
133            if (!DataHelper.DataSourceIsEmpty(ds))
134            {
135                foreach (DataRow dr in ds.Tables[0].Rows)
136                {
137                    string webPartLayoutCodeName = ValidationHelper.GetString(dr["WebPartLayoutCodeName"], "");
138                    string webPartLayoutCode = ValidationHelper.GetString(dr["WebPartLayoutCode"], "");
139
140                    WebPartInfo wpi = WebPartInfoProvider.GetWebPartInfo(ValidationHelper.GetInteger(dr["WebPartLayoutWebPartID"], 0));
141
142                    if (wpi != null)
143                    {
144                        int checkedOutByUserId = ValidationHelper.GetInteger(dr["WebPartLayoutCheckedOutByUserID"], 0);
145                        string checkedOutMachineName = ValidationHelper.GetString(dr["WebPartLayoutCheckedOutMachineName"], "");
146
147                        if (!String.IsNullOrEmpty(webPartLayoutCode) && ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower())))
148                        {
149                            // Get layout filename
150                            string filename = WebPartLayoutInfoProvider.GetWebPartLayoutUrl(wpi.WebPartName, webPartLayoutCodeName, "");
151                            // Get path to the code file
152                            string codeFilePath = URLHelper.GetVirtualPath(filename) + ".cs";
153
154                            // Get path to the original code behind file
155                            string originalCodeFilePath = String.Empty;
156                            if (codeFileRegex.IsMatch(webPartLayoutCode, 0))
157                            {
158                                originalCodeFilePath = codeFileRegex.Match(webPartLayoutCode).Result("$1");
159                            }
160
161                            // Get original class name
162                            string originalClassName = String.Empty;
163                            if (inheritsRegex.IsMatch(webPartLayoutCode))
164                            {
165                                originalClassName = inheritsRegex.Match(webPartLayoutCode).Result("$1");
166                            }
167
168                            if (codeFileRegex.IsMatch(webPartLayoutCode))
169                            {
170                                webPartLayoutCode = codeFileRegex.Replace(webPartLayoutCode, "CodeFile=\"" + codeFilePath + "\"");
171                            }
172
173                            if (inheritsRegex.IsMatch(webPartLayoutCode))
174                            {
175                                webPartLayoutCode = inheritsRegex.Replace(webPartLayoutCode, "Inherits=\"$1_Web_Deployment\"");
176                            }
177
178                            // Read original codefile and change classname
179                            string codeFileCode = String.Empty;
180                            if (!String.IsNullOrEmpty(originalCodeFilePath) && FileHelper.FileExists(originalCodeFilePath))
181                            {
182                                codeFileCode = File.ReadAllText(Server.MapPath(originalCodeFilePath));
183                                codeFileCode = codeFileCode.Replace(originalClassName, originalClassName + "_Web_Deployment");
184
185                                // Save code file
186                                SiteManagerFunctions.SaveCodeFile(filename, webPartLayoutCode);
187
188                                // Save code behind file
189                                SiteManagerFunctions.SaveCodeFile(codeFilePath, codeFileCode);
190                            }
191                        }
192                    }
193                }
194            }
195
196            lblResult.Text = GetString("Deployment.ObjectsSaved");
197        }
198        catch (Exception ex)
199        {
200            CMS.EventLog.EventLogProvider ep = new CMS.EventLog.EventLogProvider();
201            ep.LogEvent("System deployment", "E", ex);
202
203            lblError.Visible = true;
204            lblError.Text = ex.Message;
205        }
206    }
207
208
209    private void ProcessTransformations(int classId, string className)
210    {
211        // Get the transformations
212        DataSet transformationsDS = TransformationInfoProvider.GetTransformations(classId);
213        if (!DataHelper.DataSourceIsEmpty(transformationsDS))
214        {
215            foreach (DataRow transformationRow in transformationsDS.Tables[0].Rows)
216            {
217                // Get the type
218                string type = ValidationHelper.GetString(transformationRow["TransformationType"], "ascx");
219                TransformationTypeEnum transformationType = TransformationInfoProvider.GetTransformationTypeEnum(type);
220
221                // Only export ASCX transformations
222                if (transformationType == TransformationTypeEnum.Ascx)
223                {
224                    string transformationName = ValidationHelper.GetString(transformationRow["TransformationName"], "");
225                    string transformationCode = ValidationHelper.GetString(transformationRow["TransformationCode"], "");
226
227                    int checkedOutByUserId = ValidationHelper.GetInteger(transformationRow["TransformationCheckedOutByUserID"], 0);
228                    string checkedOutMachineName = ValidationHelper.GetString(transformationRow["TransformationCheckedOutMachineName"], "");
229
230                    if ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower()))
231                    {
232                        string filename = TransformationInfoProvider.GetTransformationUrl(className + "." + transformationName, null, TransformationTypeEnum.Ascx);
233
234                        // Prepare the code
235                        string code = transformationCode;
236                        code = TransformationInfoProvider.AddTransformationDirectives(code, true);
237
238                        SiteManagerFunctions.SaveCodeFile(filename, code);
239                    }
240                }
241            }
242        }
243    }
244}