Ejemplo n.º 1
0
        public override bool Perform()
        {
            LogMessage("Activating Site Core Feature");

            SPSite.Features.Add(new Guid("e97da3cd-4c42-44cd-ba51-2bfbb2c397cb"), true);

            if (bIsPfe)
            {
                using (var workEngineAPI = new WorkEngineAPI())
                {
                    LogMessage("Installing PfE Data Synch Events");

                    //workEngineAPI.Execute("AddRemoveFeatureEvents", @"<AddRemoveFeatureEvents><Data><Feature Name=""PFEDataSync"" Operation=""ADD""/></Data></AddRemoveFeatureEvents>");
                    WorkEngineAPI.AddRemoveFeatureEvents(@"<AddRemoveFeatureEvents><Data><Feature Name=""PFEDataSync"" Operation=""ADD""/></Data></AddRemoveFeatureEvents>", SPWeb);
                    //LogMessage("Installing PfE Resource Events");

                    //workEngineAPI.Execute("AddRemoveFeatureEvents", @"<AddRemoveFeatureEvents><Data><Feature Name=""PFEResourceManagement"" Operation=""ADD""/></Data></AddRemoveFeatureEvents>");
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        // Public Methods (1) 

        public override bool Perform()
        {
            LogMessage("Loading Resource Pool List");

            SPList oResourcePool    = SPWeb.Lists.TryGetList("Resources");
            SPList oRoles           = SPWeb.Lists.TryGetList("Roles");
            SPList oDepartments     = SPWeb.Lists.TryGetList("Departments");
            SPList oHolidaySchedule = SPWeb.Lists.TryGetList("Holiday Schedules");
            SPList oWorkHours       = SPWeb.Lists.TryGetList("Work Hours");

            if (oResourcePool == null)
            {
                LogMessage("", "Resources list missing", 3);
            }
            else if (oRoles == null)
            {
                LogMessage("", "Roles list missing", 3);
            }
            else if (oDepartments == null)
            {
                LogMessage("", "Departments list missing", 3);
            }
            else if (oHolidaySchedule == null)
            {
                LogMessage("", "HolidaySchedules list missing", 3);
            }
            else if (oWorkHours == null)
            {
                LogMessage("", "WorkHours list missing", 3);
            }
            else
            {
                try
                {
                    DataTable dtRoles       = oRoles.Items.GetDataTable();
                    DataTable dtDepartments = oDepartments.Items.GetDataTable();

                    #region Add Temp fields

                    try
                    {
                        if (!oResourcePool.Fields.ContainsFieldWithInternalName("TempRole"))
                        {
                            LogMessage("\tAdding TempRole field");

                            oResourcePool.Fields.Add("TempRole", SPFieldType.Text, false);

                            SPField oField = oResourcePool.Fields.GetFieldByInternalName("TempRole");
                            oField.ShowInDisplayForm = false;
                            oField.ShowInEditForm    = false;
                            oField.ShowInNewForm     = false;
                            oField.Update();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogMessage("\t", "Adding TempRole field: " + ex.Message, 3);
                    }

                    try
                    {
                        if (!oResourcePool.Fields.ContainsFieldWithInternalName("TempDept"))
                        {
                            LogMessage("\tAdding TempDept field");

                            oResourcePool.Fields.Add("TempDept", SPFieldType.Text, false);

                            SPField oField = oResourcePool.Fields.GetFieldByInternalName("TempDept");
                            oField.ShowInDisplayForm = false;
                            oField.ShowInEditForm    = false;
                            oField.ShowInNewForm     = false;
                            oField.Update();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogMessage("\t", "Adding TempDept field: " + ex.Message, 3);
                    }

                    oResourcePool.Update();

                    #endregion

                    #region Process Role and Departments

                    bool bProcessRole = false;
                    bool bProcessDept = false;

                    try
                    {
                        if (oResourcePool.Fields.GetFieldByInternalName("Role").Type == SPFieldType.Choice)
                        {
                            bProcessRole = true;
                        }
                    }
                    catch
                    {
                    }
                    try
                    {
                        if (oResourcePool.Fields.GetFieldByInternalName("Department").Type == SPFieldType.Choice)
                        {
                            bProcessDept = true;
                        }
                    }
                    catch
                    {
                    }

                    SPField oFieldRole;
                    SPField oFieldDept;

                    try
                    {
                        oFieldRole = oResourcePool.Fields.GetFieldByInternalName("Role");
                    }
                    catch
                    {
                        SPField newField = oResourcePool.Fields.CreateNewField(SPFieldType.Choice.ToString(), "Role");
                        oResourcePool.Fields.Add(newField);
                        oResourcePool.Update();

                        oFieldRole = oResourcePool.Fields.GetFieldByInternalName("Role");
                    }

                    try
                    {
                        oFieldDept = oResourcePool.Fields.GetFieldByInternalName("Department");
                    }
                    catch
                    {
                        SPField newField = oResourcePool.Fields.CreateNewField(SPFieldType.Choice.ToString(),
                                                                               "Department");
                        oResourcePool.Fields.Add(newField);
                        oResourcePool.Update();

                        oFieldDept = oResourcePool.Fields.GetFieldByInternalName("Department");
                    }

                    if (bProcessRole || bProcessDept)
                    {
                        LogMessage("\tCopying Temporary Data");

                        foreach (SPListItem li in oResourcePool.Items)
                        {
                            try
                            {
                                if (bProcessDept && li[oFieldDept.Id] != null && oFieldDept.Type == SPFieldType.Choice)
                                {
                                    li["TempDept"] = li[oFieldDept.Id].ToString();
                                }

                                if (bProcessDept && li[oFieldRole.Id] != null && oFieldRole.Type == SPFieldType.Choice)
                                {
                                    li["TempRole"] = li[oFieldRole.Id].ToString();
                                }

                                li.Update();
                            }
                            catch (Exception ex)
                            {
                                LogMessage("\t", "Error (" + li.Title + "): " + ex.Message, 3);
                            }
                        }
                    }

                    Thread.Sleep(5000);

                    #endregion

                    ProcessFields(ref oResourcePool);

                    oFieldRole = oResourcePool.Fields.GetFieldByInternalName("Role");
                    oFieldDept = oResourcePool.Fields.GetFieldByInternalName("Department");

                    LogMessage("\tUpdating Role, Department, Holiday Schedule and Work Hours");

                    EnumerableRowCollection <DataRow> resourceHSWS     = null;
                    EnumerableRowCollection <DataRow> holidaySchedules = null;
                    EnumerableRowCollection <DataRow> workHours        = null;

                    try
                    {
                        var dtResourceHSWS = new DataTable();

                        string connectionString = GetConnectionString();

                        if (string.IsNullOrEmpty(connectionString))
                        {
                            throw new Exception("PFE DB connection string is empty.");
                        }

                        using (var sqlConnection = new SqlConnection(connectionString))
                        {
                            using (var sqlCommand = new SqlCommand(SQL, sqlConnection))
                            {
                                sqlConnection.Open();
                                using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                                {
                                    dtResourceHSWS.Load(sqlDataReader);
                                }
                            }
                        }

                        resourceHSWS = dtResourceHSWS.AsEnumerable();

                        if (resourceHSWS.Any())
                        {
                            holidaySchedules = oHolidaySchedule.Items.GetDataTable().AsEnumerable();
                            workHours        = oWorkHours.Items.GetDataTable().AsEnumerable();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogMessage("\t", "(Get PFE DB ConnectionString): " + ex.Message, 3);
                    }

                    foreach (SPListItem li in oResourcePool.Items)
                    {
                        try
                        {
                            #region Copy Department and Roles

                            if (li["TempDept"] != null && !li["TempDept"].ToString().Contains(";#") &&
                                oFieldDept.Type == SPFieldType.Lookup)
                            {
                                DataRow[] dr = dtDepartments.Select("DisplayName='" + li["TempDept"] + "'");
                                if (dr.Length > 0)
                                {
                                    var lv = new SPFieldLookupValue(int.Parse(dr[0]["ID"].ToString()),
                                                                    li["TempDept"].ToString());
                                    li[oFieldDept.Id] = lv;
                                }
                            }

                            if (li["TempRole"] != null && !li["TempRole"].ToString().Contains(";#") &&
                                oFieldRole.Type == SPFieldType.Lookup)
                            {
                                DataRow[] dr = dtRoles.Select("Title='" + li["TempRole"] + "'");
                                if (dr.Length > 0)
                                {
                                    var lv = new SPFieldLookupValue(int.Parse(dr[0]["ID"].ToString()),
                                                                    li["TempRole"].ToString());
                                    li[oFieldRole.Id] = lv;
                                }
                            }

                            #endregion

                            string spAccount = string.Empty;
                            var    resAcct   = li["SharePointAccount"] as string;
                            if (!string.IsNullOrEmpty(resAcct))
                            {
                                var uv = new SPFieldUserValue(SPWeb, resAcct);
                                if (uv.User != null)
                                {
                                    spAccount = uv.User.LoginName.ToLower();
                                }
                            }

                            string extId = (li["EXTID"] ?? string.Empty).ToString();

                            if (resourceHSWS != null && resourceHSWS.Any() && !string.IsNullOrEmpty(spAccount))
                            {
                                try
                                {
                                    if (string.IsNullOrEmpty(extId))
                                    {
                                        foreach (DataRow row in resourceHSWS)
                                        {
                                            object acct = row["Account"];
                                            if (acct != null && acct != DBNull.Value)
                                            {
                                                if (acct.ToString().ToLower().Equals(spAccount))
                                                {
                                                    li["EXTID"] = (row["ResourceId"] ?? string.Empty).ToString();
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    LogMessage("\t", "(" + li.Title + "): Not setting EXTID. " + ex.Message, 3);
                                }

                                try
                                {
                                    foreach (object schedule in from hs in resourceHSWS
                                             let account = hs["Account"]
                                                           where account != null && account != DBNull.Value
                                                           where account.ToString().ToLower().Equals(spAccount)
                                                           select hs["HolidaySchedule"]
                                                           into schedule
                                                           where schedule != null && schedule != DBNull.Value
                                                           select schedule)
                                    {
                                        foreach (DataRow s in holidaySchedules)
                                        {
                                            object sch = s["Title"];
                                            if (sch == null || sch == DBNull.Value)
                                            {
                                                continue;
                                            }

                                            string hsch = sch.ToString();
                                            if (!hsch.ToLower().Equals(schedule.ToString().ToLower()))
                                            {
                                                continue;
                                            }

                                            li["HolidaySchedule"] =
                                                new SPFieldLookupValue(Convert.ToInt32(s["ID"].ToString()), hsch);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    LogMessage("\t", "(" + li.Title + "): Not setting Holiday Schedule. " + ex.Message,
                                               3);
                                }

                                try
                                {
                                    foreach (object hours in from wh in resourceHSWS
                                             let account = wh["Account"]
                                                           where account != null && account != DBNull.Value
                                                           where account.ToString().ToLower().Equals(spAccount)
                                                           select wh["WorkHours"]
                                                           into hours
                                                           where hours != null && hours != DBNull.Value
                                                           select hours)
                                    {
                                        foreach (DataRow h in workHours)
                                        {
                                            object hr = h["Title"];
                                            if (hr == null || hr == DBNull.Value)
                                            {
                                                continue;
                                            }

                                            string whr = hr.ToString();
                                            if (!whr.ToLower().Equals(hours.ToString().ToLower()))
                                            {
                                                continue;
                                            }

                                            li["WorkHours"] = new SPFieldLookupValue(
                                                Convert.ToInt32(h["ID"].ToString()), whr);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    LogMessage("\t", "(" + li.Title + "): Not setting Work Hours. " + ex.Message, 3);
                                }
                            }
                            else
                            {
                                LogMessage("\t",
                                           "(" + li.Title +
                                           "): Not setting Holiday Schedule and Work Hours. Cannot load from PFE.", 3);
                            }

                            li.Update();

                            LogMessage("\t\t" + li.Title);
                        }
                        catch (Exception ex)
                        {
                            LogMessage("\t", "(" + li.Title + "): " + ex.Message, 3);
                        }
                    }

                    if (bIsPfe)
                    {
                        using (var workEngineAPI = new WorkEngineAPI())
                        {
                            LogMessage("Installing PfE Resource Events");

                            WorkEngineAPI.AddRemoveFeatureEvents(
                                @"<AddRemoveFeatureEvents><Data><Feature Name=""pferesourcemanagement"" Operation=""ADD""/></Data></AddRemoveFeatureEvents>",
                                SPWeb);
                        }
                    }

                    LogMessage("Enabling New Button");

                    using (var spSite = new SPSite(oResourcePool.ParentWeb.Site.ID))
                    {
                        using (SPWeb spWeb = spSite.OpenWeb(oResourcePool.ParentWeb.ID))
                        {
                            SPList resourcePool = spWeb.Lists.GetList(oResourcePool.ID, false);

                            var gSettings = new GridGanttSettings(resourcePool)
                            {
                                HideNewButton = false
                            };
                            gSettings.SaveSettings(resourcePool);
                        }
                    }

                    LogMessage("Processing Editable Fields");
                    UpdateField("Generic", true, false, true, ref oResourcePool);
                    UpdateField("FirstName", true, true, true, ref oResourcePool);
                    UpdateField("LastName", true, true, true, ref oResourcePool);
                    UpdateField("Email", true, false, true, ref oResourcePool);
                    UpdateField("ResourceLevel", true, true, true, ref oResourcePool);
                    UpdateField("Permissions", true, true, true, ref oResourcePool);
                    UpdateField("StandardRate", true, true, true, ref oResourcePool);
                    UpdateField("Department", true, true, true, ref oResourcePool);
                    UpdateField("Role", true, true, true, ref oResourcePool);
                    UpdateField("HolidaySchedule", true, true, true, ref oResourcePool);
                    UpdateField("WorkHours", true, true, true, ref oResourcePool);
                    UpdateField("AvailableFrom", true, true, true, ref oResourcePool);
                    UpdateField("AvailableTo", true, true, true, ref oResourcePool);
                    UpdateField("Disabled", false, true, true, ref oResourcePool);
                }
                catch (Exception ex)
                {
                    LogMessage("", "General: " + ex.Message, 3);
                }
            }

            return(true);
        }