All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jwebmp.plugins.datatable.DataTablesServlet Maven / Gradle / Ivy

There is a newer version: 1.2.2.1-jre17
Show newest version
/*
 * Copyright (C) 2017 GedMarc
 *
 * 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 3 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, see .
 */

package com.jwebmp.plugins.datatable;

import com.google.inject.Singleton;
import com.jwebmp.core.base.servlets.JWDefaultServlet;
import com.guicedee.guicedinjection.GuiceContext;
import com.guicedee.guicedservlets.GuicedServletKeys;
import com.guicedee.logger.LogFactory;
import com.jwebmp.plugins.datatable.events.DataTableDataFetchEvent;
import com.jwebmp.plugins.datatable.search.DataTableSearchRequest;
import org.apache.commons.lang3.exception.ExceptionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.jwebmp.core.utilities.StaticStrings.*;

@Singleton
public class DataTablesServlet
		extends JWDefaultServlet
{
	private static final Logger log = LogFactory.getInstance()
	                                            .getLogger("DataTablesServlet");

	@Override
	@SuppressWarnings("unchecked")
	public void perform()
	{
		HttpServletRequest request = GuiceContext.get(GuicedServletKeys.getHttpServletRequestKey());
		StringBuilder output = new StringBuilder();
		Set> allEvents = new HashSet(GuiceContext.instance().getScanResult()
		                                                                                                                   .getSubclasses(DataTableDataFetchEvent.class.getCanonicalName()).loadClasses());
		Map params = request.getParameterMap();
		String className = params.get("c")[0];
		allEvents.removeIf(a -> !a.getCanonicalName()
		                          .equals(className.replace(CHAR_UNDERSCORE, CHAR_DOT)));
		if (allEvents.isEmpty())
		{
			writeOutput(output, HTML_HEADER_JAVASCRIPT, UTF8_CHARSET);
			DataTablesServlet.log.fine("DataTablesServlet could not find any specified data search class");
		}
		else
		{
			DataTableSearchRequest searchRequest = new DataTableSearchRequest();
			searchRequest.fromRequestMap(params);
			try
			{
				Class event = allEvents.iterator()
				                                                          .next();
				DataTableDataFetchEvent dtd = GuiceContext.get(event);
				DataTableData d = dtd.returnData(searchRequest);
				output.append(d.toString());
				writeOutput(output, HTML_HEADER_JSON, UTF8_CHARSET);
			}
			catch (Exception e)
			{
				output.append(ExceptionUtils.getStackTrace(e));
				writeOutput(output, HTML_HEADER_JAVASCRIPT, UTF8_CHARSET);
				DataTablesServlet.log.log(Level.SEVERE, "Unable to execute datatables ajax data fetch", e);
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy