Monday, March 1, 2010

Insert Text to Multiple Rows

So imagine you need to copy a whole chunk of text into multiple rows in a table where you have a threshold of “x” number of characters per row. Here is a code I created to  chop the text up with the SPACE as a delimiter. It puts the rest of the text on the next record until all texts are inserted. This code is located in the methods of the table that it is inserting it to and called in the modifiedfield method.

 

 

void CutText(str _textToCut)

{

    int threshold;

    str delimiter, origText, curText;

    int nextLineNum, curTextLen, trimPos, nextTrimPos;

    NarrationTable narration;

    ;

 

    threshold = 74;

    delimiter = " ";

    curText = _textToCut;

    origText = curText;

 

    //check how long the text is

    curTextLen = strLen(curText);

 

    //deal with the first record to insert first

    if (curTextLen > threshold)

    {

        //find position. Note a negative value in the last parameter means to start searching from the back of the text      

        trimPos = strfind(curText,delimiter,curTextLen,-curTextLen);

 

        if (trimPos > threshold)

        {

            //find the first position

            while (trimPos > threshold)

            {

                curText = substr(curText,1, trimPos - 1);

                curTextLen = strLen(curText);

                trimPos = strfind(curText,delimiter,curTextLen,-curTextLen);

            }

            //insert the first text

            this.Notes = curText;

            //if new insert, if not, just save

            if (!this.RecId)

            {

                this.LineNum = this.GetLastLineNum(this.SecondaryId) + 1;

                this.doInsert();

            }

            else

                this.doUpdate();

        }

 

        //insert the rest in a new record

        curText = substr(origText,trimPos + 1,strlen(origText));

        trimPos = strfind(curText,delimiter,1,curTextLen);      //remove extra word

        curText = substr(curText,trimPos + 1,strlen(curText));  //remove extra word

        origText = curText;

        curTextLen = strLen(curText);

       

       //go through and cut up the rest of the text and insert it and cut it until all text can fit within the threshold

        while (trimPos)

        {

            trimPos = strfind(curText,delimiter,curTextLen,-curTextLen);

           

            while (trimPos > threshold)

            {

                curText = substr(curText,1, trimPos - 1);

                curTextLen = strLen(curText);

                trimPos = strfind(curText,delimiter,curTextLen,-curTextLen);

            }

 

            ttsbegin;

            select forupdate narration

                order by LineNum desc where

                narration.SecondaryId == this.SecondaryId;

            //there should always be a record selected. but juse in case...

            if (narration)

            {

                narration.LineNum = narration.LineNum + 1;

                narration.Notes = curText;

                narration.insert();

            }

            ttscommit;

           

            curText = substr(origText,trimPos + 1,strlen(origText));

            trimPos = strfind(curText,delimiter,1,curTextLen);      //remove extra word

            curText = substr(curText,trimPos + 1,strlen(curText));  //remove extra word

            origText = curText;

            curTextLen = strLen(curText);

            trimPos = strfind(curText,delimiter,curTextLen,-curTextLen);

        }

    }

}

No comments:

Post a Comment