Warning, /pim/akonadi/autotests/server/dbpopulator.xsl is written in an unsupported language. File is not indexed.

0001 <!--
0002     SPDX-FileCopyrightText: 2014 - Daniel Vrátil <dvratil@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 -->
0006 
0007 
0008 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
0009                 xmlns:exsl="http://exslt.org/common"
0010                 extension-element-prefixes="exsl"
0011                 version="1.0">
0012 
0013 <xsl:output method="text" encoding="utf-8"/>
0014 
0015 <xsl:variable name="schema" select="document('../../src/server/storage/akonadidb.xml')/database"/>
0016 <xsl:variable name="data" select="." />
0017 
0018 <xsl:template name="first-upper-case">
0019   <xsl:param name="name"/>
0020   <xsl:value-of select="concat(translate(substring($name, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring($name, 2))"/>
0021 </xsl:template>
0022 
0023 <xsl:template name="first-lower-case">
0024   <xsl:param name="name"/>
0025   <xsl:value-of select="concat(translate(substring($name, 1, 1), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), substring($name, 2))"/>
0026 </xsl:template>
0027 
0028 <xsl:template name="entity-variable-name">
0029   <xsl:call-template name="first-lower-case">
0030     <xsl:with-param name="name" select="local-name()" />
0031   </xsl:call-template>
0032   <xsl:value-of select="generate-id()" />
0033 </xsl:template>
0034 
0035 <xsl:template name="name-to-setter">
0036   <xsl:text>set</xsl:text><xsl:call-template name="first-upper-case"><xsl:with-param name="name" select="@name"/></xsl:call-template>
0037 </xsl:template>
0038 
0039 <xsl:template name="translate-enum-value">
0040   <xsl:param name="value" />
0041   <xsl:param name="enumType" />
0042   <xsl:param name="table" />
0043 
0044   <xsl:value-of select="$table"/>::<xsl:value-of select="substring($value, string-length($enumType) + 3)" />
0045 </xsl:template>
0046 
0047 <xsl:template name="parse-entities-recursively">
0048   <xsl:param name="attributes" />
0049   <xsl:for-each select="*">
0050     <xsl:choose>
0051       <xsl:when test="local-name() = 'MimeType'">
0052         <xsl:if test="parent::Collection">
0053           <xsl:variable name="mt" select="@name" />
0054           <xsl:variable name="mimeTypeId" select="generate-id(exsl:node-set($attributes)/MimeTypes/*[@id = $mt])"/>
0055           <xsl:variable name="collectionId" select="generate-id(parent::node())" />
0056           <xsl:text>collection</xsl:text><xsl:value-of select="$collectionId"/>.addMimeType(mimeType<xsl:value-of select="$mimeTypeId" /><xsl:text>);
0057 
0058   </xsl:text>
0059         </xsl:if>
0060       </xsl:when>
0061       <xsl:when test="local-name() = 'Flag'">
0062         <xsl:if test="parent::PimItem">
0063           <xsl:variable name="flag" select="@name" />
0064           <xsl:variable name="flagId" select="generate-id(exsl:node-set($attributes)/Flags/*[@id = $flag])"/>
0065           <xsl:variable name="pimItemId" select="generate-id(parent::node())" />
0066           <xsl:text>pimItem</xsl:text><xsl:value-of select="$pimItemId"/>.addFlag(flag<xsl:value-of select="$flagId" /><xsl:text>);
0067 
0068   </xsl:text>
0069         </xsl:if>
0070       </xsl:when>
0071       <xsl:when test="local-name() = 'Tag'">
0072         <xsl:if test="parent::PimItem">
0073           <xsl:variable name="tag" select="@gid" />
0074           <xsl:variable name="tagId" select="generate-id(exsl:node-set($attributes)/Tags/*[@id = $tag])"/>
0075           <xsl:variable name="pimItemId" select="generate-id(parent::node())" />
0076           <xsl:text>pimItem</xsl:text><xsl:value-of select="$pimItemId"/>.addTag(tag<xsl:value-of select="$tagId" /><xsl:text>);
0077 
0078   </xsl:text>
0079         </xsl:if>
0080       </xsl:when>
0081       <xsl:otherwise>
0082         <xsl:call-template name="parse-entity">
0083           <xsl:with-param name="table" select="current()"/>
0084           <xsl:with-param name="attributes" select="$attributes"/>
0085         </xsl:call-template>
0086       </xsl:otherwise>
0087     </xsl:choose>
0088   </xsl:for-each>
0089 </xsl:template>
0090 
0091 <xsl:template name="parse-entity">
0092   <xsl:param name="table"/>
0093   <xsl:param name="attributes"/>
0094   <xsl:variable name="tableName" select="local-name()" />
0095 
0096   <!-- Declare the entity variable //-->
0097   <xsl:variable name="variable">
0098     <xsl:call-template name="entity-variable-name"/>
0099   </xsl:variable>
0100   <xsl:value-of select="concat(local-name(), ' ', $variable)"/><xsl:text>;
0101   </xsl:text>
0102 
0103   <!-- Call setters for all specified values //-->
0104   <xsl:for-each select="$schema/table[@name = $tableName]/column[@name != 'id']">
0105     <xsl:variable name="columnName" select="@name" />
0106     <xsl:variable name="columnValue">
0107       <xsl:choose>
0108         <!-- If value of the current column is explicitly set, then use it //-->
0109         <xsl:when test="$table/@*[local-name() = $columnName]">
0110           <xsl:value-of select="$table/@*[local-name() = $columnName]" />
0111         </xsl:when>
0112         <!-- If value is not specified, but the column has a foreign key, then resolve value from
0113              the referred entity //-->
0114         <xsl:when test="@refTable">
0115           <xsl:variable name="refTable" select="@refTable"/>
0116           <xsl:variable name="refElement" select="generate-id(($table/ancestor::*[local-name() = $refTable][1])[last()])"/>
0117           <xsl:choose>
0118             <!-- Special handling for PimItem.partTypeId //-->
0119             <xsl:when test="$refTable = 'PartType' and @refColumn = 'id'">
0120               <xsl:variable name="partType" select="$table/@partType" />
0121               <xsl:variable name="partTypeId" select="generate-id(exsl:node-set($attributes)/PartTypes/*[@id = $partType])" />
0122               <xsl:text>partType</xsl:text><xsl:value-of select="$partTypeId" /><xsl:text>.id()</xsl:text>
0123             </xsl:when>
0124             <xsl:when test="$refTable = 'MimeType' and @refColumn = 'id'">
0125               <xsl:variable name="mimeType" select="$table/@mimeType" />
0126               <xsl:variable name="mimeTypeId" select="generate-id(exsl:node-set($attributes)/MimeTypes/*[@id = $mimeType])" />
0127               <xsl:text>mimeType</xsl:text><xsl:value-of select="$mimeTypeId"/><xsl:text>.id()</xsl:text>
0128             </xsl:when>
0129             <!-- Get id of entity referred to via refTable and refColumn //-->
0130             <xsl:when test="$refElement">
0131               <xsl:call-template name="first-lower-case">
0132                 <xsl:with-param name="name" select="@refTable"/>
0133               </xsl:call-template>
0134               <xsl:value-of select="$refElement"/><xsl:text>.id()</xsl:text>
0135             </xsl:when>
0136             <!-- Default value (fallback) //-->
0137             <xsl:otherwise>
0138               <xsl:text>NULL</xsl:text>
0139             </xsl:otherwise>
0140           </xsl:choose>
0141         </xsl:when>
0142       </xsl:choose>
0143     </xsl:variable>
0144     <xsl:if test="$columnValue != ''">
0145       <xsl:value-of select="$variable"/>.<xsl:call-template name="name-to-setter" />
0146       <xsl:text>(</xsl:text>
0147       <!-- Handle various default types //-->
0148       <xsl:choose>
0149         <xsl:when test="@type = 'qint64' or @type = 'int' or @type = 'bool'">
0150           <xsl:choose>
0151             <xsl:when test="$columnValue = 'NULL'">
0152               <xsl:text>0</xsl:text>
0153             </xsl:when>
0154             <xsl:otherwise>
0155               <xsl:value-of select="$columnValue" />
0156             </xsl:otherwise>
0157           </xsl:choose>
0158         </xsl:when>
0159         <xsl:when test="@type = 'enum'">
0160           <xsl:choose>
0161             <xsl:when test="$columnValue = 'NULL'">
0162               <xsl:call-template name="translate-enum-value">
0163                 <xsl:with-param name="value"><xsl:value-of select="$schema/table[@name = $tableName]/column[@name = $columnName]/@default"/></xsl:with-param>
0164                 <xsl:with-param name="enumType"><xsl:value-of select="$schema/table[@name = $tableName]/column[@name = $columnName]/@enumType"/></xsl:with-param>
0165                 <xsl:with-param name="table"><xsl:value-of select="$tableName"/></xsl:with-param>
0166               </xsl:call-template>
0167             </xsl:when>
0168             <xsl:otherwise>
0169               <xsl:call-template name="translate-enum-value">
0170                 <xsl:with-param name="value"><xsl:value-of select="$columnValue"/></xsl:with-param>
0171                 <xsl:with-param name="enumType"><xsl:value-of select="$schema/table[@name = $tableName]/column[@name = $columnName]/@enumType"/></xsl:with-param>
0172                 <xsl:with-param name="table"><xsl:value-of select="$tableName"/></xsl:with-param>
0173               </xsl:call-template>
0174             </xsl:otherwise>
0175           </xsl:choose>
0176         </xsl:when>
0177         <xsl:when test="@type = 'QString'">
0178           <xsl:choose>
0179             <xsl:when test="$columnValue = 'NULL'">
0180               <xsl:text>QString()</xsl:text>
0181             </xsl:when>
0182             <xsl:otherwise>
0183               <xsl:text>QStringLiteral("</xsl:text><xsl:value-of select="$columnValue" /><xsl:text>")</xsl:text>
0184             </xsl:otherwise>
0185           </xsl:choose>
0186         </xsl:when>
0187         <xsl:when test="@type = 'QByteArray'">
0188           <xsl:text>"</xsl:text><xsl:value-of select="$columnValue" /><xsl:text>"</xsl:text>
0189         </xsl:when>
0190         <xsl:when test="@type = 'QDateTime'">
0191           <xsl:text>QDateTime::fromString(QStringLiteral("</xsl:text><xsl:value-of select="$columnValue" /><xsl:text>"), Qt::ISODate)</xsl:text>
0192         </xsl:when>
0193       </xsl:choose>
0194       <xsl:text>);
0195   </xsl:text>
0196     </xsl:if>
0197   </xsl:for-each>
0198 
0199   <!-- Call .insert() //-->
0200   if (!<xsl:value-of select="$variable" />.insert()) {
0201     qWarning() &lt;&lt; "Failed to insert <xsl:value-of select="$variable" /> into database";
0202     qWarning() &lt;&lt; "DB Error:" &lt;&lt; FakeDataStore::self()->database().lastError().text();
0203     return false;
0204   }
0205   qDebug() &lt;&lt; "<xsl:value-of select="local-name()" /> '<xsl:choose>
0206     <xsl:when test="local-name() = 'PimItem'">
0207       <xsl:value-of select="$table/@remoteId" />
0208     </xsl:when>
0209     <xsl:when test="local-name() = 'Tag'">
0210       <xsl:value-of select="$table/@gid" />
0211     </xsl:when>
0212     <xsl:when test="local-name() = 'PartType'">
0213       <xsl:value-of select="$table/@ns" />:<xsl:value-of select="$table/@name" />
0214     </xsl:when>
0215     <xsl:when test="local-name() = 'Part'">
0216       <xsl:value-of select="$table/@partType" />
0217     </xsl:when>
0218     <xsl:otherwise>
0219       <xsl:value-of select="$table/@name"/>
0220     </xsl:otherwise>
0221   </xsl:choose>' inserted with id" &lt;&lt; <xsl:value-of select="$variable" />.id();
0222 
0223   <!-- Recursively process child entities //-->
0224   <xsl:call-template name="parse-entities-recursively">
0225     <xsl:with-param name="attributes" select="$attributes"/>
0226   </xsl:call-template>
0227 </xsl:template>
0228 
0229 
0230 
0231 <!-- Finds all entities with name "attrType" (like <MimeType> or <Flag>) as well
0232      as all attributes with "attrName" ("mimeType = ...") and transforms them into
0233      a simple list of elements //-->
0234 <xsl:template name="transform-all-attributes">
0235   <xsl:param name="attrType" />
0236   <xsl:param name="attrName" />
0237   <xsl:copy>
0238     <xsl:for-each select="/descendant::*[local-name() = $attrType]">
0239     <xsl:element name="{$attrType}">
0240         <xsl:choose>
0241           <!-- Special case of Tags, which don't have name, but GID //-->
0242           <xsl:when test="$attrType = 'Tag'">
0243             <xsl:attribute name="id"><xsl:value-of select="@gid" /></xsl:attribute>
0244             <xsl:attribute name="gid"><xsl:value-of select="@gid" /></xsl:attribute>
0245           </xsl:when>
0246           <xsl:otherwise>
0247             <xsl:attribute name="id"><xsl:value-of select="@name" /></xsl:attribute>
0248             <xsl:attribute name="name"><xsl:value-of select="@name" /></xsl:attribute>
0249           </xsl:otherwise>
0250         </xsl:choose>
0251       </xsl:element>
0252     </xsl:for-each>
0253     <xsl:for-each select="/descendant::*">
0254       <xsl:variable name="attrValue" select="@*[local-name() = $attrName]" />
0255       <xsl:if test="$attrValue != ''">
0256         <xsl:element name="{$attrType}">
0257           <xsl:attribute name="id"><xsl:value-of select="$attrValue" /></xsl:attribute>
0258           <xsl:choose>
0259             <xsl:when test="$attrType = 'PartType' or $attrName = 'partType'">
0260               <xsl:attribute name="ns"><xsl:value-of select="substring-before(@partType, ':')" /></xsl:attribute>
0261               <xsl:attribute name="name"><xsl:value-of select="substring-after(@partType, ':')" /></xsl:attribute>
0262             </xsl:when>
0263             <xsl:otherwise>
0264               <xsl:attribute name="name"><xsl:value-of select="$attrValue" /></xsl:attribute>
0265             </xsl:otherwise>
0266           </xsl:choose>
0267         </xsl:element>
0268       </xsl:if>
0269     </xsl:for-each>
0270   </xsl:copy>
0271 </xsl:template>
0272 
0273 <!-- Finds all entities of type 'attrType' or attributes with name 'attrName',
0274      sorts them, removes duplicates and returns a new simple list of elements //-->
0275 <xsl:template name="parse-attributes">
0276   <xsl:param name="attrType" />
0277   <xsl:param name="attrName" />
0278   <xsl:variable name="tmp">
0279     <xsl:call-template name="transform-all-attributes">
0280       <xsl:with-param name="attrType" select="$attrType"/>
0281       <xsl:with-param name="attrName" select="$attrName"/>
0282     </xsl:call-template>
0283   </xsl:variable>
0284   <xsl:variable name="sorted">
0285     <xsl:copy>
0286       <xsl:for-each select="exsl:node-set($tmp)/*">
0287         <xsl:sort select="@id" data-type="text"/>
0288         <xsl:copy-of select="." />
0289       </xsl:for-each>
0290     </xsl:copy>
0291   </xsl:variable>
0292 
0293   <xsl:copy>
0294     <xsl:for-each select="exsl:node-set($sorted)/*">
0295       <xsl:sort select="@id" data-type="text"/>
0296       <xsl:if test="position() = 1 or @id != preceding-sibling::*[1]/@id">
0297         <xsl:copy-of select="."/>
0298       </xsl:if>
0299     </xsl:for-each>
0300   </xsl:copy>
0301 </xsl:template>
0302 
0303 
0304 <xsl:template match="/">
0305 <!-- Header generation //-->
0306 <xsl:if test="$code='header'">
0307 /*
0308  * This is an auto-generated file.
0309  * Do not edit! All changes made to it will be lost.
0310  */
0311 
0312 #ifndef AKONADI_SERVER_DBPOPULATOR_H
0313 #define AKONADI_SERVER_DBPOPULATOR_H
0314 
0315 namespace Akonadi {
0316 namespace Server {
0317 
0318 class DbPopulator
0319 {
0320 public:
0321     DbPopulator();
0322     ~DbPopulator();
0323 
0324     bool run();
0325 
0326 };
0327 
0328 }
0329 }
0330 #endif
0331 </xsl:if>
0332 
0333 <!-- Source generation //-->
0334 <xsl:if test="$code='source'">
0335 /*
0336  * This is an auto-generated file.
0337  * Do not edit! All changes made to it will be lost.
0338  */
0339 
0340 #include "dbpopulator.h"
0341 #include "entities.h"
0342 #include "fakedatastore.h"
0343 
0344 #include &lt;QtSql/QSqlDatabase&gt;
0345 #include &lt;QtSql/QSqlQuery&gt;
0346 #include &lt;QtSql/QSqlError&gt;
0347 
0348 #include &lt;QtCore/QString&gt;
0349 #include &lt;QtCore/QVariant&gt;
0350 
0351 using namespace Akonadi::Server;
0352 
0353 DbPopulator::DbPopulator()
0354 {
0355 }
0356 
0357 DbPopulator::~DbPopulator()
0358 {
0359 }
0360 
0361 
0362 
0363 bool DbPopulator::run()
0364 {
0365 
0366   <!-- Extract, declare and insert into database all mimetypes, flags, parttypes and tags //-->
0367   <xsl:variable name="attributes">
0368     <xsl:element name="MimeTypes">
0369       <xsl:call-template name="parse-attributes">
0370         <xsl:with-param name="attrType">MimeType</xsl:with-param>
0371         <xsl:with-param name="attrName">mimeType</xsl:with-param>
0372       </xsl:call-template>
0373     </xsl:element>
0374     <xsl:element name="Flags">
0375       <xsl:call-template name="parse-attributes">
0376         <xsl:with-param name="attrType">Flag</xsl:with-param>
0377         <xsl:with-param name="attrName">flag</xsl:with-param>
0378       </xsl:call-template>
0379     </xsl:element>
0380     <xsl:element name="PartTypes">
0381       <xsl:call-template name="parse-attributes">
0382         <xsl:with-param name="attrType">PartType</xsl:with-param>
0383         <xsl:with-param name="attrName">partType</xsl:with-param>
0384       </xsl:call-template>
0385     </xsl:element>
0386     <xsl:element name="Tags">
0387       <xsl:call-template name="parse-attributes">
0388         <xsl:with-param name="attrType">Tag</xsl:with-param>
0389         <xsl:with-param name="attrName">tag</xsl:with-param>
0390       </xsl:call-template>
0391     </xsl:element>
0392   </xsl:variable>
0393 
0394 
0395   <xsl:for-each select="exsl:node-set($attributes)/*/*">
0396     <xsl:call-template name="parse-entity">
0397       <xsl:with-param name="table" select="current()"/>
0398     </xsl:call-template>
0399   </xsl:for-each>
0400 
0401 
0402   <!-- Extract, declare and insert into database all remaining entities //-->
0403   <xsl:for-each select="/data">
0404     <xsl:call-template name="parse-entities-recursively">
0405       <xsl:with-param name="attributes" select="$attributes" />
0406     </xsl:call-template>
0407   </xsl:for-each>
0408 
0409   qDebug() &lt;&lt; "Database successfully populated";
0410   return true;
0411 }
0412 </xsl:if>
0413 
0414 
0415 </xsl:template>
0416 </xsl:stylesheet>
0417