예제 #1
0
        public bool AddSnippetProperties(long snippetID, ICollection <SnippetProperty> properties)
        {
            if ((snippetID <= 0) || properties.IsNullOrEmpty())
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT,
                             string.Format("Input error: snippetID={0}, properties={1}", snippetID, properties.Print <SnippetProperty>()));
                return(false);
            }

            //check input properties and clean them if not valid:
            List <SnippetProperty> cleanProperties = SnippetProperty.CleanPropertyList(properties, snippetID);

            //fastly return if no properties are to be added:
            if (cleanProperties.IsNullOrEmpty())
            {
                return(true);
            }

            //send the request and parse the response:
            S2CSerializer      ser           = new S2CSerializer(m_serialFormat, snippetID);
            string             contentToSend = "content=" + HttpUtility.UrlEncode(ser.SerializeBaseEntityList <SnippetProperty>(cleanProperties.ToArray(), null));
            S2CResObj <object> resp          = SendReqObj(ADD_PROPERTIES_URL, contentToSend, true);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #2
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////


        #region Create/Add/Modify Methods
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        ///// <summary>
        ///// Create a Snippet having Owner and Creator = CurrentUser
        ///// </summary>
        //public long SaveSnippet(Snippet snippet)
        //{
        //    //Check that the client has been already logged in, and if this is not the case, automatically login:
        //    if (!CheckLogin())
        //    {
        //        SetLastError(log, "Cannot login into the system");
        //        return -1;
        //    }

        //    snippet.TargetGroupID = CurrentUser.PersonalGroupID;
        //    return CreateSnippet(snippet, CurrentUser);
        //}


        public long SaveSnippet(Snippet snippet)
        {
            if ((snippet == null) || (snippet.AreFieldsValid() != SnippetWrongField.OK))
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT,
                             string.Format("Input error: snippet={0}, creator={1}", snippet.PrintNull(), CurrentUser.PrintNull()));
                return(-1);
            }

            //Check that the client has been already logged in, and if this is not the case, automatically login:
            if (!CheckLogin())
            {
                SetLastError(log, ErrorCodes.NOT_LOGGED_IN, "Cannot login into the system");
                return(-1);
            }

            //send the request and parse the response:
            string             contentToSend = "content=" + HttpUtility.UrlEncode(snippet.Serialize(m_serialFormat));
            S2CResObj <object> resp          = SendReqObj(CREATE_SNIPPET_URL, contentToSend, true);

            //build the result:
            long result = ParseLongResponse(resp);

            if (result == -1)
            {
                //check if the Status is TARGET_GROUP_INVALID:
                if ((resp != null) && (resp.Status == ErrorCodes.TARGET_GROUP_DELETED))
                {
                    return(-2);
                }
            }
            return(result);
        }
예제 #3
0
파일: BaseWS.cs 프로젝트: chcg/snip2codeNET
        /// <summary>
        /// Sends an HTTP Request to the backend and parse the response (expected as an object)
        /// </summary>
        /// <param name="action">action to query (e.g. "Snippets/Get")</param>
        /// <param name="data">
        ///     if the request is a GET, the parameters to be put in the querystring (e.g. "snippetID=100"),
        ///     otherwise data to be put in the post (e.g. "content=...")
        /// </param>
        /// <param name="isPost">whether this request is a GET(false) or a POST(true)</param>
        /// <param name="requiresLogin">false if this request calls a public web service</param>
        /// <returns>null if any error occurred; the result of the invocation otherwise</returns>
        protected S2CResObj <object> SendReqObj(string action, string data, bool isPost, bool requiresLogin = true)
        {
            string response = PrepareAndSendReq(action, data, isPost, requiresLogin);

            if (string.IsNullOrEmpty(response))
            {
                ErrorCodes errCode = ErrorCodes.COMMUNICATION_ERROR;
                if (WebConnector.Current.IsTimeout)
                {
                    errCode = ErrorCodes.TIMEOUT;
                }
                SetLastError(log, errCode, S2CRes <string> .GetErrorMsg(errCode));
                return(new S2CResObj <object>(0.0, errCode, null));
            }

            S2CResObj <object> resp = S2CSerializer.DeserializeObj(response, m_serialFormat);

            if (!CheckResp <object>(resp))
            {
                PrintRespError <object>(resp);

                //if the problem is related to user not logged in, reset login status and retry another time:
                if (requiresLogin && (resp != null) && (resp.Status == ErrorCodes.NOT_LOGGED_IN))
                {
                    WebConnector.Current.ResetLoginStatus(); //reset login status

                    //retry the WS call:
                    response = PrepareAndSendReq(action, data, isPost, requiresLogin);
                    resp     = S2CSerializer.DeserializeObj(response, m_serialFormat);
                }
            }

            return(resp);
        }
