<commit_before>package com.imcode.imcms.servlet.admin;

import imcode.server.Imcms;
import imcode.server.ImcmsServices;
import imcode.server.WebAppGlobalConstants;
import imcode.server.document.DocumentMapper;
import imcode.server.document.textdocument.TextDocumentDomainObject;
import imcode.server.document.textdocument.TextDomainObject;
import imcode.server.user.UserDomainObject;
import imcode.util.Utility;
import imcode.util.poll.PollHandlingSystem;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

/**
 * Save textdocument in a document.
 */
public final class SaveText extends HttpServlet {

    public void doPost( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException {
        req.setCharacterEncoding( WebAppGlobalConstants.DEFAULT_ENCODING_WINDOWS_1252 );
        Utility.setDefaultHtmlContentType( res );
        Writer out = res.getWriter();

        // Check if user has permission to be here
        ImcmsServices imcref = Imcms.getServices();
        int meta_id = Integer.parseInt( req.getParameter( "meta_id" ) );
        UserDomainObject user = Utility.getLoggedOnUser( req );
        if ( !imcref.checkDocAdminRights( meta_id, user, imcode.server.ImcmsConstants.PERM_EDIT_TEXT_DOCUMENT_TEXTS ) ) {	// Checking to see if user may edit this
            String output = AdminDoc.adminDoc( meta_id, user, req, res );
            if ( output != null ) {
                out.write( output );
            }
            return;
        }

        // get text_no
        int txt_no = Integer.parseInt( req.getParameter( "txt_no" ) );

        // get textdocument
        String text_string = req.getParameter( "text" );

        int text_format = Integer.parseInt( req.getParameter( "format_type" ) );

        String text_type = req.getParameter( "txt_type" ); // ex. pollrequest-1
        if ( text_type == null ) {
            text_type = "";
        }

        TextDomainObject text = new TextDomainObject( text_string, text_format );

        user.put( "flags", new Integer( imcode.server.ImcmsConstants.PERM_EDIT_TEXT_DOCUMENT_TEXTS ) );

        if ( req.getParameter( "ok" ) != null ) {
            DocumentMapper documentMapper = imcref.getDocumentMapper();
            TextDocumentDomainObject document = (TextDocumentDomainObject)documentMapper.getDocument(meta_id);

            saveText( documentMapper, text, document, txt_no, text_type, imcref, meta_id, user );
        }

        String output = AdminDoc.adminDoc( meta_id, user, req, res );
        if ( output != null ) {
            out.write( output );
        }
    }

    private void saveText( DocumentMapper documentMapper, TextDomainObject text, TextDocumentDomainObject document,
                           int txt_no, String text_type, ImcmsServices imcref, int meta_id,
                           UserDomainObject user ) {
        document.setText( txt_no, text );

        documentMapper.saveDocument( document, user );

        if ( !"".equals( text_type ) ) {

            if ( text_type.startsWith( "poll" ) ) {
                PollHandlingSystem poll = imcref.getPollHandlingSystem();
                poll.savePollparameter( text_type, document.getId(), txt_no, text.getText() );
            }
        }
        imcref.updateMainLog( "Text " + txt_no + " in [" + meta_id + "] modified by user: [" + user.getFullName() + "]");
    }
}<commit_msg>Bug 2776: Redirect back to AdminDoc after completing editing of text.<commit_after>package com.imcode.imcms.servlet.admin;

import imcode.server.Imcms;
import imcode.server.ImcmsServices;
import imcode.server.WebAppGlobalConstants;
import imcode.server.document.DocumentMapper;
import imcode.server.document.textdocument.TextDocumentDomainObject;
import imcode.server.document.textdocument.TextDomainObject;
import imcode.server.user.UserDomainObject;
import imcode.util.Utility;
import imcode.util.poll.PollHandlingSystem;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

public final class SaveText extends HttpServlet {

    public void doPost( HttpServletRequest req, HttpServletResponse res ) throws IOException {
        req.setCharacterEncoding( WebAppGlobalConstants.DEFAULT_ENCODING_WINDOWS_1252 );
        Utility.setDefaultHtmlContentType( res );
        Writer out = res.getWriter();

        // Check if user has permission to be here
        ImcmsServices imcref = Imcms.getServices();
        int meta_id = Integer.parseInt( req.getParameter( "meta_id" ) );
        UserDomainObject user = Utility.getLoggedOnUser( req );
        if ( imcref.checkDocAdminRights( meta_id, user, imcode.server.ImcmsConstants.PERM_EDIT_TEXT_DOCUMENT_TEXTS )
             && req.getParameter( "ok" ) != null ) {
            // get text_no
            int txt_no = Integer.parseInt( req.getParameter( "txt_no" ) );

            // get textdocument
            String text_string = req.getParameter( "text" );

            int text_format = Integer.parseInt( req.getParameter( "format_type" ) );

            String text_type = req.getParameter( "txt_type" ); // ex. pollrequest-1
            if ( text_type == null ) {
                text_type = "";
            }

            TextDomainObject text = new TextDomainObject( text_string, text_format );

            DocumentMapper documentMapper = imcref.getDocumentMapper();
            TextDocumentDomainObject document = (TextDocumentDomainObject)documentMapper.getDocument( meta_id );

            saveText( documentMapper, text, document, txt_no, text_type, imcref, meta_id, user );
        }

        res.sendRedirect( "AdminDoc?meta_id=" + meta_id + "&flags="
                          + imcode.server.ImcmsConstants.PERM_EDIT_TEXT_DOCUMENT_TEXTS );

    }

    private void saveText( DocumentMapper documentMapper, TextDomainObject text, TextDocumentDomainObject document,
                           int txt_no, String text_type, ImcmsServices imcref, int meta_id,
                           UserDomainObject user ) {
        document.setText( txt_no, text );

        documentMapper.saveDocument( document, user );

        if ( !"".equals( text_type ) ) {

            if ( text_type.startsWith( "poll" ) ) {
                PollHandlingSystem poll = imcref.getPollHandlingSystem();
                poll.savePollparameter( text_type, document.getId(), txt_no, text.getText() );
            }
        }
        imcref.updateMainLog( "Text " + txt_no + " in [" + meta_id + "] modified by user: [" + user.getFullName()
                              + "]" );
    }
}
{'fraction_non_alphanumeric': 0.09691176470588235, 'fraction_numerical': 0.002058823529411765, 'mean_word_length': 2.8186412128017966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7317226', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1887, 'n_words': 417}