<commit_before>/*
    Copyright 2015 Australian National Botanic Gardens

    This file is part of NSL services project.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License. You may obtain a copy
    of the License at http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

package au.org.biodiversity.nsl.api

import au.org.biodiversity.nsl.Arrangement
import au.org.biodiversity.nsl.Name
import au.org.biodiversity.nsl.Instance
import au.org.biodiversity.nsl.Namespace
import au.org.biodiversity.nsl.TreeViewService
import grails.converters.JSON
import grails.transaction.Transactional
import grails.validation.Validateable
import org.codehaus.groovy.grails.commons.GrailsApplication

@Transactional
class TreeViewController {
	TreeViewService treeViewService

	def index(TreeViewParam p) {
		return [tree: p.tree, name: p.name]
	}

	def namePlacementPath(TreeViewParam p) {
		render treeViewService.getPathForName(p.tree, p.name) as JSON
	}

	def treeTopPath(TreeViewParam p) {
		render treeViewService.getPathForTree(p.tree) as JSON
	}

	def namePlacementBranch(TreeViewParam p) {
		render treeViewService.getBranchForName(p.tree, p.name) as JSON
	}

	def treeTopBranch(TreeViewParam p) {
		render treeViewService.getBranchForTree(p.tree) as JSON
	}

	def nameInTree(TreeViewParam p) {
		render treeViewService.getNameInTree(p.tree, p.name) as JSON
	}

	def namePlacementInTree(TreeViewParam p) {
		render treeViewService.getNamePlacementInTree(p.tree, p.name) as JSON
	}

	def instancePlacementInTree(TreeViewInstanceParam p) {
		render treeViewService.getInstancePlacementInTree(p.tree, p.instance) as JSON
	}
}

@Validateable
class TreeViewParam {
	GrailsApplication grailsApplication
	String treeLabel
	String nameId

	String toString() {
		return [treeLabel: treeLabel, nameId:nameId].toString()
	}

	Name getName() {
		return nameId ? Name.get(nameId as Long) : null
	}

	Arrangement getTree() {
		return Arrangement.findByNamespaceAndLabel(
				Namespace.findByName(grailsApplication.config.services.classification.namespace),
				treeLabel)
	}

	static constraints = {
		nameId nullable: true
		name nullable: true
	}
}

@Validateable
class TreeViewInstanceParam {
	GrailsApplication grailsApplication
	String treeLabel
	String instanceId

	String toString() {
		return [treeLabel: treeLabel, instanceId:instanceId].toString()
	}

	Instance getInstance() {
		return instanceId ? Instance.get(instanceId as Long) : null
	}

	Arrangement getTree() {
		return Arrangement.findByNamespaceAndLabel(
				Namespace.findByName(grailsApplication.config.services.classification.namespace),
				treeLabel)
	}

	static constraints = {
		instanceId nullable: true
		instance nullable: true
	}
}<commit_msg>NSL-1558 - Add namespace to tree Tree view now works for the editor<commit_after>/*
    Copyright 2015 Australian National Botanic Gardens

    This file is part of NSL services project.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License. You may obtain a copy
    of the License at http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

package au.org.biodiversity.nsl.api

import au.org.biodiversity.nsl.Arrangement
import au.org.biodiversity.nsl.Name
import au.org.biodiversity.nsl.Instance
import au.org.biodiversity.nsl.Namespace
import au.org.biodiversity.nsl.TreeViewService
import grails.converters.JSON
import grails.transaction.Transactional
import grails.validation.Validateable
import org.codehaus.groovy.grails.commons.GrailsApplication

@Transactional
class TreeViewController {
	GrailsApplication grailsApplication
	TreeViewService treeViewService

	def index(TreeViewParam p) {
		return [tree: p.getTree(grailsApplication), name: p.name]
	}

	def namePlacementPath(TreeViewParam p) {
		render treeViewService.getPathForName(getTree(p), p.name) as JSON
	}

	def treeTopPath(TreeViewParam p) {
		render treeViewService.getPathForTree(getTree(p)) as JSON
	}

	def namePlacementBranch(TreeViewParam p) {
		render treeViewService.getBranchForName(getTree(p), p.name) as JSON
	}

	def treeTopBranch(TreeViewParam p) {
		render treeViewService.getBranchForTree(getTree(p)) as JSON
	}

	def nameInTree(TreeViewParam p) {
		render treeViewService.getNameInTree(getTree(p), p.name) as JSON
	}

	def namePlacementInTree(TreeViewParam p) {
		render treeViewService.getNamePlacementInTree(getTree(p), p.name) as JSON
	}

	def instancePlacementInTree(TreeViewInstanceParam p) {
		render treeViewService.getInstancePlacementInTree(getTree(p), p.instance) as JSON
	}

    private Arrangement getTree(TreeViewParam p) {
        return Arrangement.findByNamespaceAndLabel(
                Namespace.findByName(grailsApplication.config.services.classification.namespace),
                p.treeLabel)
    }

    private Arrangement getTree(TreeViewInstanceParam p) {
        return Arrangement.findByNamespaceAndLabel(
                Namespace.findByName(grailsApplication.config.services.classification.namespace),
                p.treeLabel)
    }
}

@Validateable
class TreeViewParam {
	String treeLabel
	String nameId

	String toString() {
		return [treeLabel: treeLabel, nameId:nameId].toString()
	}

	Name getName() {
		return nameId ? Name.get(nameId as Long) : null
	}

	static constraints = {
		nameId nullable: true
		name nullable: true
	}
}

@Validateable
class TreeViewInstanceParam {
	String treeLabel
	String instanceId

	String toString() {
		return [treeLabel: treeLabel, instanceId:instanceId].toString()
	}

	Instance getInstance() {
		return instanceId ? Instance.get(instanceId as Long) : null
	}

	static constraints = {
		instanceId nullable: true
		instance nullable: true
	}
}
{'fraction_non_alphanumeric': 0.07531576485264307, 'fraction_numerical': 0.003118665211289568, 'mean_word_length': 4.515047291487532, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6989999', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1912, 'n_words': 521}