<?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="Subscribed Documents" pageWidth="842" pageHeight="595" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="782" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20" whenResourceMissingType="Key">
	<property name="ireport.scriptlethandling" value="0"/>
	<property name="ireport.encoding" value="UTF-8"/>
	<property name="ireport.zoom" value="1.5"/>
	<property name="ireport.x" value="355"/>
	<property name="ireport.y" value="0"/>
	<import value="net.sf.jasperreports.engine.*"/>
	<import value="java.util.*"/>
	<import value="net.sf.jasperreports.engine.data.*"/>
	<parameter name="host" class="java.lang.String"/>
	<queryString>
		<![CDATA[import java.util.*;
import com.openkm.api.OKMDocument;
import com.openkm.api.OKMRepository;
import com.openkm.dao.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import com.openkm.dao.bean.NodeDocument;


int COLS = 10;

class PathSorter implements Comparator {
    public int compare(Object obj1, Object obj2) {
		String path1 = (String) obj1;
		String path2 = (String) obj2;
		String[] paths1 = path1.split("/");
		String[] paths2 = path2.split("/");
		for (int i=0; i<paths1.length-1; i++) {
			if (i<paths2.length) {
				if (!paths1[i].equals(paths2[i])) {
					return paths1[i].compareTo(paths2[i]);
				}
			} else {
				return 1;
			}
		}
		if (paths2.length == paths1.length) {
			return paths1[paths1.length-1].compareTo(paths2[paths2.length-1]);
		} else if (paths2.length > paths1.length) {
			return -1;
		} else {
			return 1;
		}
    }
}

List al = new ArrayList();
List pathList = new ArrayList();
String qs = "from NodeDocument";
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Query q = session.createQuery(qs);
List docVersionList = q.list();
HibernateUtil.commit(tx);
 
for (NodeDocument ndv : docVersionList) {
    String path = OKMRepository.getInstance().getNodePath(null,ndv.getUuid());
    if (path.startsWith("/okm:root/")) {
        pathList.add(path);
    }
    
}
HibernateUtil.close(session);

// Ordering by path
Collections.sort(pathList, new PathSorter());

// Initialize older map
Map older = new HashMap();
for (int i=1; i<COLS; i++) {
    older.put("path"+i,"");
}

// Iterating paths
for (String docPath : pathList) {
    Map dob = new HashMap();

    String[] paths = docPath.split("/");
    for (int i=1; i<paths.length; i++) {
        String path = paths[i];
        if (i<(paths.length-1)) {
            // Repeat info will not appearing
            if (!older.get("path"+i).equals((paths[i]+"/"))) {
                dob.put("path"+i, (paths[i]+"/"));
                older.put("path"+i, (paths[i]+"/"));
            } else {
                dob.put("path"+i, "");
            }

        } else {
            dob.put("path"+i, paths[i]);
        }
    }


    // Set empty path values
    for (int i=paths.length; i<COLS; i++) {
        dob.put("path"+i,"");
        older.put("path"+i,"");
    }


    // Preview
    for (int i=1; i<COLS; i++) {
        print("path"+i+">"+dob.get("path"+i));
    }
    print("<br/>");

    al.add(dob);

}

return al]]>
	</queryString>
	<field name="path1" class="java.lang.String">
		<fieldDescription><![CDATA[path]]></fieldDescription>
	</field>
	<field name="path2" class="java.lang.String"/>
	<field name="path3" class="java.lang.String"/>
	<field name="path4" class="java.lang.String"/>
	<field name="path5" class="java.lang.String"/>
	<field name="path6" class="java.lang.String"/>
	<field name="path7" class="java.lang.String"/>
	<field name="path8" class="java.lang.String"/>
	<field name="path9" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="44" splitType="Stretch">
			<image>
				<reportElement key="image-1" x="631" y="0" width="150" height="35"/>
				<imageExpression class="java.lang.String"><![CDATA["http://localhost:8080/OpenKM/logo/report"]]></imageExpression>
			</image>
			<textField pattern="yyyy/MM/dd" isBlankWhenNull="false">
				<reportElement key="textField-13" x="0" y="0" width="65" height="10" forecolor="#010101"/>
				<box>
					<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
					<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
					<bottomPen lineWidth="0.0" lineColor="#000000"/>
					<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
				</box>
				<textElement verticalAlignment="Middle">
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H" size="8" isBold="true" isUnderline="false"/>
				</textElement>
				<textFieldExpression class="java.util.Date"><![CDATA[new Date()]]></textFieldExpression>
			</textField>
			<textField pattern="h.mm.ss a" isBlankWhenNull="false">
				<reportElement key="textField-14" x="0" y="10" width="65" height="10" forecolor="#010101"/>
				<box>
					<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
					<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
					<bottomPen lineWidth="0.0" lineColor="#000000"/>
					<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
				</box>
				<textElement verticalAlignment="Middle">
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H" size="8" isBold="true" isUnderline="false"/>
				</textElement>
				<textFieldExpression class="java.util.Date"><![CDATA[new Date()]]></textFieldExpression>
			</textField>
			<line>
				<reportElement key="line-3" x="1" y="39" width="781" height="1" forecolor="#010101"/>
				<graphicElement fill="Solid"/>
			</line>
			<staticText>
				<reportElement x="256" y="0" width="297" height="44"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H" size="22" isBold="true" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[Accessible Documents]]></text>
			</staticText>
		</band>
	</title>
	<pageHeader>
		<band splitType="Stretch"/>
	</pageHeader>
	<columnHeader>
		<band height="30" splitType="Stretch">
			<line>
				<reportElement key="line-5" x="1" y="28" width="780" height="1" forecolor="#010101"/>
			</line>
			<staticText>
				<reportElement x="1" y="13" width="781" height="14"/>
				<textElement>
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H" isBold="true" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[Document]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="18" splitType="Stretch">
			<textField isStretchWithOverflow="true" isBlankWhenNull="false">
				<reportElement key="textField" x="1" y="0" width="47" height="18"/>
				<textElement>
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{path1}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="false">
				<reportElement key="textField" x="48" y="0" width="74" height="18"/>
				<textElement>
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{path2}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="false">
				<reportElement key="textField" x="122" y="0" width="67" height="18"/>
				<textElement>
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{path3}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="false">
				<reportElement key="textField" x="188" y="0" width="68" height="18"/>
				<textElement>
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{path4}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="false">
				<reportElement key="textField" x="256" y="0" width="68" height="18"/>
				<textElement>
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{path5}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="false">
				<reportElement key="textField" x="324" y="0" width="68" height="18"/>
				<textElement>
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{path6}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="false">
				<reportElement key="textField" x="392" y="0" width="68" height="18"/>
				<textElement>
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{path7}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="false">
				<reportElement key="textField" x="460" y="0" width="68" height="18"/>
				<textElement>
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{path8}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="false">
				<reportElement key="textField" x="528" y="0" width="68" height="18"/>
				<textElement>
					<font fontName="Arial" pdfFontName="unicode.ttf" pdfEncoding="Identity-H"/>
				</textElement>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{path9}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<columnFooter>
		<band splitType="Stretch"/>
	</columnFooter>
	<pageFooter>
		<band height="30" splitType="Stretch">
			<line>
				<reportElement key="line-2" x="1" y="4" width="780" height="1" forecolor="#010101" backcolor="#FFFFFF"/>
				<graphicElement fill="Solid"/>
			</line>
		</band>
	</pageFooter>
	<summary>
		<band splitType="Stretch"/>
	</summary>
</jasperReport>
