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

org.structr.websocket.command.AutocompleteCommand Maven / Gradle / Ivy

Go to download

Structr is an open source framework based on the popular Neo4j graph database.

The newest version!
/**
 * Copyright (C) 2010-2016 Structr GmbH
 *
 * This file is part of Structr .
 *
 * Structr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Structr 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Structr.  If not, see .
 */
package org.structr.websocket.command;

import org.structr.core.GraphObject;
import org.structr.websocket.message.WebSocketMessage;

//~--- JDK imports ------------------------------------------------------------

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.structr.autocomplete.AbstractHintProvider;
import org.structr.autocomplete.JavascriptHintProvider;
import org.structr.autocomplete.PlaintextHintProvider;
import org.structr.common.error.FrameworkException;
import org.structr.core.GraphObjectMap;
import org.structr.core.app.StructrApp;
import org.structr.core.property.GenericProperty;
import org.structr.core.property.Property;
import org.structr.websocket.StructrWebSocket;

//~--- classes ----------------------------------------------------------------

/**
 * Websocket command to return the children of the given node
 *
 *
 */
public class AutocompleteCommand extends AbstractCommand {

	private static final Logger logger                                   = Logger.getLogger(AutocompleteCommand.class.getName());
	private static final Property> list             = new GenericProperty("list");
	private static final Map hintProviders = new HashMap<>();

	static {

		StructrWebSocket.addCommand(AutocompleteCommand.class);

		hintProviders.put("text/plain",             new PlaintextHintProvider());
		hintProviders.put("text/javascript",        new JavascriptHintProvider());
		hintProviders.put("application/javascript", new JavascriptHintProvider());
	}

	@Override
	public void processMessage(final WebSocketMessage webSocketData) {

		final Map data    = webSocketData.getNodeData();
		final String id                   = webSocketData.getId();
		final List result    = new LinkedList<>();
		final String contentType          = getOrDefault(data.get("contentType"), "text/plain");

		if (contentType != null) {

			final AbstractHintProvider hintProvider = hintProviders.get(contentType);
			if (hintProvider != null) {

				final String currentToken  = getAndTrim(data.get("currentToken"));
				final String previousToken = getAndTrim(data.get("previousToken"));
				final String thirdToken    = getAndTrim(data.get("thirdToken"));
				final String type          = getAndTrim(data.get("type"));
				final int cursorPosition   = getInt(data.get("cursorPosition"));
				final int line             = getInt(data.get("line"));

				try {

					final List hints = hintProvider.getHints(StructrApp.getInstance().get(id), type, currentToken, previousToken, thirdToken, line, cursorPosition);
					result.addAll(hints);

				} catch(FrameworkException fex) {
					logger.log(Level.WARNING, "", fex);
				}

			} else {

				logger.log(Level.WARNING, "No HintProvider for content type {0}, ignoring.", contentType);
			}

		} else {

			logger.log(Level.WARNING, "No content type for AutocompleteCommand, ignoring.");
		}

		// set full result list
		webSocketData.setResult(result);
		webSocketData.setRawResultCount(result.size());
		getWebSocket().send(webSocketData, true);
	}

	@Override
	public String getCommand() {

		return "AUTOCOMPLETE";
	}

	// ----- private methods -----
	private String getAndTrim(final Object source) {

		if (source != null && source instanceof String) {
			return ((String)source).trim();
		}

		return "";
	}

	private String getOrDefault(final Object source, final String defaultValue) {

		if (source != null && source instanceof String) {
			return ((String)source).trim();
		}

		return defaultValue;
	}

	private int getInt(final Object source) {

		if (source != null) {

			if (source instanceof Number) {
				return ((Number)source).intValue();
			}

			if (source instanceof String) {
				try { return Integer.parseInt(source.toString()); } catch (Throwable t) {}
			}
		}

		return -1;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy