<commit_before>/*
 * UniTime 3.0 (University Course Timetabling & Student Sectioning Application)
 * Copyright (C) 2007, UniTime.org, and individual contributors
 * as indicated by the @authors tag.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.unitime.timetable.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.hibernate.EntityMode;
import org.hibernate.MappingException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.hql.QueryExecutionRequestException;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.type.CollectionType;
import org.hibernate.type.Type;
import org.unitime.commons.Debug;
import org.unitime.commons.hibernate.util.HibernateUtil;
import org.unitime.commons.web.Web;
import org.unitime.timetable.form.HibernateQueryTestForm;
import org.unitime.timetable.model.dao._RootDAO;


/** 
 * MyEclipse Struts
 * Creation date: 12-16-2005
 * 
 * XDoclet definition:
 * @struts:action path="/hibernateQueryTest" name="hibernateQueryTestForm" input="/form/hibernateQueryTest.jsp" scope="request"
 */
public class HibernateQueryTestAction extends Action {

    // --------------------------------------------------------- Instance Variables

    // --------------------------------------------------------- Methods

    /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        HttpSession httpSession = request.getSession();
		if(!Web.isLoggedIn( httpSession ) || !Web.isAdmin(httpSession)) {
            throw new Exception ("Access Denied.");
        }

		String op = request.getParameter("op");
		if(op==null || !op.equals("Submit")) 
		    return mapping.findForward("displayQueryForm");
		
        HibernateQueryTestForm frm = (HibernateQueryTestForm) form;
        ActionMessages errors =  frm.validate(mapping, request);
        
        if(errors.size()==0) {
            try {
		        String query = frm.getQuery();	        
		        _RootDAO rdao = new _RootDAO();
		        Session hibSession = rdao.getSession();	        
		        Query q = hibSession.createQuery(query);
		        try {
	                List l = q.list();
	                StringBuffer s = new StringBuffer();
	                int line = 0;
	                for (Iterator i=l.iterator();i.hasNext();line++) {
	                    if (line>=100) {
	                        s.append("<tr><td>...</td></tr>"); break;
	                    }
	                    Object o = i.next();
	                    if (s.length()==0) printHeader(s, o);
	                    printLine(s, o);
	                }
	                if (s.length()>0) {
	                    printFooter(s);
	                    request.setAttribute("result", s.toString());
	                }
	                frm.setListSize(String.valueOf(l.size()));
		        } catch (QueryExecutionRequestException e) {
		            int i = q.executeUpdate();
		            HibernateUtil.clearCache();
		            request.setAttribute("result", i+" lines updated.");
		            frm.setListSize(String.valueOf(i));
		        }
            }
            catch (Exception e) {
                errors.add("query", 
                        	new ActionMessage("errors.generic", e.getMessage()));
                Debug.error(e);
            }
        }

        saveErrors(request, errors);        
        return mapping.findForward("displayQueryForm");
        
    }
    
    private void header(StringBuffer s, int idx, String text) {
        s.append("<td class='WebTableHeader'><i>");
        if (text==null || text.length()<=0)<commit_msg>execute update/insert/delete query in a transaction<commit_after>/*
 * UniTime 3.0 (University Course Timetabling & Student Sectioning Application)
 * Copyright (C) 2007, UniTime.org, and individual contributors
 * as indicated by the @authors tag.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.unitime.timetable.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.hibernate.EntityMode;
import org.hibernate.MappingException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.hql.QueryExecutionRequestException;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.type.CollectionType;
import org.hibernate.type.Type;
import org.unitime.commons.Debug;
import org.unitime.commons.hibernate.util.HibernateUtil;
import org.unitime.commons.web.Web;
import org.unitime.timetable.form.HibernateQueryTestForm;
import org.unitime.timetable.model.dao._RootDAO;


/** 
 * MyEclipse Struts
 * Creation date: 12-16-2005
 * 
 * XDoclet definition:
 * @struts:action path="/hibernateQueryTest" name="hibernateQueryTestForm" input="/form/hibernateQueryTest.jsp" scope="request"
 */
public class HibernateQueryTestAction extends Action {

    // --------------------------------------------------------- Instance Variables

    // --------------------------------------------------------- Methods

    /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        HttpSession httpSession = request.getSession();
		if(!Web.isLoggedIn( httpSession ) || !Web.isAdmin(httpSession)) {
            throw new Exception ("Access Denied.");
        }

		String op = request.getParameter("op");
		if(op==null || !op.equals("Submit")) 
		    return mapping.findForward("displayQueryForm");
		
        HibernateQueryTestForm frm = (HibernateQueryTestForm) form;
        ActionMessages errors =  frm.validate(mapping, request);
        
        if(errors.size()==0) {
            try {
		        String query = frm.getQuery();	        
		        _RootDAO rdao = new _RootDAO();
		        Session hibSession = rdao.getSession();	        
		        Query q = hibSession.createQuery(query);
		        try {
	                List l = q.list();
	                StringBuffer s = new StringBuffer();
	                int line = 0;
	                for (Iterator i=l.iterator();i.hasNext();line++) {
	                    if (line>=100) {
	                        s.append("<tr><td>...</td></tr>"); break;
	                    }
	                    Object o = i.next();
	                    if (s.length()==0) printHeader(s, o);
	                    printLine(s, o);
	                }
	                if (s.length()>0) {
	                    printFooter(s);
	                    request.setAttribute("result", s.toString());
	                }
	                frm.setListSize(String.valueOf(l.size()));
		        } catch (QueryExecutionRequestException e) {
		            Transaction tx = null;
		            try {
		                tx = hibSession.beginTransaction();
		                int i = q.executeUpdate();
	                    request.setAttribute("result", i+" lines updated.");
	                    frm.setListSize(String.valueOf(i));
		                tx.commit();
		            } catch (Exception ex) {
		                if (tx!=null && tx.isActive()) tx.rollback();
		                throw ex;
		            }
		            hibSession.flush();
		            HibernateUtil.clearCache();
		        }
            }
            catch (Exception e) {
                errors.add("query", 
                        	new ActionMessage("errors.generic", e.getMessage()));
                Debug.error(e);
            }
        }

        saveErrors(request, errors);        
        return mapping.findForward("displayQueryForm");
        
    }
    
    private void header(StringBuffer s, int idx, String text) {
        s.append("<td class='WebTableHeader'><i>");
        if (text==null || text.length()<=0)
{'fraction_non_alphanumeric': 0.12448687350835322, 'fraction_numerical': 0.006491646778042959, 'mean_word_length': 2.234331583822167, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '3957238', 'n_tokens_mistral': 2948, 'n_tokens_neox': 2650, 'n_words': 713}