<commit_before><?xml version="1.0" encoding="UTF-8"?>

<!--
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->

<xwikidoc>
  <web>PhenoTips</web>
  <name>Export</name>
  <language/>
  <defaultLanguage/>
  <translation>0</translation>
  <parent>PhenoTips.WebHome</parent>
  <creator>xwiki:XWiki.Admin</creator>
  <author>xwiki:XWiki.Admin</author>
  <customClass/>
  <contentAuthor>xwiki:XWiki.Admin</contentAuthor>
  <creationDate>1341433524000</creationDate>
  <date>1341890547000</date>
  <contentUpdateDate>1341890547000</contentUpdateDate>
  <version>1.1</version>
  <title/>
  <defaultTemplate/>
  <validationScript/>
  <comment/>
  <minorEdit>false</minorEdit>
  <syntaxId>xwiki/2.1</syntaxId>
  <hidden>false</hidden>
  <content>{{velocity wiki="true"}}{{{
#set ($targetSpace = "$!{request.space}")
#if (!$targetSpace || $targetSpace == 'PhenoTips')
  #break
#end
#set ($preferencesObject = $xwiki.getDocument("${targetSpace}.WebHome").getObject('PhenoTips.DBConfigurationClass'))
#if (!$preferencesObject)
  #break
#end
#set ($targetClassName = $preferencesObject.getProperty('patientClass').value)
#set ($targetClass = $xwiki.getClass($targetClassName))
#set ($activeFields = $preferencesObject.getProperty('activeFields').value)
#set ($dateFormat = $xwiki.jodatime.getDateTimeFormatterForPattern('MM/dd/yyyy'))
#set ($query = ', BaseObject obj where doc.space = ? and obj.name = doc.fullName and obj.className = ?')
#set ($parameters = [$targetSpace, $targetClassName])
#set ($isFirstReferrer = true)
#foreach ($referrer in $request.getParameterValues('doc.creator'))
  #if ($referrer != '')
    #if ($isFirstReferrer)
      #set ($query = $query.concat(" and (doc.creator = ?"))
      #set ($isFirstReferrer = false)
    #else
      #set ($query = $query.concat(" or doc.creator = ?"))
    #end
    #set ($discard = $parameters.add($referrer))
  #end
#end
#if (!$isFirstReferrer)
  #set ($query = $query.concat(')'))
#end
#set ($isFirstPhenotype = true)
#set ($joinOperator = "$!{request.get('phenotype/join_mode').toUpperCase()}")
#if ($joinOperator != 'AND' &amp;&amp; $joinOperator != 'OR')
  #set ($joinOperator = 'AND')
#end
#foreach ($phenotype in $request.getParameterValues('phenotype'))
  #if ($phenotype != '')
    #if ($isFirstPhenotype)
      #set ($isFirstPhenotype = false)
      #if ("$!request.phenotype_subterms" == 'no')
        #set ($query = ", DBStringListProperty phenotype join phenotype.list as phenotypeValue ${query} and phenotype.id.id = obj.id and phenotype.id.name = 'phenotype' and (? in phenotypeValue")
      #else
        #set ($query = ", DBStringListProperty phenotype join phenotype.list as phenotypeValue ${query} and phenotype.id.id = obj.id and phenotype.id.name = 'extended_phenotype' and (? in phenotypeValue")
      #end
    #else
      #set ($query = $query.concat(" ${joinOperator} ? in phenotypeValue"))
    #end
    #set ($discard = $parameters.add($phenotype))
  #end
#end
#if (!$isFirstPhenotype)
  #set ($query = $query.concat(')'))
#end
#set ($isFirstDisease = true)
#set ($joinOperator = "$!{request.get('omim/join_mode').toUpperCase()}")
#if ($joinOperator != 'AND' &amp;&amp; $joinOperator != 'OR')
  #set ($joinOperator = 'AND')
#end
#foreach ($disease in $request.getParameterValues('omim_id'))
  #if ($disease != '')
    #if ($isFirstDisease)
      #set ($query = ", DBStringListProperty disease join disease.list as diseaseValue ${query} and disease.id.id = obj.id and disease.id.name = 'omim_id' and (? in diseaseValue")
      #set ($isFirstDisease = false)
    #else
      #set ($query = $query.concat(" ${joinOperator} ? in diseaseValue"))
    #end
    #set ($discard = $parameters.add($disease))
  #end
#end
#if (!$isFirstDisease)
  #set ($query = $query.concat(')'))
#end
#if ("$!request.get('doc.creationDate/after')" != '')
  #set ($date = $dateFormat.parseDateTime($request.get('doc.creationDate/after')))
  #set ($query = $query.concat(' and doc.creationDate &gt; ?'))
  #set ($discard = $parameters.add($date.toDate()))
#end
#if ("$!request.get('doc.creationDate/before')" != '')
  #set ($date = $dateFormat.parseDateTime($request.get('doc.creationDate/before')))
  #set ($query = $query.concat(' and doc.creationDate &lt; ?'))
  #set ($discard = $parameters.add($date.toDate()))
#end
#set ($hasBDate = false)
#if ("$!request.get('date_of_birth/after')" != '')
  #set ($date = $dateFormat.parseDateTime($request.get('date_of_birth/after')))
  #set ($query = ", DateProperty birthDate $query and birthDate.id.id = obj.id and birthDate.id.name = 'date_of_birth' and birthDate.value &gt; ?")
  #set ($discard = $parameters.add($date.toDate()))
  #set ($hasBDate = true)
#end
#if ("$!request.get('date_of_birth/before')" != '')
  #set ($date = $dateFormat.parseDateTime($request.get('date_of_birth/before')))
  #if ($hasBDate)
    #set ($query = "$query and birthDate.value &lt; ?")
  #else
    #set ($query = ", DateProperty birthDate $query and birthDate.id.id = obj.id and birthDate.id.name = 'date_of_birth' and birthDate.value &lt; ?")
  #end
  #set ($discard = $parameters.add($date.toDate()))
#end
#set ($patients = $xwiki.searchDocuments($query, $parameters))
#if ($request.count)
  $patients.size()
  #break
#end
$response.addHeader("Content-disposition", "attachment; filename=phenotips_${xwiki.formatDate($util.date, 'yyyy-MM-dd_HH-mm')}.csv")##
$response.setContentType('text/csv')##
#set ($newline = ' ')
#set ($newline = "${newline.charAt(0).toChars(13)[0]}${newline.charAt(0).toChars(10)[0]}")
#set ($codeProperties = ['phenotype', 'negative_phenotype', 'prenatal_phenotype', 'omim_id'])
#set ($enabledFields = $request.getParameterValues('columns'))
#set ($displayedFields = [])
#foreach ($field in $activeFields)
  #if ($enabledFields.contains($field))
    #set ($discard = $displayedFields.add($field))
  #elseif ($codeProperties.contains($field) &amp;&amp; $enabledFields.contains("${field}_code"))
    #set ($discard = $displayedFields.add($field))
  #end
#end
#if ($enabledFields.contains('doc.name'))Report ID,#end
#if ($enabledFields.contains('referrer'))Referrer,#end
#if ($enabledFields.contains('creationDate'))Date,#end
#foreach ($field in $displayedFields)
  #if ($codeProperties.contains($field) &amp;&amp; $enabledFields.contains("${field}_code"))
    ${targetClass.get($field).prettyName} (code)##
    #if ($enabledFields.contains($field) || $enabledFields.contains("${field}_combined")),#end
  #end
  #if ($enabledFields.contains($field))
    ${targetClass.get($field).prettyName}##
    #if ($enabledFields.contains("${field}_combined")),#end
  #end
  #if ($codeProperties.contains($field) &amp;&amp; $enabledFields.contains("${field}_combined"))
    ${targetClass.get($field).prettyName} (code and name)##
  #end
  #if ($foreach.hasNext)##
    ,##
  #end
#end
${newline}##
#foreach ($patient in $patients)
  #set ($patientDoc = $xwiki.getDocument($patient))
  #set ($patientObj = $patientDoc.getObject($targetClassName))
  #if ($enabledFields.contains('doc.name'))${patientDoc.documentReference.name},#end
  #if ($enabledFields.contains('referrer'))${xwiki.getUserName($patientDoc.creator, false)},#end
  #if ($enabledFields.contains('creationDate'))$!{xwiki.formatDate($patientDoc.creationDate, 'MM/dd/yyyy')},#end
  #foreach ($field in $displayedFields)
    #if ($codeProperties.contains($field))
      #if ($enabledFields.contains("${field}_code"))
        "##
        #foreach ($code in $!patientObj.getProperty($field).value)
          $code##
          #if ($foreach.hasNext)
            ${newline}##
          #end
        #end
        "##
        #if ($enabledFields.contains($field) || $enabledFields.contains("${field}_combined")),#end
      #end
      #if ($enabledFields.contains($field))
        "##
        #foreach ($code in $!patientObj.getProperty($field).value)
          #if ($field.endsWith('phenotype'))
            #set ($name = $!{services.hpo.get($code).name})
            #if ($name)
              ${name.replace('"', '""')}##
            #else
              ${code.replace('"', '""')}##
            #end
          #else
            ${services.omim.get($code).name.replace('"', '""')}##
          #end
          #if ($foreach.hasNext)
            ${newline}##
          #end
        #end
        "##
        #if ($enabledFields.contains("${field}_combined")),#end
      #end
      #if ($enabledFields.contains("${field}_combined"))
        "##
        #foreach ($code in $!patientObj.getProperty($field).value)
          #if ($field.endsWith('phenotype'))
            #if ($code.startsWith('HP:'))[${code}] #end
            #set ($name = $!{services.hpo.get($code).name})
            #if ($name)
              ${name.replace('"', '""')}##
            #else
              ${code.replace('"', '""')}##
            #end
          #else
            [${code}] ${services.omim.get($code).name.replace('"', '""')}##
          #end
          #if ($foreach.hasNext)
            ${newline}##
          #end
        #end
        "##
      #end
    #else
      #set ($value = $!patientObj.getProperty($field).value)
      #if ("$!value.getTime()" != '')
        #set ($classFormat = "$!patientObj.xWikiClass.get($field).getProperty('dateFormat').value")
        #set ($format = "$!{preferencesObject.getProperty('dateOfBirthFormat').value}")
        #if ("$!format" == '')
          #set ($format = $classFormat)
        #end
        "$!{xwiki.formatDate($value, $format)}"##
      #elseif ("$!value.size()" != '')
        "##
        #foreach ($line in $value)
          $!{line.replace('"', '""')}##
          #if ($foreach.hasNext)
            ${newline}##
          #end
        #end
        "##
      #else
        #set($displayedValue = "$!{patientObj.display($field, 'view')}")
        #if ($displayedValue.startsWith('{{html'))
          "$!{value.replace('"', '""')}"##
        #else
          "$!{displayedValue.replace('"', '""')}"##
        #end
      #end
    #end
    #if ($foreach.hasNext)##
      ,##
    #end
  #end
  ${newline}##
#end
}}}
{{/velocity}}</content>
</xwikidoc>
<commit_msg>Issue #468: Option to export the code and name of phenotypes combined in single column in the CSV export Make it work also when only the combined column is exported<commit_after><?xml version="1.0" encoding="UTF-8"?>

<!--
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->

<xwikidoc>
  <web>PhenoTips</web>
  <name>Export</name>
  <language/>
  <defaultLanguage/>
  <translation>0</translation>
  <parent>PhenoTips.WebHome</parent>
  <creator>xwiki:XWiki.Admin</creator>
  <author>xwiki:XWiki.Admin</author>
  <customClass/>
  <contentAuthor>xwiki:XWiki.Admin</contentAuthor>
  <creationDate>1341433524000</creationDate>
  <date>1341890547000</date>
  <contentUpdateDate>1341890547000</contentUpdateDate>
  <version>1.1</version>
  <title/>
  <defaultTemplate/>
  <validationScript/>
  <comment/>
  <minorEdit>false</minorEdit>
  <syntaxId>xwiki/2.1</syntaxId>
  <hidden>false</hidden>
  <content>{{velocity wiki="true"}}{{{
#set ($targetSpace = "$!{request.space}")
#if (!$targetSpace || $targetSpace == 'PhenoTips')
  #break
#end
#set ($preferencesObject = $xwiki.getDocument("${targetSpace}.WebHome").getObject('PhenoTips.DBConfigurationClass'))
#if (!$preferencesObject)
  #break
#end
#set ($targetClassName = $preferencesObject.getProperty('patientClass').value)
#set ($targetClass = $xwiki.getClass($targetClassName))
#set ($activeFields = $preferencesObject.getProperty('activeFields').value)
#set ($dateFormat = $xwiki.jodatime.getDateTimeFormatterForPattern('MM/dd/yyyy'))
#set ($query = ', BaseObject obj where doc.space = ? and obj.name = doc.fullName and obj.className = ?')
#set ($parameters = [$targetSpace, $targetClassName])
#set ($isFirstReferrer = true)
#foreach ($referrer in $request.getParameterValues('doc.creator'))
  #if ($referrer != '')
    #if ($isFirstReferrer)
      #set ($query = $query.concat(" and (doc.creator = ?"))
      #set ($isFirstReferrer = false)
    #else
      #set ($query = $query.concat(" or doc.creator = ?"))
    #end
    #set ($discard = $parameters.add($referrer))
  #end
#end
#if (!$isFirstReferrer)
  #set ($query = $query.concat(')'))
#end
#set ($isFirstPhenotype = true)
#set ($joinOperator = "$!{request.get('phenotype/join_mode').toUpperCase()}")
#if ($joinOperator != 'AND' &amp;&amp; $joinOperator != 'OR')
  #set ($joinOperator = 'AND')
#end
#foreach ($phenotype in $request.getParameterValues('phenotype'))
  #if ($phenotype != '')
    #if ($isFirstPhenotype)
      #set ($isFirstPhenotype = false)
      #if ("$!request.phenotype_subterms" == 'no')
        #set ($query = ", DBStringListProperty phenotype join phenotype.list as phenotypeValue ${query} and phenotype.id.id = obj.id and phenotype.id.name = 'phenotype' and (? in phenotypeValue")
      #else
        #set ($query = ", DBStringListProperty phenotype join phenotype.list as phenotypeValue ${query} and phenotype.id.id = obj.id and phenotype.id.name = 'extended_phenotype' and (? in phenotypeValue")
      #end
    #else
      #set ($query = $query.concat(" ${joinOperator} ? in phenotypeValue"))
    #end
    #set ($discard = $parameters.add($phenotype))
  #end
#end
#if (!$isFirstPhenotype)
  #set ($query = $query.concat(')'))
#end
#set ($isFirstDisease = true)
#set ($joinOperator = "$!{request.get('omim/join_mode').toUpperCase()}")
#if ($joinOperator != 'AND' &amp;&amp; $joinOperator != 'OR')
  #set ($joinOperator = 'AND')
#end
#foreach ($disease in $request.getParameterValues('omim_id'))
  #if ($disease != '')
    #if ($isFirstDisease)
      #set ($query = ", DBStringListProperty disease join disease.list as diseaseValue ${query} and disease.id.id = obj.id and disease.id.name = 'omim_id' and (? in diseaseValue")
      #set ($isFirstDisease = false)
    #else
      #set ($query = $query.concat(" ${joinOperator} ? in diseaseValue"))
    #end
    #set ($discard = $parameters.add($disease))
  #end
#end
#if (!$isFirstDisease)
  #set ($query = $query.concat(')'))
#end
#if ("$!request.get('doc.creationDate/after')" != '')
  #set ($date = $dateFormat.parseDateTime($request.get('doc.creationDate/after')))
  #set ($query = $query.concat(' and doc.creationDate &gt; ?'))
  #set ($discard = $parameters.add($date.toDate()))
#end
#if ("$!request.get('doc.creationDate/before')" != '')
  #set ($date = $dateFormat.parseDateTime($request.get('doc.creationDate/before')))
  #set ($query = $query.concat(' and doc.creationDate &lt; ?'))
  #set ($discard = $parameters.add($date.toDate()))
#end
#set ($hasBDate = false)
#if ("$!request.get('date_of_birth/after')" != '')
  #set ($date = $dateFormat.parseDateTime($request.get('date_of_birth/after')))
  #set ($query = ", DateProperty birthDate $query and birthDate.id.id = obj.id and birthDate.id.name = 'date_of_birth' and birthDate.value &gt; ?")
  #set ($discard = $parameters.add($date.toDate()))
  #set ($hasBDate = true)
#end
#if ("$!request.get('date_of_birth/before')" != '')
  #set ($date = $dateFormat.parseDateTime($request.get('date_of_birth/before')))
  #if ($hasBDate)
    #set ($query = "$query and birthDate.value &lt; ?")
  #else
    #set ($query = ", DateProperty birthDate $query and birthDate.id.id = obj.id and birthDate.id.name = 'date_of_birth' and birthDate.value &lt; ?")
  #end
  #set ($discard = $parameters.add($date.toDate()))
#end
#set ($patients = $xwiki.searchDocuments($query, $parameters))
#if ($request.count)
  $patients.size()
  #break
#end
$response.addHeader("Content-disposition", "attachment; filename=phenotips_${xwiki.formatDate($util.date, 'yyyy-MM-dd_HH-mm')}.csv")##
$response.setContentType('text/csv')##
#set ($newline = ' ')
#set ($newline = "${newline.charAt(0).toChars(13)[0]}${newline.charAt(0).toChars(10)[0]}")
#set ($codeProperties = ['phenotype', 'negative_phenotype', 'prenatal_phenotype', 'omim_id'])
#set ($enabledFields = $request.getParameterValues('columns'))
#set ($displayedFields = [])
#foreach ($field in $activeFields)
  #if ($enabledFields.contains($field))
    #set ($discard = $displayedFields.add($field))
  #elseif ($codeProperties.contains($field) &amp;&amp; ($enabledFields.contains("${field}_code") || $enabledFields.contains("${field}_combined")))
    #set ($discard = $displayedFields.add($field))
  #end
#end
#if ($enabledFields.contains('doc.name'))Report ID,#end
#if ($enabledFields.contains('referrer'))Referrer,#end
#if ($enabledFields.contains('creationDate'))Date,#end
#foreach ($field in $displayedFields)
  #if ($codeProperties.contains($field) &amp;&amp; $enabledFields.contains("${field}_code"))
    ${targetClass.get($field).prettyName} (code)##
    #if ($enabledFields.contains($field) || $enabledFields.contains("${field}_combined")),#end
  #end
  #if ($enabledFields.contains($field))
    ${targetClass.get($field).prettyName}##
    #if ($enabledFields.contains("${field}_combined")),#end
  #end
  #if ($codeProperties.contains($field) &amp;&amp; $enabledFields.contains("${field}_combined"))
    ${targetClass.get($field).prettyName} (code and name)##
  #end
  #if ($foreach.hasNext)##
    ,##
  #end
#end
${newline}##
#foreach ($patient in $patients)
  #set ($patientDoc = $xwiki.getDocument($patient))
  #set ($patientObj = $patientDoc.getObject($targetClassName))
  #if ($enabledFields.contains('doc.name'))${patientDoc.documentReference.name},#end
  #if ($enabledFields.contains('referrer'))${xwiki.getUserName($patientDoc.creator, false)},#end
  #if ($enabledFields.contains('creationDate'))$!{xwiki.formatDate($patientDoc.creationDate, 'MM/dd/yyyy')},#end
  #foreach ($field in $displayedFields)
    #if ($codeProperties.contains($field))
      #if ($enabledFields.contains("${field}_code"))
        "##
        #foreach ($code in $!patientObj.getProperty($field).value)
          $code##
          #if ($foreach.hasNext)
            ${newline}##
          #end
        #end
        "##
        #if ($enabledFields.contains($field) || $enabledFields.contains("${field}_combined")),#end
      #end
      #if ($enabledFields.contains($field))
        "##
        #foreach ($code in $!patientObj.getProperty($field).value)
          #if ($field.endsWith('phenotype'))
            #set ($name = $!{services.hpo.get($code).name})
            #if ($name)
              ${name.replace('"', '""')}##
            #else
              ${code.replace('"', '""')}##
            #end
          #else
            ${services.omim.get($code).name.replace('"', '""')}##
          #end
          #if ($foreach.hasNext)
            ${newline}##
          #end
        #end
        "##
        #if ($enabledFields.contains("${field}_combined")),#end
      #end
      #if ($enabledFields.contains("${field}_combined"))
        "##
        #foreach ($code in $!patientObj.getProperty($field).value)
          #if ($field.endsWith('phenotype'))
            #if ($code.startsWith('HP:'))[${code}] #end
            #set ($name = $!{services.hpo.get($code).name})
            #if ($name)
              ${name.replace('"', '""')}##
            #else
              ${code.replace('"', '""')}##
            #end
          #else
            [${code}] ${services.omim.get($code).name.replace('"', '""')}##
          #end
          #if ($foreach.hasNext)
            ${newline}##
          #end
        #end
        "##
      #end
    #else
      #set ($value = $!patientObj.getProperty($field).value)
      #if ("$!value.getTime()" != '')
        #set ($classFormat = "$!patientObj.xWikiClass.get($field).getProperty('dateFormat').value")
        #set ($format = "$!{preferencesObject.getProperty('dateOfBirthFormat').value}")
        #if ("$!format" == '')
          #set ($format = $classFormat)
        #end
        "$!{xwiki.formatDate($value, $format)}"##
      #elseif ("$!value.size()" != '')
        "##
        #foreach ($line in $value)
          $!{line.replace('"', '""')}##
          #if ($foreach.hasNext)
            ${newline}##
          #end
        #end
        "##
      #else
        #set($displayedValue = "$!{patientObj.display($field, 'view')}")
        #if ($displayedValue.startsWith('{{html'))
          "$!{value.replace('"', '""')}"##
        #else
          "$!{displayedValue.replace('"', '""')}"##
        #end
      #end
    #end
    #if ($foreach.hasNext)##
      ,##
    #end
  #end
  ${newline}##
#end
}}}
{{/velocity}}</content>
</xwikidoc>

{'fraction_non_alphanumeric': 0.19274086908941682, 'fraction_numerical': 0.006338060279968994, 'mean_word_length': 3.88789837307778, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 2, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '5558432', 'n_tokens_mistral': 7287, 'n_tokens_neox': 7105, 'n_words': 1478}