/**
@create on:		19/07/2015

@author:		Konstantinos Pechlivanis

@project:		The work has been undertaken under the National Center for Scientific Research "Demokritos"

@summary:		Giving the DOI of publications, extract from hmtl code of publications's page the needs elements
				in order to filling in the publication form on www.iit.demokrtitos.gr
**/

package iit.server.com.ServerServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.simple.JSONObject;

public class sender extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	/***************************************************
     * URL: /sender
     * doPost(): receives data about DOI and type of publication, parse it and send back as JSON Array
     ****************************************************/

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// get received text data from request
		String DOI = request.getParameter("doiANDtype").split(",")[0];
		String field_pub_publication_type_value = request.getParameter("doiANDtype").split(",")[1];
		
		JSONObject elements = new JSONObject();
		String publisher;
		
		if (DOI.contains("springer.com"))
			publisher = "Springer";	// the publisher of the publications
		else 
			publisher = "Not available";

		try {
			URL url = new URL(DOI); // the DOI of publication which have to insert
			BufferedReader bfr = new BufferedReader(new InputStreamReader(url.openStream()));
			
			Publishers myPubl = null;
			if (publisher.equals("Springer")){
				myPubl = new Springer(field_pub_publication_type_value);
			}
	
			if (field_pub_publication_type_value.equals("Book / Editorial")) {
				myPubl.bookEditorial(bfr);
	
			} else if (field_pub_publication_type_value.equals("Book Chapter")) {
				myPubl.bookChapter(bfr);
	
			} else if (field_pub_publication_type_value.equals("Journal")) {
				myPubl.journal(bfr);
				
			} else if (field_pub_publication_type_value.equals("Conference Proceedings (fully refereed)") || field_pub_publication_type_value.equals("Conference Proceedings (partially refereed)")) {
				myPubl.conferenceProceedings(bfr);
				
			} else if (field_pub_publication_type_value.equals("Popularised Publication")) {
				myPubl.popularisedPublication(bfr);
				
			} else if (field_pub_publication_type_value.equals("Phd Thesis") || field_pub_publication_type_value.equals("MSc/BSc Thesis")) {
				myPubl.phdMscThesis(bfr);
				
			} else if (field_pub_publication_type_value.equals("Project Technical Report") || field_pub_publication_type_value.equals("Internal (Demokritos) Report")) {
				myPubl.technInterReport(bfr);
			}
			
			// replace last character "," with string "and" or with ""
			if (Publishers.field_pub_authors_value!=""){
				Publishers.field_pub_authors_value = Publishers.field_pub_authors_value.substring(0, Publishers.field_pub_authors_value.length()-2);// remove last useless ","

				if (Publishers.field_pub_authors_value.contains(",")){
					int index = Publishers.field_pub_authors_value.lastIndexOf(",");
					Publishers.field_pub_authors_value = Publishers.field_pub_authors_value.substring(0, index) + " and " + Publishers.field_pub_authors_value.substring(index+2, Publishers.field_pub_authors_value.length());
				}
			}
		    // replace last character "," with string "and" or with ""
			if (Publishers.field_pub_editor_value!=""){
				Publishers.field_pub_editor_value = Publishers.field_pub_editor_value.substring(0, Publishers.field_pub_editor_value.length()-2);// remove last useless ","
		        if (Publishers.field_pub_editor_value.contains(",")){
		        	int index = Publishers.field_pub_editor_value.lastIndexOf(",");
		        	Publishers.field_pub_editor_value = Publishers.field_pub_editor_value.substring(0, index) + " and " + Publishers.field_pub_editor_value.substring(index+2, Publishers.field_pub_editor_value.length());
		        }
			}

			Publishers.field_pub_online_title = Publishers.field_pub_title_value;
			elements.put("field_pub_publication_type_value", field_pub_publication_type_value);elements.put("language", Publishers.language);
		    elements.put("field_pub_day_published_value", Publishers.field_pub_day_published_value);elements.put("field_pub_month_published_value", Publishers.field_pub_month_published_value);
			elements.put("field_pub_year_published_value", Publishers.field_pub_year_published_value);
		    elements.put("field_pub_title_value", Publishers.field_pub_title_value);elements.put("field_pub_first_author_value", Publishers.field_pub_first_author_value);
			elements.put("field_pub_authors_value", Publishers.field_pub_authors_value);elements.put("field_pub_abstract_value", Publishers.field_pub_abstract_value);
			elements.put("field_pub_online_title", Publishers.field_pub_online_title);elements.put("field_pub_online_url", Publishers.field_pub_online_url);
			elements.put("field_pub_conference_short_name_value", Publishers.field_pub_conference_short_name_value);elements.put("field_pub_conference_full_name_value", Publishers.field_pub_conference_full_name_value);
			elements.put("field_pub_conference_country_value", Publishers.field_pub_conference_country_value);elements.put("field_pub_conference_city_value", Publishers.field_pub_conference_city_value);
			elements.put("field_pub_conference_venue_value", Publishers.field_pub_conference_venue_value);elements.put("field_pub_conference_dates_value", Publishers.field_pub_conference_dates_value);
			elements.put("field_pub_conference_dates_value2", Publishers.field_pub_conference_dates_value2);elements.put("field_pub_conference_level_value", Publishers.field_pub_conference_level_value);
		    elements.put("field_pub_name_value", Publishers.field_pub_name_value);elements.put("field_pub_editor_value", Publishers.field_pub_editor_value);
			elements.put("field_pub_publisher_value", Publishers.field_pub_publisher_value);elements.put("field_pub_series_value", Publishers.field_pub_series_value);
			elements.put("field_pub_volume_value", Publishers.field_pub_volume_value);elements.put("field_pub_number_value", Publishers.field_pub_number_value);
			elements.put("field_pub_pages_start_value", Publishers.field_pub_pages_start_value);elements.put("field_pub_pages_end_value", Publishers.field_pub_pages_end_value);
			elements.put("field_pub_isbn_code_value", Publishers.field_pub_isbn_code_value);elements.put("field_pub_issn_code_value", Publishers.field_pub_issn_code_value);
			
		    // Set response type to JSON
		    response.setContentType("application/json");

		    // Send List<Article> as JSON to client
		    PrintWriter pw = response.getWriter();
		    pw.println(elements);

		} catch (MalformedURLException e1) { e1.printStackTrace(); }
	}
}