<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="PermissionsByUser" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
	<parameter name="RolesOfLoggedInUser" class="java.util.Collection"/>
	<queryString>
		<![CDATA[SELECT
     perm.id as jiobjectpermission_id,
     folder.uri AS jiresourcefolder_uri,
     null as jiresource_name,
     role.id as jirole_id,
     role.rolename AS jirole_rolename,
     perm.permissionMask as permission,
     usr.id as jiuser_id,
     usr.username AS jiuser_username
FROM
     JIObjectPermission perm
     INNER JOIN JIResourceFolder folder ON perm.uri = concat('repo:', folder.uri)
     inner JOIN JIRole role ON role.id = perm.recipientobjectid and
             perm.recipientobjectClass = 'com.jaspersoft.jasperserver.api.metadata.user.domain.impl.hibernate.RepoRole'
     inner join JIUserRole userrole on role.id = userrole.roleId
     inner JOIN JIUser usr ON usr.id = userrole.userId
where $X{IN, role.rolename, RolesOfLoggedInUser}

union

SELECT
     perm.id as jiobjectpermission_id,
     folder.uri AS jiresourcefolder_uri,
     res.name as jiresource_name,
     role.id as jirole_id,
     role.rolename AS jirole_rolename,
     perm.permissionMask as permission,
     usr.id as jiuser_id,
     usr.username AS jiuser_username
FROM
     JIResource res
     INNER JOIN JIResourceFolder folder ON res.parent_folder = folder.id
     inner join JIObjectPermission perm ON perm.uri = concat(concat('repo:', folder.uri), concat('/', res.name))
     inner JOIN JIRole role ON role.id = perm.recipientobjectid and
             perm.recipientobjectClass = 'com.jaspersoft.jasperserver.api.metadata.user.domain.impl.hibernate.RepoRole'
     inner join JIUserRole userrole on role.id = userrole.roleId
     inner JOIN JIUser usr ON usr.id = userrole.userId
where $X{IN, role.rolename, RolesOfLoggedInUser}


order by jiresourcefolder_uri, jiresource_name, jirole_rolename desc, jiuser_username]]>
	</queryString>
	<field name="jiresourcefolder_uri" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="jiresource_name" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="jirole_rolename" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="permission" class="java.lang.Integer">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="jiuser_username" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<group name="jiresourcefolder_uri">
		<groupExpression><![CDATA[$F{jiresourcefolder_uri}]]></groupExpression>
		<groupHeader>
			<band height="33">
				<staticText>
					<reportElement mode="Opaque" x="0" y="0" width="100" height="32" forecolor="#666666" backcolor="#E6E6E6"/>
					<textElement>
						<font size="12"/>
					</textElement>
					<text><![CDATA[Folder]]></text>
				</staticText>
				<textField>
					<reportElement mode="Opaque" x="100" y="0" width="455" height="32" forecolor="#006699" backcolor="#E6E6E6"/>
					<textElement>
						<font size="12" isBold="true"/>
					</textElement>
					<textFieldExpression class="java.lang.String"><![CDATA[$F{jiresourcefolder_uri}]]></textFieldExpression>
				</textField>
				<line>
					<reportElement x="-20" y="32" width="595" height="1" forecolor="#666666"/>
				</line>
			</band>
		</groupHeader>
		<groupFooter>
			<band/>
		</groupFooter>
	</group>
	<group name="jiresource_name">
		<groupExpression><![CDATA[$F{jiresource_name}]]></groupExpression>
		<groupHeader>
			<band height="32">
				<printWhenExpression><![CDATA[Boolean.valueOf($F{jiresource_name} != null)]]></printWhenExpression>
				<textField>
					<reportElement mode="Opaque" x="100" y="0" width="455" height="32" forecolor="#006699" backcolor="#E6E6E6"/>
					<textElement>
						<font size="12" isBold="true"/>
					</textElement>
					<textFieldExpression class="java.lang.String"><![CDATA[$F{jiresourcefolder_uri} + "/" + $F{jiresource_name}]]></textFieldExpression>
				</textField>
				<staticText>
					<reportElement mode="Opaque" x="0" y="0" width="100" height="32" forecolor="#666666" backcolor="#E6E6E6"/>
					<textElement>
						<font size="12"/>
					</textElement>
					<text><![CDATA[Resource Name]]></text>
				</staticText>
			</band>
		</groupHeader>
		<groupFooter>
			<band/>
		</groupFooter>
	</group>
	<group name="jirole_rolename">
		<groupExpression><![CDATA[$F{jirole_rolename}]]></groupExpression>
		<groupHeader>
			<band height="51">
				<textField>
					<reportElement x="100" y="0" width="299" height="32" forecolor="#006699"/>
					<textElement>
						<font size="12" isBold="true"/>
					</textElement>
					<textFieldExpression class="java.lang.String"><![CDATA[$F{jirole_rolename} != null ? $F{jirole_rolename} : "User level"]]></textFieldExpression>
				</textField>
				<staticText>
					<reportElement x="0" y="0" width="100" height="20" forecolor="#666666"/>
					<textElement>
						<font size="12"/>
					</textElement>
					<text><![CDATA[Role]]></text>
				</staticText>
				<staticText>
					<reportElement x="0" y="29" width="100" height="20" forecolor="#666666"/>
					<textElement>
						<font size="12"/>
					</textElement>
					<text><![CDATA[User]]></text>
				</staticText>
				<textField isStretchWithOverflow="true">
					<reportElement x="436" y="0" width="106" height="20">
						<printWhenExpression><![CDATA[Boolean.valueOf($F{jirole_rolename} != null)]]></printWhenExpression>
					</reportElement>
					<textElement>
						<font size="10"/>
					</textElement>
					<textFieldExpression class="java.lang.Integer"><![CDATA[$F{permission}]]></textFieldExpression>
				</textField>
			</band>
		</groupHeader>
		<groupFooter>
			<band height="10">
				<line>
					<reportElement x="-20" y="9" width="595" height="1" forecolor="#666666"/>
				</line>
			</band>
		</groupFooter>
	</group>
	<group name="jiuser_username">
		<groupExpression><![CDATA[$F{jiuser_username}]]></groupExpression>
		<groupHeader>
			<band/>
		</groupHeader>
		<groupFooter>
			<band/>
		</groupFooter>
	</group>
	<background>
		<band/>
	</background>
	<title>
		<band height="60">
			<frame>
				<reportElement mode="Opaque" x="-20" y="-20" width="595" height="55" backcolor="#006699"/>
				<textField>
					<reportElement mode="Transparent" x="20" y="20" width="555" height="20" forecolor="#FFFFFF" backcolor="#FFFFFF"/>
					<textElement textAlignment="Center" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
						<font fontName="SansSerif" size="14" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
					</textElement>
					<textFieldExpression class="java.lang.String"><![CDATA["Permissions By User and Role for Roles"]]></textFieldExpression>
				</textField>
			</frame>
			<textField>
				<reportElement x="0" y="40" width="309" height="20"/>
				<textElement/>
				<textFieldExpression class="java.lang.String"><![CDATA["" + $P{RolesOfLoggedInUser}]]></textFieldExpression>
			</textField>
		</band>
	</title>
	<pageHeader>
		<band/>
	</pageHeader>
	<columnHeader>
		<band height="21">
			<line>
				<reportElement x="-20" y="20" width="595" height="1" forecolor="#666666"/>
			</line>
			<staticText>
				<reportElement mode="Opaque" x="431" y="0" width="119" height="20" forecolor="#006699" backcolor="#E6E6E6"/>
				<textElement>
					<font size="14" isBold="true"/>
				</textElement>
				<text><![CDATA[permission]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="20">
			<textField isStretchWithOverflow="true">
				<reportElement x="436" y="0" width="106" height="20">
					<printWhenExpression><![CDATA[Boolean.valueOf($F{jirole_rolename} == null)]]></printWhenExpression>
				</reportElement>
				<textElement>
					<font size="10"/>
				</textElement>
				<textFieldExpression class="java.lang.Integer"><![CDATA[$F{permission}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="100" y="0" width="187" height="19" forecolor="#006699"/>
				<textElement>
					<font size="10" isBold="true"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{jiuser_username}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<columnFooter>
		<band/>
	</columnFooter>
	<pageFooter>
		<band height="17">
			<textField>
				<reportElement mode="Opaque" x="0" y="4" width="515" height="13" backcolor="#E6E6E6"/>
				<textElement textAlignment="Right"/>
				<textFieldExpression class="java.lang.String"><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
			</textField>
			<textField evaluationTime="Report">
				<reportElement mode="Opaque" x="515" y="4" width="40" height="13" backcolor="#E6E6E6"/>
				<textElement/>
				<textFieldExpression class="java.lang.String"><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
			</textField>
			<textField pattern="EEEEE dd MMMMM yyyy">
				<reportElement x="0" y="4" width="100" height="13"/>
				<textElement/>
				<textFieldExpression class="java.util.Date"><![CDATA[new java.util.Date()]]></textFieldExpression>
			</textField>
		</band>
	</pageFooter>
	<summary>
		<band/>
	</summary>
</jasperReport>