예제 #4
0
        public int GetSnippetVote(long snippetID)
        {
            //send the request and parse the response:
            S2CResObj <object> resp = SendReqObj(GET_SNIPPET_VOTE_URL, "snippetID=" + snippetID, false, false);

            //build the result:
            return((int)ParseLongResponse(resp));
        }
예제 #5
0
        public int CountSnippets(int groupID = -1)
        {
            //send the request and parse the response:
            S2CResObj <object> resp = SendReqObj(COUNT_SNIPPET_URL, "groupID=" + groupID, false, true);

            //build the result:
            return((int)ParseLongResponse(resp));
        }
예제 #6
0
파일: BaseWS.cs 프로젝트: chcg/snip2codeNET
 /// <summary>
 /// Prints the info regarding the given erroneous response
 /// </summary>
 /// <param name="resp">response to print</param>
 /// <returns></returns>
 protected void PrintRespError <T>(S2CRes <T> resp)
 {
     if ((resp == null) || !(resp is S2CResObj <object>))
     {
         SetLastError(log, (resp == null ? ErrorCodes.FAIL : resp.Status), "Generic Error");
         log.Warn("null response");
     }
     else
     {
         S2CResObj <T> errMessage = (S2CResObj <T>)resp;
         SetLastError(log, resp.Status, errMessage.Data.PrintNull());
         log.ErrorFormat("Error receiving the response: status={0}; messsage={1}", resp.Status, LastErrorMsg);
     }
 }
예제 #7
0
        public int GetCommentsCount(long snippetID)
        {
            if (snippetID <= 0)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippetID={0}", snippetID));
                return(-1);
            }

            //send the request and parse the response:
            S2CResObj <object> resp = SendReqObj(GET_COMMENTS_COUNT_URL, "id=" + snippetID, false);

            //build the result:
            return((int)ParseLongResponse(resp));
        }
예제 #8
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////


        #region Add Methods
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        public bool SuggestDefaultProperty(DefaultProperty defProp, int userID)
        {
            //send the request and parse the response:
            string             contentToSend = "content=" + HttpUtility.UrlEncode(defProp.Serialize(m_serialFormat));
            S2CResObj <object> resp          = SendReqObj(SUGGEST_DEFAULTS_URL, contentToSend, true);

            //build the result:
            if (!CheckResp(resp))
            {
                PrintRespError(resp);
                return(false);
            }

            return(ParseBoolResponse(resp));
        }
예제 #9
0
        public long GetSnippetIDByName(string name)
        {
            //check for erroneous input:
            if (string.IsNullOrWhiteSpace(name))
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: userID={0}, name={1}", CurrentUserID, name.PrintNull()));
                return(-1);
            }

            //send the request and parse the response:
            S2CResObj <object> resp = SendReqObj(GETID_BYNAME_URL, HttpUtility.UrlEncode(name), false, false);

            //build the result:
            return(ParseLongResponse(resp));
        }
예제 #10
0
        public bool DeleteComment(long commentID)
        {
            if (commentID <= 0)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: commentID={0}", commentID));
                return(false);
            }

            //send the request and parse the response:
            string             contentToSend = string.Format("commentID={0}", commentID);
            S2CResObj <object> resp          = SendReqObj(DELETE_COMMENT_URL, contentToSend, true);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #11
0
        public bool StoreHit(Snippet snippet)
        {
            //check input validity:
            if (snippet == null)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippet={0}", snippet.PrintNull()));
                return(false);
            }

            //send the request and parse the response:
            S2CResObj <object> resp = SendReqObj("SnippetHit/" + snippet.ID, string.Empty, true, false);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #12
0
        public bool ClearSnippetProperties(long snippetID)
        {
            if (snippetID <= 0)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippetID={0}", snippetID));
                return(false);
            }

            //send the request and parse the response:
            string             contentToSend = string.Format("snippetID={0}", snippetID);
            S2CResObj <object> resp          = SendReqObj(CLEAR_PROPERTIES_URL, contentToSend, true);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #13
0
        public bool ShareSnippet(long snippetID, int groupID)
        {
            if ((snippetID <= 0) || (groupID < 0))
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippetID={0}; groupID={1}", snippetID, groupID));
                return(false);
            }

            //send the request and parse the response:
            string             contentToSend = string.Format("snippetID={0}&groupID={1}", snippetID, groupID);
            S2CResObj <object> resp          = SendReqObj(SHARE_SNIPPET_URL, contentToSend, true);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #14
0
        public bool RateSnippet(long snippetID, int rating, int previousRating = 0)
        {
            if ((snippetID <= 0) || (rating < 0))
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippetID={0}; rating={1}", snippetID, rating));
                return(false);
            }

            //send the request and parse the response:
            string             contentToSend = string.Format("snippetID={0}&rating={1}&previousRating={2}", snippetID, rating, previousRating);
            S2CResObj <object> resp          = SendReqObj(RATE_SNIPPET_URL, contentToSend, true);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #15
0
        public bool PublishSnippet(long snippetID, ICollection <int> channelIDs)
        {
            if ((snippetID <= 0) || channelIDs.IsNullOrEmpty())
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippetID={0}; channelIDs={1}", snippetID, channelIDs.PrintNull()));
                return(false);
            }

            //send the request and parse the response:
            string             content       = Utilities.MergeIntoCommaSeparatedString(channelIDs, true);
            string             contentToSend = string.Format("snippetID={0}&channelsIDs={1}", snippetID, content);
            S2CResObj <object> resp          = SendReqObj(PUBLISH_SNIPPET_URL, contentToSend, true);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #16
0
        public bool AddSnippetTags(long snippetID, ICollection <string> tags)
        {
            if ((snippetID <= 0) || tags.IsNullOrEmpty())
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippetID={0}, tags={1}", snippetID, tags.Print <string>()));
                return(false);
            }

            //send the request and parse the response:
            S2CSerializer      ser           = new S2CSerializer(m_serialFormat, snippetID);
            string             contentToSend = "content=" + HttpUtility.UrlEncode(ser.SerializeListObj <string>(tags.ToArray()));
            S2CResObj <object> resp          = SendReqObj(ADD_TAGS_URL, contentToSend, true);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #17
0
        public bool DeleteSnippetProperty(SnippetProperty prop)
        {
            if ((prop == null) || !prop.DataAreValid())
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: property={0}", prop.PrintNull()));
                return(false);
            }

            //send the request and parse the response:
            string contentToSend = string.Format("snippetID={0}&name={1}&value={2}",
                                                 prop.SnippetID, HttpUtility.UrlEncode(prop.Name), HttpUtility.UrlEncode(prop.Value));
            S2CResObj <object> resp = SendReqObj(DELETE_PROPERTY_BYNAME_URL, contentToSend, true);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #18
0
        public bool StoreCopyAction(Snippet snippet, bool embedded)
        {
            //check input validity:
            if (snippet == null)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippet={0}", snippet.PrintNull()));
                return(false);
            }

            //send the request and parse the response:
            S2CResObj <object> resp = SendReqObj("Explore/WhoCopy?snippetId=" + snippet.ID + "&embedded=" + embedded,
                                                 string.Empty, true, false);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #19
