public function write()
    {
        $adapter = new Adapter("/");
        $filesystem = new Filesystem($adapter);
        if ( ! $filesystem->has($this->path)) {
            $filesystem->createDir($this->path);
        }

        $filename = $this->path . $this->getFileName();

        $writer   = new CsvWriter( "," );
        $writer->setStream( fopen( $filename, 'w' ) );

        $header = $this->getPrologHeader();
        if ($this->includeProlog) {
            $header[ 1 ][ 0 ] = 'uri';
            ksort($header[1]);
            $header[ 2 ][ 0 ] = 'lang';
            $header[ 2 ][ 1 ] = $this->getSchema()->getLanguage(); //default language
            ksort($header[2]);
            $header[ 3 ][ 0 ] = 'type';
            $header[ 3 ][ 1 ] = 'uri'; //default type
            ksort($header[3]);
        }

        foreach ( $header as $line )
        {
            $writer->writeItem( $line );
        }

        if ($this->includeProlog) {

            $metadata = $this->getMetadata();
            foreach ($metadata as $line) {
                $writer->writeItem($line);
            }

            $prefixRows = $this->getPrefixRows();
            foreach ($prefixRows as $line) {
                $writer->writeItem($line);
            }
        }
        //get the data
        if ( $this->populate )
        {
            $prefixes = $this->getPrefixes();
            $prefixPattern = array();
            $prefixReplacement = array();
            foreach ( $prefixes as $prefix => $namespace )
            {
                if (trim($prefix)) {
                    if ( ! is_int($prefix)) {
                        $prefixPattern[] = "|" . $namespace . "|";
                        $prefixReplacement[] = $prefix . ":";
                    }
                }
            }

            $map = $this->getHeaderMap();
            $c   = new \Criteria();
            $c->clearSelectColumns();
            if ('schema' === $this->type) {
                $c->addSelectColumn(\SchemaPropertyPeer::ID);
                $c->add(\SchemaPropertyPeer::SCHEMA_ID, $this->schema->getId());
                if ($this->excludeDeprecated) {
                    $c->add(\SchemaPropertyPeer::STATUS_ID, 8, \Criteria::NOT_EQUAL);
                }
                $c->addAscendingOrderByColumn(\SchemaPropertyPeer::URI);
                $properties = \SchemaPropertyPeer::doSelectRS($c);
            } else {
                $c->addSelectColumn(\ConceptPeer::ID);
                $c->addSelectColumn(\ConceptPeer::URI);
                $c->addSelectColumn(\ConceptPeer::STATUS_ID);
                $c->add(\ConceptPeer::VOCABULARY_ID, $this->schema->getId());
                if ($this->excludeDeprecated) {
                    $c->add(\ConceptPeer::STATUS_ID, 8, \Criteria::NOT_EQUAL);
                }
                $c->addAscendingOrderByColumn(\ConceptPeer::URI);
                $properties = \ConceptPeer::doSelectRS($c);
            }
            foreach ( $properties as $property )
            {
                $line    = array_fill( 0, $this->getHeaderCount(), '' );
                $line[0] = $property[0];
                $map     = $this->getHeaderMap();

                $ce = new \Criteria();
                if ('schema' === $this->type) {
                    $ce->add(\BaseSchemaPropertyElementPeer::SCHEMA_PROPERTY_ID, $property[0]);
                    if (!$this->includeDeleted) {
                        $ce->add(\BaseSchemaPropertyElementPeer::DELETED_AT, null);
                    }
                    if ($this->includeDeleted) {
                        $ce->addAscendingOrderByColumn(\SchemaPropertyElementPeer::UPDATED_AT);
                    }
                    $elements = \SchemaPropertyElementPeer::doSelectJoinProfileProperty($ce);
                } else {
                    $ce->add(\ConceptPropertyPeer::CONCEPT_ID, $property[0]);
                    if (!$this->includeDeleted) {
                        $ce->add(\ConceptPropertyPeer::DELETED_AT, null);
                    }
                    if ($this->includeDeleted) {
                        $ce->addAscendingOrderByColumn(\ConceptPropertyPeer::UPDATED_AT);
                    }
                    $elements = \ConceptPropertyPeer::doSelectJoinProfilePropertyRelatedBySkosPropertyId($ce);
                    $line[array_search('uri', $header[0])] = $property[1];
                    $line[array_search('status', $header[0])] = $property[2];
                }
                /** @var \SchemaPropertyElement $element */
                foreach ($elements as $element )
                {
                    if ($this->excludeGenerated and $element->getIsGenerated()) {
                        continue;
                    }
                    /** @var \ProfileProperty $profileProperty */
                    $profileProperty = $element->getProfileProperty();
                    $propertyId      = $profileProperty->getId();
                    if ('schema' === $this->type and in_array( $propertyId, [ 6, 9, ] ) and $element->getIsSchemaProperty() )
                    {
                        $language = 'parent';
                    }
                    else
                    {
                        $language = $profileProperty->getHasLanguage() ? $element->getLanguage() : '';
                    }
                    $index = $propertyId . $language;
                    if (isset($map[ $index ])) {
                        foreach ($map[ $index ] as &$column) {
                            if (false !== $column) {
                                $line[ $column ] = $element->getObject();
                                $column = false;
                                break;
                            }
                        }
                    }
                }

                $writer->writeItem( preg_replace( $prefixPattern, $prefixReplacement, $line ));

                unset($line, $elements);
            }
        }

        //add an empty line at the end
        $line = array_fill( 0, $this->getHeaderCount(), '' );
        $writer->writeItem( $line );
        $writer->finish();
    }
 /**
  * If this collection has already been initialized with
  * an identical criteria, it returns the collection.
  * Otherwise if this Concept is new, it will return
  * an empty collection; or if this Concept has previously
  * been saved, it will retrieve related ConceptPropertysRelatedByRelatedConceptId from storage.
  *
  * This method is protected by default in order to keep the public
  * api reasonable.  You can provide public methods for those you
  * actually need in Concept.
  */
 public function getConceptPropertysRelatedByRelatedConceptIdJoinProfilePropertyRelatedBySkosPropertyId($criteria = null, $con = null)
 {
     // include the Peer class
     include_once 'lib/model/om/BaseConceptPropertyPeer.php';
     if ($criteria === null) {
         $criteria = new Criteria();
     } elseif ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
     }
     if ($this->collConceptPropertysRelatedByRelatedConceptId === null) {
         if ($this->isNew()) {
             $this->collConceptPropertysRelatedByRelatedConceptId = array();
         } else {
             $criteria->add(ConceptPropertyPeer::RELATED_CONCEPT_ID, $this->getId());
             $this->collConceptPropertysRelatedByRelatedConceptId = ConceptPropertyPeer::doSelectJoinProfilePropertyRelatedBySkosPropertyId($criteria, $con);
         }
     } else {
         // the following code is to determine if a new query is
         // called for.  If the criteria is the same as the last
         // one, just return the collection.
         $criteria->add(ConceptPropertyPeer::RELATED_CONCEPT_ID, $this->getId());
         if (!isset($this->lastConceptPropertyRelatedByRelatedConceptIdCriteria) || !$this->lastConceptPropertyRelatedByRelatedConceptIdCriteria->equals($criteria)) {
             $this->collConceptPropertysRelatedByRelatedConceptId = ConceptPropertyPeer::doSelectJoinProfilePropertyRelatedBySkosPropertyId($criteria, $con);
         }
     }
     $this->lastConceptPropertyRelatedByRelatedConceptIdCriteria = $criteria;
     return $this->collConceptPropertysRelatedByRelatedConceptId;
 }