public static UserJobTitleServiceAttributes Get(int userID, int jobTitleID, int languageID, int countryID)
        {
            var data = new UserJobTitleServiceAttributes
            {
                userID     = userID,
                jobTitleID = jobTitleID,
                languageID = languageID,
                countryID  = countryID
            };

            data.serviceAttributes = LcRest.ServiceAttribute.GetGroupedUserJobTitleAttributeIDs(jobTitleID, userID, languageID, countryID);
            data.experienceLevelID = GetExperienceLevelID(userID, jobTitleID, languageID, countryID);

            return(data);
        }
Exemplo n.º 2
0
        public static PublicUserJobTitleServiceAttributes Get(int userID, int jobTitleID, int languageID, int countryID)
        {
            var data = new PublicUserJobTitleServiceAttributes();

            data.userID            = userID;
            data.jobTitleID        = jobTitleID;
            data.languageID        = languageID;
            data.countryID         = countryID;
            data.serviceAttributes = ServiceAttribute.GetGroupedUserJobTitleAttributes(jobTitleID, userID, languageID, countryID);
            var experienceID = UserJobTitleServiceAttributes.GetExperienceLevelID(userID, jobTitleID, languageID, countryID);

            data.experienceLevel = ExperienceLevel.GetItem(experienceID, languageID, countryID);

            return(data);
        }
    private LcRest.UserJobTitleServiceAttributes GetInputData()
    {
        var data = new LcRest.UserJobTitleServiceAttributes();

        data.serviceAttributes         = new Dictionary <int, List <int> >();
        data.proposedServiceAttributes = new Dictionary <int, List <string> >();

        // Attributes
        var reg = new System.Text.RegularExpressions.Regex(@"^(?:proposed)?[sS]erviceAttributes\[([^\]]+)\]", System.Text.RegularExpressions.RegexOptions.ECMAScript);

        foreach (var key in Request.Form.AllKeys)
        {
            var match = reg.Match(key);
            if (match.Success)
            {
                var catID = match.Groups[1].Value.AsInt();
                // Get attributes IDs from the list (avoid any '0' or non positive integer value)
                var values = Request.Form.GetValues(key);
                if (values != null)
                {
                    var atts = values.Select(x => x.AsInt(0)).Where(x => x > 0).ToList();
                    if (atts.Count > 0)
                    {
                        data.serviceAttributes.Add(catID, atts);
                    }
                    // Get attributes NAMEs from the list, for new proposed attributes
                    var attNames = values.Where(x => !x.IsInt()).ToList();
                    if (attNames.Count > 0)
                    {
                        data.proposedServiceAttributes.Add(catID, attNames);
                    }
                }
            }
        }

        // Experience level
        data.experienceLevelID = Request.Form["experienceLevelID"].AsInt(0);

        return(data);
    }
        static public void Set(UserJobTitleServiceAttributes serviceAttributes)
        {
            // Validate
            // Get all attributes that applies (we avoid save selected attributes that does not apply
            // to the job title).
            var validAttributes        = ServiceAttribute.GetGroupedJobTitleAttributes(serviceAttributes.jobTitleID, serviceAttributes.languageID, serviceAttributes.countryID);
            var indexedValidAttributes = new Dictionary <int, HashSet <int> >();

            // Check that there is almost one value for required categories, or show error
            foreach (var attCat in validAttributes)
            {
                if (attCat.requiredInput && (
                        !serviceAttributes.serviceAttributes.ContainsKey(attCat.serviceAttributeCategoryID) ||
                        serviceAttributes.serviceAttributes[attCat.serviceAttributeCategoryID].Count == 0))
                {
                    throw new ValidationException(String.Format(requiredAttCatError, attCat.name), attCat.serviceAttributeCategoryID.ToString(), "serviceAttributes");
                }
                indexedValidAttributes.Add(attCat.serviceAttributeCategoryID, new HashSet <int>(attCat.serviceAttributes.Select(x => x.serviceAttributeID)));
            }

            // Save data
            using (var db = new LcDatabase())
            {
                // Transaction
                db.Execute("BEGIN TRANSACTION");

                // First, remove all current attributes, replaced by the new set
                db.Execute(sqlDelAllAttributes, serviceAttributes.userID, serviceAttributes.jobTitleID, serviceAttributes.languageID, serviceAttributes.countryID);

                // Add new ones, if they are valid
                foreach (var cat in serviceAttributes.serviceAttributes)
                {
                    if (indexedValidAttributes.ContainsKey(cat.Key))
                    {
                        foreach (var att in cat.Value)
                        {
                            if (indexedValidAttributes[cat.Key].Contains(att))
                            {
                                // Add to database
                                db.Execute(sqlInsertAttribute, serviceAttributes.userID, serviceAttributes.jobTitleID, cat.Key, att, serviceAttributes.languageID, serviceAttributes.countryID);
                            }
                            // else JUST DISCARD SILENTLY INVALID ATTID
                        }
                    }
                    // else JUST DISCARD SILENTLY INVALID CATID
                }

                // Register user proposed new attributes:
                foreach (var cat in serviceAttributes.proposedServiceAttributes)
                {
                    // Category must exists, even if the attribute is new.
                    if (indexedValidAttributes.ContainsKey(cat.Key))
                    {
                        foreach (var attName in cat.Value)
                        {
                            if (String.IsNullOrWhiteSpace(attName))
                            {
                                continue;
                            }

                            // Clean-up, preparation of the new name
                            var newAttName = attName.Capitalize().Replace(",", "");

                            // Register new attribute
                            int serviceAttributeID = db.QueryValue(sqlRegisterNewAttribute,
                                                                   serviceAttributes.languageID,
                                                                   serviceAttributes.countryID,
                                                                   null, // sourceID
                                                                   newAttName,
                                                                   null, // description
                                                                   serviceAttributes.jobTitleID,
                                                                   serviceAttributes.userID,
                                                                   false,  // Initially not approved
                                                                   cat.Key // categoryID
                                                                   );
                            // Set for the user:
                            db.Execute(sqlInsertAttribute, serviceAttributes.userID, serviceAttributes.jobTitleID,
                                       cat.Key, serviceAttributeID, serviceAttributes.languageID, serviceAttributes.countryID);
                        }
                    }
                }

                if (serviceAttributes.proposedServiceAttributes.Count > 0)
                {
                    LcMessaging.NotifyNewServiceAttributes(serviceAttributes.userID, serviceAttributes.jobTitleID, serviceAttributes.proposedServiceAttributes);
                }

                // Since ExperienceLevel is not a service category anymore else an independent table, we need
                // specific code to save its data.
                if (serviceAttributes.experienceLevelID > 0)
                {
                    db.Execute(sqlSetExpLevel, serviceAttributes.userID, serviceAttributes.jobTitleID, serviceAttributes.languageID, serviceAttributes.countryID, serviceAttributes.experienceLevelID);
                }
                else
                {
                    db.Execute(sqlDelExpLevel, serviceAttributes.userID, serviceAttributes.jobTitleID, serviceAttributes.languageID, serviceAttributes.countryID);
                }

                // Check alert
                db.Execute("EXEC TestAlertPositionServices @0, @1", serviceAttributes.userID, serviceAttributes.jobTitleID);

                // Ends transaction (very important for the delete-insert attributes part, but it guarantees that all or nothing):
                db.Execute("COMMIT TRANSACTION");
            }
        }