0
        public bool AddSnippetComment(long snippetID, string comment)
        {
            if ((snippetID <= 0) || string.IsNullOrEmpty(comment))
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT,
                             string.Format("Input error: snippetID={0}, comment={1}", snippetID, comment.PrintNull()));
                return(false);
            }

            //send the request and parse the response:
            string             contentToSend = string.Format("snippetID={0}&comment={1}", snippetID, HttpUtility.UrlEncode(comment));
            S2CResObj <object> resp          = SendReqObj(ADD_COMMENT_URL, contentToSend, true);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #20
0
        public long AddSnippetProperty(long snippetID, string propName, string propValue, string oldPropValue = "")
        {
            if ((snippetID <= 0) || !SnippetProperty.DataAreValid(propName, propValue))
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippetID={0}, propName={1}, propValue={2}",
                                                                        snippetID, propName.PrintNull(), propValue.PrintNull()));
                return(-1);
            }

            //send the request and parse the response:
            string contentToSend = string.Format("snippetID={0}&propName={1}&propValue={2}&oldPropValue={3}", snippetID,
                                                 HttpUtility.UrlEncode(propName), HttpUtility.UrlEncode(propValue), HttpUtility.UrlEncode(oldPropValue));
            S2CResObj <object> resp = SendReqObj(ADD_PROPERTY_URL, contentToSend, true);

            //build the result:
            return(ParseLongResponse(resp));
        }
예제 #21
0
        public bool DeleteSnippetProperty(long propertyID, long snippetID, string value)
        {
            if ((propertyID <= 0) || (snippetID <= 0) || string.IsNullOrEmpty(value))
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: property SnippetID={0}, ID={1}, Value={2}",
                                                                        snippetID, propertyID, value));
                return(false);
            }

            //send the request and parse the response:
            string contentToSend = string.Format("propertyID={0}&snippetID={1}&value={2}",
                                                 propertyID, snippetID, HttpUtility.UrlEncode(value));
            S2CResObj <object> resp = SendReqObj(DELETE_PROPERTY_URL, contentToSend, true);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #22
0
        public bool CheckExistenceByNameAndCode(string itemName, string code)
        {
            //check for erroneous input:
            if (string.IsNullOrWhiteSpace(itemName) || string.IsNullOrWhiteSpace(code))
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: userID={0}, name={1}, code={2}",
                                                                        CurrentUserID, itemName.PrintNull(), code.PrintNull()));
                return(false);
            }

            //send the request and parse the response:
            string             querystring = string.Format("itemName={0}&code={1}", HttpUtility.UrlEncode(itemName), HttpUtility.UrlEncode(code));
            S2CResObj <object> resp        = SendReqObj(CHECKEXIST_BYNAME_URL, querystring, true, true);

            //build the result:
            return(ParseBoolResponse(resp));
        }
예제 #23
0
파일: BaseWS.cs 프로젝트: chcg/snip2codeNET
        /// <summary>
        /// Try to parse the given response that is expected to return a long result
        /// </summary>
        /// <returns></returns>
        protected long ParseLongResponse <T>(S2CRes <T> resp)
        {
            if (!CheckResp <T>(resp))
            {
                PrintRespError <T>(resp);
                return(-1);
            }

            long          result = -1;
            S2CResObj <T> value  = (S2CResObj <T>)resp;

            if (!long.TryParse(value.Data.ToString(), out result))
            {
                PrintRespError <T>(resp);
                return(-1);
            }

            return(result);
        }
예제 #24
0
파일: BaseWS.cs 프로젝트: chcg/snip2codeNET
        /// <summary>
        /// Try to parse the given response that is expected to return a boolean result
        /// </summary>
        /// <returns></returns>
        protected bool ParseBoolResponse <T>(S2CRes <T> resp)
        {
            if (!CheckResp <T>(resp))
            {
                PrintRespError <T>(resp);
                return(false);
            }

            bool          result = false;
            S2CResObj <T> value  = (S2CResObj <T>)resp;

            if (!bool.TryParse(value.Data.ToString(), out result))
            {
                PrintRespError <T>(resp);
                return(false);
            }

            return(result);
        }