package ru.semiot.platform.deviceproxyservice.manager;

import java.io.StringReader;

import org.apache.jena.riot.RiotException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class DirectoryService {
	
	private static final Logger logger = LoggerFactory
			.getLogger(DirectoryService.class);
	private static final String LANG = "TURTLE";
    private static final String QUERY_SELECT_SYSTEM = "prefix saref: <http://ontology.tno.nl/saref#> "
			+ "SELECT ?state where{ <${URI_SYSTEM}> saref:hasState ?state }";
    private static final String templateOnState = "prefix saref: <http://ontology.tno.nl/saref#> "
			+ "<${URI_SYSTEM}> saref:hasState saref:OnState.";
    protected static final Query SYSTEM_QUERY = QueryFactory
			.create("prefix saref: <http://ontology.tno.nl/saref#> "
			+ "SELECT ?uri_system ?state where{ ?uri_system saref:hasState ?state }");
	protected static final String QUERY_UPDATE_STATE_SYSTEM = "prefix saref: <http://ontology.tno.nl/saref#> "
			+ "DELETE { <${URI_SYSTEM}> saref:hasState ?x } "
			+ "INSERT { <${URI_SYSTEM}> saref:hasState ${STATE} } "
			+ "WHERE { <${URI_SYSTEM}> saref:hasState ?x }";
	
	DeviceManagerImpl dmi;
	RDFStore rdfStore;
	
	public DirectoryService(DeviceManagerImpl dmi) {
		this.dmi = dmi;
		rdfStore = new RDFStore(dmi);
	}
	
	public void inactiveDevice(String message) {
		try {
			Model description = ModelFactory.createDefaultModel().read(
					new StringReader(message), null, LANG);
			if (!description.isEmpty()) {
				logger.info("Update " + message);
				
				QueryExecution qe = QueryExecutionFactory.create(SYSTEM_QUERY,
						description);
				ResultSet systems = qe.execSelect();
				
				while (systems.hasNext()) {
					QuerySolution qs = systems.next();
					String uriSystem = qs.getResource("uri_system").getURI();
					String state = qs.getResource("state").getURI();
					if(uriSystem != null && state != null) {
						rdfStore.update(QUERY_UPDATE_STATE_SYSTEM.replace("${URI_SYSTEM}", uriSystem)
							.replace("${STATE}", state)); 
					}
				}
			} else {
				logger.warn("Received an empty message or in a wrong format!");
			}
		} catch (RiotException ex) {
			logger.warn(ex.getMessage(), ex);
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}
	}
    
    public void registerDevice(String message) {
    	try {
            Model description = ModelFactory.createDefaultModel()
                    .read(new StringReader(message), null, LANG);
            if (!description.isEmpty()) {
            	QueryExecution qe = QueryExecutionFactory.create(SYSTEM_QUERY, 
						description);
				ResultSet systems = qe.execSelect();

				while (systems.hasNext()) {
					QuerySolution qs = systems.next();
					String uriSystem = qs.getResource("uri_system").getURI();
					if (uriSystem != null) {
						ResultSet rs = rdfStore.select(QUERY_SELECT_SYSTEM.replace("${URI_SYSTEM}", uriSystem));
						if(rs.hasNext()) {
							rdfStore.update(QUERY_UPDATE_STATE_SYSTEM.replace("${URI_SYSTEM}", uriSystem)
									.replace("${STATE}", "saref:OnState"));
							// for interface
							WAMPClient.getInstance().publish(dmi.getTopicInactive(), 
									templateOnState.replace("${URI_SYSTEM}", uriSystem));
						} else {
							rdfStore.save(description);
							WAMPClient.getInstance().publish(dmi.getTopicNewAndObserving(), message);
						}
					}
				}
            } else {
                logger.warn("Received an empty message or in a wrong format!");
            }
        } catch (RiotException ex) {
            logger.warn(ex.getMessage(), ex);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
}

@@ -55,145 +55,8 @@
 %0D%0A%0D%0A
-import java.io.StringReader;%0D%0A%0D%0Aimport org.apache.jena.riot.RiotException;%0D%0Aimport org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory;%0D%0A%0D%0A
 impo
@@ -405,16 +405,149 @@
 actory;%0D
+%0Aimport java.io.StringReader;%0D%0Aimport org.apache.jena.riot.RiotException;%0D%0Aimport org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory;%0D
 %0A%0D%0Apubli
@@ -1733,16 +1733,42 @@
 age) %7B%0D%0A
+%09%09String request = null;%0D%0A
 %09%09try %7B%0D
@@ -2296,72 +2296,18 @@
 %09%09%09%09
-if(uriSystem != null && state != null) %7B%0D%0A%09%09%09%09%09%09rdfStore.update(
+request = 
 QUER
@@ -2399,16 +2399,95 @@
 , state)
+;%0D%0A%09%09%09%09%09if(uriSystem != null && state != null) %7B%0D%0A%09%09%09%09%09%09rdfStore.update(request
 ); %0D%0A%09%09%09
@@ -2676,32 +2676,153 @@
 xception ex) %7B%0D%0A
+%09%09%09logger.info(%22Exception with string: %22 + ((request!=null && !request.isEmpty())?request:%22request message is empty%22));%0D%0A
 %09%09%09logger.error(

Update log message for exception