<commit_before>package org.apache.clerezza.platform.typerendering.gui

import org.apache.clerezza.rdf.core.BNode
import org.apache.clerezza.rdf.core.impl.SimpleMGraph
import org.osgi.framework.{BundleActivator, BundleContext, ServiceRegistration}
import scala.collection.JavaConversions._
import org.apache.clerezza.platform.dashboard.GlobalMenuItem
import org.apache.clerezza.platform.dashboard.GlobalMenuItemsProvider
import org.apache.clerezza.platform.typerendering.{TypeRenderlet, RenderletManager}
import java.util.HashSet
import javax.ws.rs._
import org.apache.clerezza.rdf.ontologies.{DC, RDF, PLATFORM}
import org.apache.clerezza.rdf.utils.GraphNode;
import org.apache.clerezza.rdf.scala.utils._

/**
 * Activator for a bundle using Apache Clerezza.
 */
class Activator extends BundleActivator {

	private var renderletsOverview: ServiceRegistration = null
	private var renderletRegistration, menuProviderRegistration: ServiceRegistration = null
	private var bundleContext: BundleContext = null


	final val path = "admin/renderlets/overview"
	@Path(path)
	object RenderletsOverview {
		@GET def get() = {
			val resultMGraph = new SimpleMGraph();
			val preamble = new Preamble(resultMGraph)
			import preamble._
			val resultNode = new GraphNode(new BNode(), resultMGraph);
			resultNode.addProperty(RDF.`type` , Ontology.RenderletOverviewPage);
			resultNode.addProperty(RDF.`type` , PLATFORM.HeadedPage);
			resultNode.addProperty(RDF.`type` , RDF.List);
			val renderletList = resultNode.asList;
			for (sr <- bundleContext.getServiceReferences(classOf[TypeRenderlet].getName, null)) {
				val renderlet = bundleContext.getService(sr).asInstanceOf[TypeRenderlet]
				val rendRes = new BNode()
				rendRes.addProperty(RDF.`type`, Ontology.Renderlet);
				rendRes.addPropertyValue(Ontology.mediaType,
										renderlet.getMediaType.toString)
				if (renderlet.getModePattern != null) rendRes.addPropertyValue(Ontology.modePattern,
										renderlet.getModePattern)
				rendRes.addProperty(Ontology.rdfType,
										renderlet.getRdfType)
				rendRes.addPropertyValue(Ontology.stringRepresentation,
										renderlet.toString)
				rendRes.addPropertyValue(Ontology.providingBundle,
										sr.getBundle.getLocation)
				renderletList.add(rendRes)
			}
			resultNode;
		}
	}

	object MenuProvider extends GlobalMenuItemsProvider {
		override def getMenuItems = {
			val result = new HashSet[GlobalMenuItem]();
			result.add(new GlobalMenuItem("/"+path,"renderlet-overview", "Renderlet Overview", -999, "Administration"))
			result
		}
	}

	/**
	 * called when the bundle is started, this method initializes the provided service
	 */
	def start(context: BundleContext) {
		this.bundleContext = context
		val args = scala.collection.mutable.Map("javax.ws.rs" -> true)
		renderletsOverview = context.registerService(classOf[Object].getName,
													 RenderletsOverview, args)
		val renderlet = new RenderletDescriptionRenderlet<commit_msg>CLEREZZA-498: only showing menu items for users with right to write to the content graph<commit_after>package org.apache.clerezza.platform.typerendering.gui

import org.apache.clerezza.rdf.core.BNode
import org.apache.clerezza.rdf.core.impl.SimpleMGraph
import org.osgi.framework.{BundleActivator, BundleContext, ServiceRegistration}
import scala.collection.JavaConversions._
import org.apache.clerezza.platform.dashboard.GlobalMenuItem
import org.apache.clerezza.platform.dashboard.GlobalMenuItemsProvider
import org.apache.clerezza.platform.typerendering.{TypeRenderlet, RenderletManager}
import java.util.HashSet
import javax.ws.rs._
import org.apache.clerezza.rdf.utils.GraphNode;
import org.apache.clerezza.rdf.scala.utils._
import org.apache.clerezza.rdf.ontologies.{RDF, DC, PLATFORM}
import org.apache.clerezza.rdf.core.access.security.TcPermission
import java.security.{AccessControlException, AccessController}

/**
 * Activator for a bundle using Apache Clerezza.
 */
class Activator extends BundleActivator {

	private var renderletsOverview: ServiceRegistration = null
	private var renderletRegistration, menuProviderRegistration: ServiceRegistration = null
	private var bundleContext: BundleContext = null


	final val path = "admin/renderlets/overview"
	@Path(path)
	object RenderletsOverview {
		@GET def get() = {
			val resultMGraph = new SimpleMGraph();
			val preamble = new Preamble(resultMGraph)
			import preamble._
			val resultNode = new GraphNode(new BNode(), resultMGraph);
			resultNode.addProperty(RDF.`type` , Ontology.RenderletOverviewPage);
			resultNode.addProperty(RDF.`type` , PLATFORM.HeadedPage);
			resultNode.addProperty(RDF.`type` , RDF.List);
			val renderletList = resultNode.asList;
			for (sr <- bundleContext.getServiceReferences(classOf[TypeRenderlet].getName, null)) {
				val renderlet = bundleContext.getService(sr).asInstanceOf[TypeRenderlet]
				val rendRes = new BNode()
				rendRes.addProperty(RDF.`type`, Ontology.Renderlet);
				rendRes.addPropertyValue(Ontology.mediaType,
										renderlet.getMediaType.toString)
				if (renderlet.getModePattern != null) rendRes.addPropertyValue(Ontology.modePattern,
										renderlet.getModePattern)
				rendRes.addProperty(Ontology.rdfType,
										renderlet.getRdfType)
				rendRes.addPropertyValue(Ontology.stringRepresentation,
										renderlet.toString)
				rendRes.addPropertyValue(Ontology.providingBundle,
										sr.getBundle.getLocation)
				renderletList.add(rendRes)
			}
			resultNode;
		}
	}

	object MenuProvider extends GlobalMenuItemsProvider {
		override def getMenuItems: java.util.Set[GlobalMenuItem] = {
			import collection.JavaConversions._
			val result = new HashSet[GlobalMenuItem]();
			try {
				//TODO should have a more general way to say that a user has some administrative priviledges
				AccessController.checkPermission(new TcPermission("http://tpf.localhost/content.graph", "readwrite"))
			}
			catch {
				case e: AccessControlException => {
					return result
				}
			}
			result.add(new GlobalMenuItem("/"+path,"renderlet-overview", "Renderlet Overview", -999, "Administration"))
			result
		}
	}

	/**
	 * called when the bundle is started, this method initializes the provided service
	 */
	def start(context: BundleContext) {
		this.bundleContext = context
		val args = scala.collection.mutable.Map("javax.ws.rs" -> true)
		renderletsOverview = context.registerService(classOf[Object].getName,
													 RenderletsOverview, args)
		val renderlet = new RenderletDescriptionRenderlet
{'fraction_non_alphanumeric': 0.09642637091805299, 'fraction_numerical': 0.0013863216266173752, 'mean_word_length': 4.766429840142096, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4554903', 'n_tokens_mistral': 2237, 'n_tokens_neox': 1927, 'n_words': 383}