public function executePropertylist()
 {
     $c = new Criteria();
     $c->add(ConceptPropertyPeer::CONCEPT_ID, $this->conceptId);
     $c->addDescendingOrderByColumn(ConceptPropertyPeer::SKOS_PROPERTY_ID);
     $this->properties = ConceptPropertyPeer::doSelect($c);
 }
Exemplo n.º 2
0
 /**
  * rss and atom feeds
  *
  * @return return_type
  */
 public function executeFeed()
 {
     /** @var sfWebRequest **/
     $request = $this->getContext()->getRequest();
     $IdType = $this->getRequestParameter('IdType', null);
     $id = $this->getRequestParameter('id', null);
     //Build the title
     $title = "Metadata Registry Change History";
     $filter = false;
     switch ($IdType) {
         case "property_id":
             /** @var ConceptProperty **/
             $conceptProperty = ConceptPropertyPeer::retrieveByPK($id);
             if ($conceptProperty) {
                 $vocabularyname = $conceptProperty->getVocabulary() ? $conceptProperty->getVocabulary()->getName() : "?";
                 $title .= " for Property: '" . $conceptProperty->getProfileProperty()->getName() . "' of Concept: '" . $conceptProperty->getConceptRelatedByConceptId()->getPrefLabel() . "' in Vocabulary: '" . $vocabularyname . "'";
             }
             $filter = true;
             break;
         case "concept_id":
             /** @var Concept **/
             $concept = DbFinder::from('Concept')->findPk($id);
             if ($concept) {
                 $title .= " for Concept: '" . $concept->getPrefLabel() . "' in Vocabulary: '" . $concept->getVocabulary()->getName() . "'";
             }
             $filter = true;
             break;
         case "vocabulary_id":
             /** @var Vocabulary **/
             $vocab = DbFinder::from('Vocabulary')->findPk($id);
             if ($vocab) {
                 $title .= " for Vocabulary: '" . $vocab->getName() . "'";
             }
             $filter = true;
             break;
         default:
             //the whole shebang
             $title .= " for all Vocabularies";
             break;
     }
     //special rule for property_id
     $column = "property_id" == $IdType ? "ConceptPropertyId" : sfInflector::camelize($IdType);
     //default limit to 100 if not set in config
     $limit = $request->getParameter('nb', sfConfig::get('app_frontend_feed_count', 100));
     $finder = DbFinder::from('ConceptPropertyHistory')->orderBy('ConceptPropertyHistory.CreatedAt', 'desc')->join('ConceptProperty', 'left join')->join('Concept', 'left join')->join('Vocabulary', 'left join')->join('Status', 'left join')->join('User', 'left join')->join('SkosProperty', 'left join')->with('Vocabulary', 'SkosProperty', 'User', 'Status', 'ConceptProperty', 'Concept');
     if ($filter) {
         $finder = $finder->where('ConceptPropertyHistory.' . $column, $id);
     }
     $finder = $finder->find($limit);
     $this->setTemplate('feed');
     $this->feed = sfFeedPeer::createFromObjects($finder, array('format' => $request->getParameter('format', 'atom1'), 'link' => $request->getUriPrefix() . $request->getPathInfo(), 'feedUrl' => $request->getUriPrefix() . $request->getPathInfo(), 'title' => htmlentities($title), 'methods' => array('authorEmail' => '', 'link' => 'getFeedUniqueId')));
     return;
 }
Exemplo n.º 3
0
 /**
  * Set the defaults
  *
  * @param  ConceptProperty $concept_property
  */
 public function setDefaults($discuss)
 {
     $action = $this->getRequest()->getParameter('action');
     if ('create' == strtolower($action)) {
         $filter = $this->getUser()->getAttributeHolder()->getAll('sf_admin/discuss/filters');
         //we need to get all the numbers
         if ($filter && is_array($filter)) {
             $filterKey = array_keys($filter);
             try {
                 switch ($filterKey[0]) {
                     case "property":
                         $property = ConceptPropertyPeer::retrieveByPK($filter[$filterKey[0]]);
                         $concept = $property->getConceptRelatedByConceptId();
                         $vocabId = $concept->getVocabularyId();
                         $vocabulary = myActionTools::findCurrentVocabulary();
                         /** @var Discuss **/
                         $discuss->setConceptProperty($property);
                         $discuss->setConcept($concept);
                         $discuss->setVocabularyId($vocabId);
                         break;
                     case "concept_id":
                         $concept = ConceptPeer::retrieveByPK($filter[$filterKey[0]]);
                         $vocabId = $concept->getVocabularyId();
                         /** @var Discuss **/
                         $discuss->setConcept($concept);
                         $discuss->setVocabularyId($vocabId);
                     case "vocabulary_id":
                         $vocabId = VocabularyPeer::retrieveByPK($filter[$filterKey[0]]);
                         /** @var Discuss **/
                         $discuss->setVocabularyId($vocabId);
                         break;
                     default:
                 }
             } catch (Exception $e) {
             }
         }
     } else {
     }
     parent::setDefaults($discuss);
 }
 /**
  * Populates the object using an array.
  *
  * This is particularly useful when populating an object from one of the
  * request arrays (e.g. $_POST).  This method goes through the column
  * names, checking to see whether a matching key exists in populated
  * array. If so the setByName() method is called for that column.
  *
  * You can specify the key type of the array by additionally passing one
  * of the class type constants TYPE_PHPNAME, TYPE_COLNAME, TYPE_FIELDNAME,
  * TYPE_NUM. The default key type is the column's phpname (e.g. 'authorId')
  *
  * @param      array  $arr     An array to populate the object from.
  * @param      string $keyType The type of keys the array uses.
  * @return     void
  */
 public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
 {
     $keys = ConceptPropertyPeer::getFieldNames($keyType);
     if (array_key_exists($keys[0], $arr)) {
         $this->setId($arr[$keys[0]]);
     }
     if (array_key_exists($keys[1], $arr)) {
         $this->setCreatedAt($arr[$keys[1]]);
     }
     if (array_key_exists($keys[2], $arr)) {
         $this->setUpdatedAt($arr[$keys[2]]);
     }
     if (array_key_exists($keys[3], $arr)) {
         $this->setDeletedAt($arr[$keys[3]]);
     }
     if (array_key_exists($keys[4], $arr)) {
         $this->setLastUpdated($arr[$keys[4]]);
     }
     if (array_key_exists($keys[5], $arr)) {
         $this->setCreatedUserId($arr[$keys[5]]);
     }
     if (array_key_exists($keys[6], $arr)) {
         $this->setUpdatedUserId($arr[$keys[6]]);
     }
     if (array_key_exists($keys[7], $arr)) {
         $this->setConceptId($arr[$keys[7]]);
     }
     if (array_key_exists($keys[8], $arr)) {
         $this->setPrimaryPrefLabel($arr[$keys[8]]);
     }
     if (array_key_exists($keys[9], $arr)) {
         $this->setSkosPropertyId($arr[$keys[9]]);
     }
     if (array_key_exists($keys[10], $arr)) {
         $this->setObject($arr[$keys[10]]);
     }
     if (array_key_exists($keys[11], $arr)) {
         $this->setSchemeId($arr[$keys[11]]);
     }
     if (array_key_exists($keys[12], $arr)) {
         $this->setRelatedConceptId($arr[$keys[12]]);
     }
     if (array_key_exists($keys[13], $arr)) {
         $this->setLanguage($arr[$keys[13]]);
     }
     if (array_key_exists($keys[14], $arr)) {
         $this->setStatusId($arr[$keys[14]]);
     }
     if (array_key_exists($keys[15], $arr)) {
         $this->setIsConceptProperty($arr[$keys[15]]);
     }
     if (array_key_exists($keys[16], $arr)) {
         $this->setProfilePropertyId($arr[$keys[16]]);
     }
     if (array_key_exists($keys[17], $arr)) {
         $this->setIsGenerated($arr[$keys[17]]);
     }
 }
Exemplo n.º 5
0
     if (!isset($vocabulary)) {
         if ($concept) {
             $vocabulary = $concept->getVocabulary();
         }
     }
     $objectId = $concept->getID();
     break;
 case 'conceptprop':
     $showBc = true;
     $showVocabularyBc = true;
     $showConceptBc = true;
     $showconceptpropBc = true;
     if (!isset($concept_property)) {
         $id = 'show' == $action ? $sf_params->get('id') : $paramId;
         if ($id) {
             $concept_property = ConceptPropertyPeer::retrieveByPK($id);
         }
     }
     if (!isset($concept)) {
         if ($concept_property) {
             $concept = $concept_property->getConceptRelatedByConceptId();
         }
     }
     if (!isset($vocabulary)) {
         if ($concept) {
             $vocabulary = $concept->getVocabulary();
         }
     }
     $objectId = $concept_property->getId();
     break;
 case 'historydetail':
function run_import_marc_vocabulary($task, $args)
{


    //check the argument counts
    if (count($args) < 1) {
        throw new Exception('You must provide a vocabulary type.');
    }

    if (count($args) < 2) {
        throw new Exception('You must provide a file name.');
    }

    if (count($args) < 3) {
        throw new Exception('You must provide a vocabulary id.');
    }

    //set the arguments
    $type          = strtolower($args[0]);
    $filePath      = $args[1];
    $id            = $args[2];
    $deleteMissing = (isset($args[3]) && ("-d" == $args[3]));

    //do some basic validity checks

    if (! in_array($type, array("schema", "vocab", "vocabulary"))) {
        throw new Exception('You must import into a schema or a vocab');
    }

    if ("vocabulary" == $type) {
        $type = "vocab";
    }

    if (! is_numeric($id)) {
        throw new Exception('You must provide a valid ID');
    }

    //does the file exist?
    if (! file_exists($filePath)) {
        throw new Exception('You must supply a valid file to import: ' . $filePath);
    }

    //is the file a valid type?
    if (preg_match('/^.+\.([[:alpha:]]{2,4})$/', $filePath, $matches)) {
        if (! in_array(strtolower($matches[1]), array("json", "rdf", "csv", "xml"))) {
            throw new Exception('You must provide a valid file type based on the extension');
        }
    } else {
        throw new Exception("File type cannot be determined from the file extension");
    }

    $fileType = $matches[1];

    //is the object a valid object?
    if ('vocab' == $type) {
        $vocabObj = VocabularyPeer::retrieveByPK($id);
        if (is_null($vocabObj)) {
            throw new Exception('Invalid vocabulary ID');
        }

        //set some defaults
        $baseDomain = $vocabObj->getBaseDomain();
        $language   = $vocabObj->getLanguage();
        $statusId   = $vocabObj->getStatusId();

        //get a skos property id map
        $skosMap = SkosPropertyPeer::getPropertyNames();

        //there has to be a hash or a slash
        $tSlash = preg_match('@(/$)@i', $vocabObj->getUri()) ? '' : '/';
        $tSlash = preg_match('/#$/', $vocabObj->getUri()) ? '' : $tSlash;
    } else {
        $schemaObj = SchemaPeer::retrieveByPK($id);
        if (is_null($schemaObj)) {
            throw new Exception('Invalid schema ID');
        }

        //set some defaults
        $baseDomain = $schemaObj->getUri();
        $language   = $schemaObj->getLanguage();
        $statusId   = $schemaObj->getStatusId();

        //get a element set property id map
        $profileId  = 1;
        $profile    = ProfilePeer::retrieveByPK($profileId);
        $elementMap = $profile->getAllProperties();

        //there has to be a hash or a slash
        $tSlash = preg_match('@(/$)@i', $baseDomain) ? '' : '/';
        $tSlash = preg_match('/#$/', $baseDomain) ? '' : $tSlash;
    }

    //     insert jon's user id
    $userId = 36;

    /* From here on the process is the same regardless of UI */

    //execute
    //     parse file to get the fields/columns and data
    $file = fopen($filePath, "r");
    if (! $file) {
        throw new Exception("Can't read supplied file");
    }

    //     check to see if file has been uploaded before
    //          check import history for file name
    $importHistory = FileImportHistoryPeer::retrieveByLastFilePath($filePath);
    //          if reimport
    //               get last import history for filename
    //               unserialize column map
    //               match column names to AP based on map
    //     look for matches in unmatched field/column names to AP (ideal)
    //     csv table of data --
    //          row1: parsed field names/column headers
    //          row2: select dropdown with available fields from object AP (pre-select known matches)
    //                each select identified by column number
    //          row3: display datatype of selected field (updated dynamically when field selected)
    //          row4-13: first 10 rows of parsed data from file
    //     require a column that can match to 'URI' (maybe we'll allow an algorithm later)
    //     require columns that are required by AP
    //     on reimport there should be a flag to 'delete missing properties' from the current data
    //     note: at some point there will be a reimport process that allows URI changing
    //          this will require that there be an OMR identifier embedded in the incoming data

    switch ($fileType) {
        case "csv":
            try {
                $reader = new aCsvReader($filePath);
            } catch(Exception $e) {
                throw new Exception("Not a happy CSV file!");
            }

            if ('vocab' == $type) {
                // Get array of heading names found
                $headings = $reader->getHeadings();
                $fields   = ConceptPeer::getFieldNames();

                //set the map
                //      $map[] = array("property" => "Uri", "column" => "URILocal");
                //      $map[] = array("property" => "prefLabel", "column" => "skos:prefLabel");
                //      $map[] = array("property" => "definition", "column" => "skos:definition");
                //      $map[] = array("property" => "notation", "column" => "skos:notation");
                //      $map[] = array("property" => "scopeNote", "column" => "skos:scopeNote");

                $map = array(
                  "uri"        => "URILocal",
                  "prefLabel"  => "skos:prefLabel",
                  "definition" => "skos:definition",
                  "notation"   => "skos:notation",
                  "scopeNote"  => "skos:scopeNote"
                );

                $rows = 0;

                //executeImport:

                //    serialize the column map
                try {
                    while ($row = $reader->getRow()) {
                        $rows ++;
                        //        lookup the URI (or the OMR ID if available) for a match
                        $uri        = $baseDomain . $row[$map["uri"]];
                        $concept    = ConceptPeer::getConceptByUri($uri);
                        $updateTime = time();
                        $language   = (isset($map['language'])) ? $row[$map['language']] : $vocabObj->getLanguage();

                        if (! $concept) {
                            //          create a new concept or element
                            $concept = new Concept();
                            $concept->setVocabulary($vocabObj);
                            $concept->setUri($uri);
                            /**
                             * @todo Need to handle updates for topconcept here, just like language
                             **/
                            $concept->setIsTopConcept(false);
                            $concept->updateFromRequest(
                                    $userId,
                                      fixMarcEncoding(rtrim($row[$map['prefLabel']])),
                                      $language,
                                      $statusId
                            );
                        } //don't update the concept if the preflabel matches
                        else if ($row[$map['prefLabel']] != $concept->getPrefLabel()) {
                            $concept->updateFromRequest($userId, fixMarcEncoding(rtrim($row[$map['prefLabel']])));
                        }

                        //there needs to be a language to lookup the properties unless it's an objectProperty
                        $rowLanguage = (isset($map['language'])) ? $row[$map['language']] : $concept->getLanguage();

                        foreach ($map as $key => $value) {
                            //we skip because we already did them
                            if (! in_array($key, array('uri', 'prefLabel', 'language'))) {
                                $skosId = $skosMap[$key];
                                //check to see if the property already exists
                                $property =
                                  ConceptPropertyPeer::lookupProperty($concept->getId(), $skosId, $rowLanguage);

                                //create a new property for each unmatched column
                                if (! empty($row[$value])) {
                                    if (! $property) {
                                        $property = new ConceptProperty();
                                        $property->setCreatedUserId($userId);
                                        $property->setConceptId($concept->getId());
                                        $property->setCreatedAt($updateTime);
                                        $property->setSkosPropertyId($skosId);
                                    }

                                    if (($row[$value] != $property->getObject()) ||
                                        ($rowLanguage != $property->getLanguage())
                                    ) {
                                        /**
                                         * @todo We need a check here for skos objectproperties and handle differently
                                         **/
                                        if ($rowLanguage != $property->getLanguage()) {
                                            $property->setLanguage($rowLanguage);
                                        }
                                        if ($row[$value] != $property->getObject()) {
                                            $property->setObject(fixMarcEncoding(rtrim($row[$value])));
                                        }
                                        $property->setUpdatedUserId($userId);
                                        $property->setUpdatedAt($updateTime);
                                        $property->save();
                                    }
                                } //the row value is empty
                                else if ($deleteMissing && $property) {
                                    $property->delete();
                                }
                            }
                        }

                        //          else
                        //               lookup and update concept or element
                        //               lookup and update each property
                        //          update the history for each property, action is 'import', should be a single timestamp for all (this should be automatic)
                        //          if 'delete missing properties' is true
                        //               delete each existing, non-required property that wasn't updated by the import
                    }
                } catch(Exception $e) {
                    //          catch
                    //            if there's an error of any kind, write to error log and continue
                    echo "Error on row: " . $rows . ", " . $uri . "\n" . $e . "\n";
                    continue;
                }
                $objects = $vocabObj->countConcepts();
            } else //it's an element set
            {
                $map  = array(
                  "uri"        => "uriLocalPart",
                  "name"       => "reg:name",
                  "definition" => "skos:definition",
                  "label"      => "rdfs:label",
                  "note"       => array("tag" => "tagCap", "ind1" => "ind1Cap", "ind2" => "ind2Cap", "sub" => "subCap")
                );
                $rows = 0;

                //executeImport:
                //    serialize the column map
                try {
                    while ($row = $reader->getRow()) {
                        //        lookup the URI (or the OMR ID if available) for a match

                        //There always has to be a URI on either update or create
                        if (! isset($row[$map["uri"]])) {
                            throw new Exception('Missing URI for row: ' . $reader->getRowCount());
                            continue;
                        }

                        $rows ++;
                        $uri         = $baseDomain . $tSlash . $row[$map["uri"]];
                        $property    = SchemaPropertyPeer::retrieveByUri($uri);
                        $updateTime  = time();
                        $rowLanguage = (isset($map['language'])) ? $row[$map['language']] : $language;
                        $rowStatusId = (isset($map['status'])) ? $row[$map['status']] : $statusId;

                        if (! $property) {
                            //          create a new property
                            /** @var SchemaProperty * */
                            $property = new SchemaProperty();
                            $property->setSchema($schemaObj);
                            $property->setUri($uri);
                            $property->setCreatedUserId($userId);
                            $property->setCreatedAt($updateTime);
                        }

                        $property->setLanguage($rowLanguage);
                        $property->setStatusId($rowStatusId);
                        $property->setUpdatedUserId($userId);
                        $property->setUpdatedAt($updateTime);

                        if (isset($row[$map["label"]])) {
                            $property->setLabel($row[$map["label"]]);
                        }

                        if (isset($row[$map["name"]])) {
                            $property->setName($row[$map["name"]]);
                        }

                        if (isset($row[$map["definition"]])) {
                            $property->setDefinition($row[$map["definition"]]);
                        }

                        if (is_array($map["note"])) {
                            $note = '';
                            foreach ($map["note"] as $key => $value) {
                                $caption = ! empty($row[$value]) ? " (" . $row[$value] . ")" : ' (no caption)';
                                $note .= ! empty($row[$key]) ? $key . ": " . $row[$key] . $caption . "<br />" : "";
                            }
                            $property->setNote($note);
                        } else {
                            if (isset($row[$map["note"]])) {
                                $property->setNote($row[$map["note"]]);
                            }
                        }
                        $property->saveSchemaProperty($userId);

                        /**
                         * @todo Need to handle domain and range
                         **/

                        foreach ($map as $key => $value) {
                            //we skip because we already did them
                            if (! in_array(
                              $key,
                              array('uri', 'status', 'language', 'label', 'name', 'definition', 'comment', 'note')
                            )
                            ) {
                                $elementId = $elementMap[$key];
                                //check to see if the property already exists
                                //note that this also checks the object value as well, so there's no way to update or delete an existing triple
                                //the sheet would have to conatin the identifier for the triple
                                $element = SchemaPropertyElementPeer::lookupElement(
                                                                    $schemaObj->getId(),
                                                                      $elementId,
                                                                      $map[$value]
                                );

                                //create a new property for each unmatched column
                                if (! empty($row[$value])) {
                                    if (! $element) {
                                        $element = new SchemaPropertyElement();
                                        $element->setCreatedUserId($userId);
                                        $element->setCreatedAt($updateTime);
                                        $element->setProfilePropertyId($elementId);
                                    }

                                    if (($row[$value] != $element->getObject()) ||
                                        ($rowLanguage != $element->getLanguage())
                                    ) {
                                        /**
                                         * @todo We need a check here for objectproperties and handle differently
                                         **/
                                        if ($rowLanguage != $element->getLanguage()) {
                                            $element->setLanguage($rowLanguage);
                                        }
                                        if ($row[$value] != $element->getObject()) {
                                            $element->setObject($row[$value]);
                                        }
                                        $element->setUpdatedUserId($userId);
                                        $element->setUpdatedAt($updateTime);
                                        $element->save();
                                    }
                                } //the row value is empty
                                else if ($deleteMissing && $element) {
                                    $element->delete();
                                }
                            }
                        }

                        //          else
                        //               lookup and update concept or element
                        //               lookup and update each property
                        //          update the history for each property, action is 'import', should be a single timestamp for all (this should be automatic)
                        //          if 'delete missing properties' is true
                        //               delete each existing, non-required property that wasn't updated by the import
                    }
                } catch(Exception $e) {
                    //          catch
                    //            if there's an error of any kind, write to error log and continue
                    echo "Error on row: " . $rows . ", " . $uri . "\n" . $e . "\n";
                    continue;
                }
                $objects = $schemaObj->countSchemaPropertys();
            }
            //     save the import history file (match timestamp to history entries)
            break;
        case "json":
            break;
        case "rdf":
            break;
        case "xml":
            break;
        default:
    }

    /* output to stdout*/
    //          number of objects imported (link to history, filtered on timestamp of import)
    echo "File:" . $filePath . ";\n     Objects imported: " . $objects . "; Rows read: " . $rows . "\n";
    //          number of errors (link to error log)

}
Exemplo n.º 7
0
 /**
  * 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 getConceptPropertysRelatedByRelatedConceptIdJoinProfilePropertyRelatedByProfilePropertyId($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::doSelectJoinProfilePropertyRelatedByProfilePropertyId($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::doSelectJoinProfilePropertyRelatedByProfilePropertyId($criteria, $con);
         }
     }
     $this->lastConceptPropertyRelatedByRelatedConceptIdCriteria = $criteria;
     return $this->collConceptPropertysRelatedByRelatedConceptId;
 }
 /**
  * Get the associated ConceptProperty object
  *
  * @param      Connection Optional Connection object.
  * @return     ConceptProperty The associated ConceptProperty object.
  * @throws     PropelException
  */
 public function getConceptProperty($con = null)
 {
     if ($this->aConceptProperty === null && $this->concept_property_id !== null) {
         // include the related Peer class
         include_once 'lib/model/om/BaseConceptPropertyPeer.php';
         $this->aConceptProperty = ConceptPropertyPeer::retrieveByPK($this->concept_property_id, $con);
         /* The following can be used instead of the line above to
         		   guarantee the related object contains a reference
         		   to this object, but this level of coupling
         		   may be undesirable in many circumstances.
         		   As it can lead to a db query with many results that may
         		   never be used.
         		   $obj = ConceptPropertyPeer::retrieveByPK($this->concept_property_id, $con);
         		   $obj->addConceptPropertys($this);
         		 */
     }
     return $this->aConceptProperty;
 }
 /**
  * Selects a collection of Concept objects pre-filled with all related objects except Status.
  *
  * @return array Array of Concept objects.
  * @throws PropelException Any exceptions caught during processing will be
  *		 rethrown wrapped into a PropelException.
  */
 public static function doSelectJoinAllExceptStatus(Criteria $c, $con = null)
 {
     $c = clone $c;
     // Set the correct dbName if it has not been overridden
     // $c->getDbName() will return the same object if not set to another value
     // so == check is okay and faster
     if ($c->getDbName() == Propel::getDefaultDB()) {
         $c->setDbName(self::DATABASE_NAME);
     }
     ConceptPeer::addSelectColumns($c);
     $startcol2 = ConceptPeer::NUM_COLUMNS - ConceptPeer::NUM_LAZY_LOAD_COLUMNS + 1;
     UserPeer::addSelectColumns($c);
     $startcol3 = $startcol2 + UserPeer::NUM_COLUMNS;
     UserPeer::addSelectColumns($c);
     $startcol4 = $startcol3 + UserPeer::NUM_COLUMNS;
     VocabularyPeer::addSelectColumns($c);
     $startcol5 = $startcol4 + VocabularyPeer::NUM_COLUMNS;
     ConceptPropertyPeer::addSelectColumns($c);
     $startcol6 = $startcol5 + ConceptPropertyPeer::NUM_COLUMNS;
     $c->addJoin(ConceptPeer::CREATED_USER_ID, UserPeer::ID);
     $c->addJoin(ConceptPeer::UPDATED_USER_ID, UserPeer::ID);
     $c->addJoin(ConceptPeer::VOCABULARY_ID, VocabularyPeer::ID);
     $c->addJoin(ConceptPeer::PREF_LABEL_ID, ConceptPropertyPeer::ID);
     $rs = BasePeer::doSelect($c, $con);
     $results = array();
     while ($rs->next()) {
         $omClass = ConceptPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj1 = new $cls();
         $obj1->hydrate($rs);
         $omClass = UserPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj2 = new $cls();
         $obj2->hydrate($rs, $startcol2);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj2 = $temp_obj1->getUserRelatedByCreatedUserId();
             //CHECKME
             if ($temp_obj2->getPrimaryKey() === $obj2->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj2->addConceptRelatedByCreatedUserId($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj2->initConceptsRelatedByCreatedUserId();
             $obj2->addConceptRelatedByCreatedUserId($obj1);
         }
         $omClass = UserPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj3 = new $cls();
         $obj3->hydrate($rs, $startcol3);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj3 = $temp_obj1->getUserRelatedByUpdatedUserId();
             //CHECKME
             if ($temp_obj3->getPrimaryKey() === $obj3->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj3->addConceptRelatedByUpdatedUserId($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj3->initConceptsRelatedByUpdatedUserId();
             $obj3->addConceptRelatedByUpdatedUserId($obj1);
         }
         $omClass = VocabularyPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj4 = new $cls();
         $obj4->hydrate($rs, $startcol4);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj4 = $temp_obj1->getVocabulary();
             //CHECKME
             if ($temp_obj4->getPrimaryKey() === $obj4->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj4->addConcept($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj4->initConcepts();
             $obj4->addConcept($obj1);
         }
         $omClass = ConceptPropertyPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj5 = new $cls();
         $obj5->hydrate($rs, $startcol5);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj5 = $temp_obj1->getConceptProperty();
             //CHECKME
             if ($temp_obj5->getPrimaryKey() === $obj5->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj5->addConcept($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj5->initConcepts();
             $obj5->addConcept($obj1);
         }
         $results[] = $obj1;
     }
     return $results;
 }
Exemplo n.º 10
0
    /**
     * @return \Ddeboer\DataImport\Result
     * @throws \Ddeboer\DataImport\Exception\ExceptionInterface
     * @throws \Exception
     */
    public function processData()
    {
        $workflow = new Workflow($this->reader);
        $output   = new ConsoleOutput();
        // Don’t import the non-metadata
        $filter = new Filter\CallbackFilter(function ($row) {
            if (is_numeric($row['reg_id'])) {
                return true;
            }
            if ( ! trim($row['reg_id'])) {
                foreach ($row as $item) {
                    if ( ! is_array($item)) {
                        if (trim($item)) {
                            return true;
                        }
                    } else {
                        foreach ($item as $foo) {
                            if (trim($foo)) {
                                return true;
                            }
                        }
                    }
                }
            }

            return false;
        });

        $trimConverter = new CallbackItemConverter(function ($row) {
            foreach ($row as $key => $value) {
                $row[$key] = trim($row[$key]);
            }

            return $row;
        });

        $lexicalConverter = new CallbackItemConverter(function ($row) {
            foreach ($row as $key => $value) {
                if (isset( $this->prolog['columns'][$key]['id'] ) and $this->prolog['columns'][$key]['id'] == 27) {
                    $lang = $this->prolog['columns'][$key]['lang'];
                    //check for existing language suffix
                    if ($value and ! preg_match("/\\." . $lang . "$/u", $value)) {
                        $row[$key] = $value . "." . $lang;
                    }
                }
            }

            return $row;
        });

        $typeConverter = new MappingValueConverter([
                                                       'rdfs:class'    => 'class',
                                                       'rdfs:property' => 'property',
                                                       'class'         => 'class',
                                                       'property'      => 'property',
                                                       'Class'         => 'class',
                                                       'Property'      => 'property',
                                                       'subclass'      => 'class',
                                                       'subproperty'   => 'property',
                                                       ''              => '',
                                                   ]);

        $vocabWriter  = new Writer\CallbackWriter(function ($row) {
            $this->setPrologColumns();

            //todo: $row[59] (status) and $row[62] (uri) sre hardcoded but the array key should be set somewhere globally
            if ( ! isset( $row[59] )) {
                $row[59] = $this->prolog['defaults']['statusId'];
            }
            $rowStatus = $row[59];
            $language  = $this->prolog['defaults']['lang'];

            foreach ($row as $key => &$element) {
                $this->updateRowUris($key, $element);
            }

            $uri      = $this->getFqn($row[62]);
            $property = null;

            if ( ! empty( $row['reg_id'] )) {
                $property = \ConceptPeer::retrieveByPK($row['reg_id']);
            } else {
                //check for an existing property by uri
                /** @var \Concept $property */
                $property = \ConceptPeer::getConceptByUri($uri);
            }

            //even if we found a property, we kill it if it's in a different schema than we're populating
            if ($property and $property->getVocabularyId() !== $this->vocabId) {
                //todo: we should log this event
                unset( $property );
            }

            if (empty( $property )) { //it's a new property
                $property = new \Concept();
                $property->setVocabularyId($this->vocabId);
                $property->setCreatedUserId($this->userId);
                $property->setUpdatedUserId($this->userId);
                $property->setStatusId($rowStatus);
                $property->setLanguage($language);
                $property->setUri($uri);
                $property->save();
            }

            unset( $row['reg_id'] );

            if ($property) {
                //        if (8 == $rowStatus) {
//          //it's been deprecated and we don't do anything else
//          $property->setStatusId($rowStatus);
//          $this->updateElement($element, $dbElement, $property);
//        } else {
                $dbElements = $property->getElementsForImport($this->profileProperties);
                foreach ($dbElements as $key => $dbElement) {
                    /** @var string | array $rowElement */
                    $rowElement = isset( $row[$key] ) ? $row[$key] : null;
                    if (is_array($rowElement)) {
                        foreach ($rowElement as $elementKey => &$element) {
                            if ($this->updateConceptProperty($element, $dbElement, $property)) {
                                unset( $rowElement[$elementKey] );
                            }
                        }
                    } else {
                        if ($this->updateConceptProperty($rowElement, $dbElement, $property)) {
                            unset( $row[$key] );
                        }
                    }
                }
                foreach ($row as $key => $value) {
                    $dbElement = isset( $dbElements[$key] ) ? $dbElements[$key] : null;
                    if ( ! empty( $this->prolog['columns'][$key]['property'] )) {
                        $profileProperty = $this->prolog['columns'][$key]['property'];
                        if (is_array($value)) {
                            foreach ($value as &$oneValue) {
                                $language = $this->prolog['columns'][$key]['lang'][0];
                                $this->upsertConceptFromRow($dbElement,
                                                            $oneValue,
                                                            $rowStatus,
                                                            $property,
                                                            $profileProperty,
                                                            $language,
                                                            $key);
                            }
                        } else {
                            $language = $this->prolog['columns'][$key]['lang'];
                            $this->upsertConceptFromRow($dbElement,
                                                        $value,
                                                        $rowStatus,
                                                        $property,
                                                        $profileProperty,
                                                        $language,
                                                        $key);
                        }
                    }
                }
            }

            $property->setUri($uri);
            $property->setStatusId($rowStatus);

            $affectedRows = $property->save();

            if (empty( $property->getPrefLabel() )) {
                $prefLabel = \ConceptPropertyPeer::lookupProperty($property->getId(),
                                                                  19,
                                                                  $this->prolog['defaults']['lang']);
                if ( ! empty( $prefLabel )) {
                    $property->setPrefLabel($prefLabel->getObject());
                    $property->setPrefLabelId($prefLabel->getId());
                    $property->setLanguage($this->prolog['defaults']['lang']);
                    $property->save();
                }
            }

            return;

            //**************
            // CRUFT ALERT
            // The rest of this is never run!!
            //**************

            //build an array of references
            $newElements  = [ ];
            $newElements2 = [ ];
            if ( ! isset( $row['status'] )) {
                $row[14] = $this->prolog['defaults']['statusId'];
            }
            foreach ($row as $key => $element) {
                //skip it there's no property id
                $columnKey = $this->prolog['columns'][$key];
                if ( ! $columnKey['id']) {
                    continue;
                }

                if ( ! empty( $columnKey['type'] ) and $this->useCuries) {
                    $element = $this->getFqn($element);
                }

                $key2               = md5(strval($columnKey['id']) . strval($columnKey['lang']) . $element);
                $newElements[$key2] = [ ];
                $newElements[$key2] += $columnKey;
                $newElements[$key2]['val'] = $element;
                /** @var \ProfileProperty $profileProperty */
                if (isset( $columnKey['property'] )) {
                    $profileProperty = $columnKey['property'];
                    $var             = [
                        'matchkey' => $key2,
                        'val'      => $newElements[$key2],
                    ];
                    if (isset( $profileProperty ) and $profileProperty->getHasLanguage()) {
                        $newElements2[$columnKey['id']][$columnKey['lang']][] = $var;
                    } else {
                        $newElements2[$columnKey['id']][] = $var;
                    }
                }
            }
            if ( ! empty( $row['reg_id'] )) {
                $property = \SchemaPropertyPeer::retrieveByPK($row['reg_id']);
                if ($property) {
                    $dbElements  = $property->getSchemaPropertyElementsRelatedBySchemaPropertyIdJoinProfileProperty();
                    $dbElements2 = [ ];
                    /** @var \SchemaPropertyElement $dbElement */
                    foreach ($dbElements as $dbElement) {
                        if ($dbElement->getProfileProperty()->getHasLanguage()) {
                            $dbElements2[$dbElement->getProfilePropertyId()][$dbElement->getLanguage()][] = &$dbElement;
                        } else {
                            $dbElements2[$dbElement->getProfilePropertyId()][] = &$dbElement;
                        }
                    }

                    /** @var \SchemaPropertyElement $element */
                    foreach ($dbElements as $element) {
                        $language          = $element->getLanguage();
                        $profilePropertyId = $element->getProfilePropertyId();
                        $key               = md5(strval($profilePropertyId) . strval($language) . $element->getObject());
                        //if the newelement key matches then
                        if (isset( $newElements[$key] )) {
                            if ($element->getProfileProperty()->getHasLanguage()) {
                                $newElements2Array = $newElements2[$profilePropertyId][$language];
                            } else {
                                $newElements2Array = $newElements2[$profilePropertyId];
                            }
                            $count = count($newElements2Array);
                            for ($I = 0; $I < $count; $I++) {
                                if ($newElements2Array[$I]['matchkey'] == $key) {
                                    unset( $newElements2Array[$I] );
                                }
                            }
                            unset( $newElements[$key] );
                            $element->importStatus = 'match';
                            continue;
                        } else {
                            if ($element->getProfileProperty()->getHasLanguage()) {
                                if (isset( $newElements2[$profilePropertyId][$language] )) {
                                    $count = count($newElements2[$profilePropertyId][$language]);
                                    for ($I = 0; $I < $count; $I++) {
                                        if ($newElements2[$profilePropertyId][$language][$I]['val']['val'] == $element->getObject()) {
                                            unset( $newElements2[$profilePropertyId][$language][$I] );
                                            $element->importStatus = 'match';
                                            if ( ! count($newElements2[$profilePropertyId][$language])) {
                                                unset( $newElements2[$profilePropertyId][$language] );
                                            }
                                            continue;
                                        }
                                    }
                                }
                            } else {
                                //compare the old values with the new with the same key
                                $count = count($newElements2[$profilePropertyId]);
                                for ($I = 0; $I < $count; $I++) {
                                    if (isset( $newElements2[$profilePropertyId][$I] )) {
                                        if ($newElements2[$profilePropertyId][$I]['val']['val'] == $element->getObject()) {
                                            unset( $newElements2[$profilePropertyId][$I] );
                                            $element->importStatus = 'match';
                                            continue;
                                        }
                                    }
                                }
                            }
                            //if the key matches then
                            //if the value matches
                            //delete the newElement
                            //else the value doesn't match
                            //if the newElement value is empty
                            //delete the dbElement
                        }

                        $element->matchKey = $key;
                    }
                    //update the property values
                    $property->save();
                } else {
                    //there's no existing property an we have to create a new one
                    $property = new \SchemaProperty();
                }
                foreach ($newElements as $key => $newElement) {
                    if ( ! empty( $newElement['id'] ) and ! isset( $oldElements[$key] )) {
                        $profileProperty = $newElement['property'];
                        //walk the old elements looking for a match on predicate + language
                        /** @var \SchemaPropertyElement[] $oldElement */
                        foreach ($dbElements as $oldElement) {
                            /** @var \SchemaPropertyElement $oldOne */
                            $oldOne = &$oldElement['element'];
                            if ($newElement['id'] == $oldOne->getProfilePropertyId()) {
                                /** @var \ProfileProperty $profileProperty */
                                if (( $profileProperty->getHasLanguage() and $newElement['lang'] == $oldOne->getLanguage() ) or ! $profileProperty->getHasLanguage()) {
                                    if ( ! empty( $newElement['val'] )) {
                                        $oldOne->setObject($newElement['val']);
                                        $oldOne->setUpdatedUserId($this->userId);
                                        $oldOne->setStatusId($row['status']);
                                        //$oldOne->save();
                                        $oldElement['status'] = "updated";
                                    } else {
                                        $oldOne->delete();
                                        $oldElement['status'] = "deleted";
                                    }
                                    //update the property value
                                    if ($profileProperty->getIsInForm()) {
                                        $this->setPropertyValue($newElement['val'],
                                                                $property,
                                                                $profileProperty->getName(),
                                                                ! $profileProperty->getIsObjectProp());
                                    }
                                    break;
                                }
                            }
                        }
                        //we looked through them all, add a new one
                        if ( ! empty( $newElement['val'] )) {
                            $addMe = new \SchemaPropertyElement();
                            $addMe->setObject($newElement['val']);
                            //$addMe->setSchemaPropertyRelatedBySchemaPropertyId($property);
                            $addMe->setCreatedUserId($this->userId);
                            $addMe->setUpdatedUserId($this->userId);
                            $addMe->setLanguage($newElement['lang']);
                            $addMe->setProfilePropertyId($newElement['id']);
                            $addMe->setStatusId($row['status']);
                            $addMe->importId = $this->importId;
                            //$addMe->save();
                            $property->addSchemaPropertyElementRelatedBySchemaPropertyId($addMe);
                            //update the property value
                            if ($profileProperty->getIsInForm()) {
                                $this->setPropertyValue($newElement['val'],
                                                        $property,
                                                        $profileProperty->getName(),
                                                        ! $profileProperty->getIsObjectProp());
                            }
                        }
                    }
                }
                //update the property
                if ($property) {
                    $property->setStatusId($row['status']);
                    $property->save();
                }
            }
            //var_dump($row);
        });
        $schemaWriter = new Writer\CallbackWriter(function ($row) {
            $this->setPrologColumns();

            if ( ! isset( $row[14] )) {
                $row[14] = $this->prolog['defaults']['statusId'];
            }
            $rowStatus = $row[14];
            $language  = $this->prolog['defaults']['lang'];

            foreach ($row as $key => &$element) {
                $this->updateRowUris($key, $element);
            }

            $uri      = $row[13];
            $property = null;
            $schemaId = $this->vocabId;

            if ( ! empty( $row['reg_id'] )) {
                $property = \SchemaPropertyPeer::retrieveByPK($row['reg_id']);
            } else {
                //check for an existing property by uri
                /** @var \SchemaProperty $property */
                $property = \SchemaPropertyPeer::retrieveByUri($uri);
                if ($property) {
                    $schemaId = $property->getSchemaId();
                }
            }

            //even if we found a property, we kill it if it's in a different schema than we're populating
            if ($property and $schemaId !== $this->vocabId) {
                //todo: we should log this event
                unset( $property );
            }

            if (empty( $property )) { //it's a new property
                $property = new \SchemaProperty();
                $property->setSchemaId($this->vocabId);
                $property->setCreatedUserId($this->userId);
                $property->setUpdatedUserId($this->userId);
                $property->setStatusId($rowStatus);
                $property->setLanguage($language);
                $property->save();
            }

            unset( $row['reg_id'] );

            if ($property) {
                //        if (8 == $rowStatus) {
//          //it's been deprecated and we don't do anything else
//          $property->setStatusId($rowStatus);
//          $this->updateElement($element, $dbElement, $property);
//        } else {
                $dbElements = $property->getElementsForImport($this->profileProperties);
                foreach ($dbElements as $key => $dbElement) {
                    /** @var string | array $rowElement */
                    $rowElement = isset( $row[$key] ) ? $row[$key] : null;
                    if (is_array($rowElement)) {
                        foreach ($rowElement as $elementKey => &$element) {
                            if ($this->updateElement($element, $dbElement, $property)) {
                                unset( $rowElement[$elementKey] );
                            }
                        }
                    } else {
                        if ($this->updateElement($rowElement, $dbElement, $property)) {
                            unset( $row[$key] );
                        }
                    }
                }
                foreach ($row as $key => $value) {
                    $dbElement = isset( $dbElements[$key] ) ? $dbElements[$key] : null;
                    if ( ! empty( $this->prolog['columns'][$key]['property'] )) {
                        $profileProperty = $this->prolog['columns'][$key]['property'];
                        if (is_array($value)) {
                            foreach ($value as &$oneValue) {
                                $language = $this->prolog['columns'][$key]['lang'][0];
                                $this->upsertElementFromRow($dbElement,
                                                            $oneValue,
                                                            $rowStatus,
                                                            $property,
                                                            $profileProperty,
                                                            $language,
                                                            $key);
                            }
                        } else {
                            $language = $this->prolog['columns'][$key]['lang'];
                            $this->upsertElementFromRow($dbElement,
                                                        $value,
                                                        $rowStatus,
                                                        $property,
                                                        $profileProperty,
                                                        $language,
                                                        $key);
                        }
                        if ($key == 'parent_class' and strtolower($property->getType()) == 'class' and $row[$key] != $property->getParentUri()) {
                            $property->setParentUri($row[$key]);
                            //we'll set this later
                            $property->setIsSubpropertyOf(null);
                        }
                        if ($key == 'parent_property' and strtolower($property->getType()) == 'property' and $row[$key] != $property->getParentUri()) {
                            $property->setParentUri($row[$key]);
                            //we'll set this later
                            $property->setIsSubpropertyOf(null);
                        }
                    }
                }
            }

            $affectedRows = $property->save();

//      }

            return;
            //build an array of references
            $newElements  = [ ];
            $newElements2 = [ ];
            if ( ! isset( $row['status'] )) {
                $row[14] = $this->prolog['defaults']['statusId'];
            }
            foreach ($row as $key => $element) {
                //skip it there's no property id
                $columnKey = $this->prolog['columns'][$key];
                if ( ! $columnKey['id']) {
                    continue;
                }

                if ( ! empty( $columnKey['type'] ) and $this->useCuries) {
                    $element = $this->getFqn($element);
                }

                $key2               = md5(strval($columnKey['id']) . strval($columnKey['lang']) . $element);
                $newElements[$key2] = [ ];
                $newElements[$key2] += $columnKey;
                $newElements[$key2]['val'] = $element;
                /** @var \ProfileProperty $profileProperty */
                if (isset( $columnKey['property'] )) {
                    $profileProperty = $columnKey['property'];
                    $var             = [
                        'matchkey' => $key2,
                        'val'      => $newElements[$key2],
                    ];
                    if (isset( $profileProperty ) and $profileProperty->getHasLanguage()) {
                        $newElements2[$columnKey['id']][$columnKey['lang']][] = $var;
                    } else {
                        $newElements2[$columnKey['id']][] = $var;
                    }
                }
            }
            if ( ! empty( $row['reg_id'] )) {
                $property = \SchemaPropertyPeer::retrieveByPK($row['reg_id']);
                if ($property) {
                    $dbElements  = $property->getSchemaPropertyElementsRelatedBySchemaPropertyIdJoinProfileProperty();
                    $dbElements2 = [ ];
                    /** @var \SchemaPropertyElement $dbElement */
                    foreach ($dbElements as $dbElement) {
                        if ($dbElement->getProfileProperty()->getHasLanguage()) {
                            $dbElements2[$dbElement->getProfilePropertyId()][$dbElement->getLanguage()][] = &$dbElement;
                        } else {
                            $dbElements2[$dbElement->getProfilePropertyId()][] = &$dbElement;
                        }
                    }

                    /** @var \SchemaPropertyElement $element */
                    foreach ($dbElements as $element) {
                        $language          = $element->getLanguage();
                        $profilePropertyId = $element->getProfilePropertyId();
                        $key               = md5(strval($profilePropertyId) . strval($language) . $element->getObject());
                        //if the newelement key matches then
                        if (isset( $newElements[$key] )) {
                            if ($element->getProfileProperty()->getHasLanguage()) {
                                $newElements2Array = $newElements2[$profilePropertyId][$language];
                            } else {
                                $newElements2Array = $newElements2[$profilePropertyId];
                            }
                            $count = count($newElements2Array);
                            for ($I = 0; $I < $count; $I++) {
                                if ($newElements2Array[$I]['matchkey'] == $key) {
                                    unset( $newElements2Array[$I] );
                                }
                            }
                            unset( $newElements[$key] );
                            $element->importStatus = 'match';
                            continue;
                        } else {
                            if ($element->getProfileProperty()->getHasLanguage()) {
                                if (isset( $newElements2[$profilePropertyId][$language] )) {
                                    $count = count($newElements2[$profilePropertyId][$language]);
                                    for ($I = 0; $I < $count; $I++) {
                                        if ($newElements2[$profilePropertyId][$language][$I]['val']['val'] == $element->getObject()) {
                                            unset( $newElements2[$profilePropertyId][$language][$I] );
                                            $element->importStatus = 'match';
                                            if ( ! count($newElements2[$profilePropertyId][$language])) {
                                                unset( $newElements2[$profilePropertyId][$language] );
                                            }
                                            continue;
                                        }
                                    }
                                }
                            } else {
                                //compare the old values with the new with the same key
                                $count = count($newElements2[$profilePropertyId]);
                                for ($I = 0; $I < $count; $I++) {
                                    if (isset( $newElements2[$profilePropertyId][$I] )) {
                                        if ($newElements2[$profilePropertyId][$I]['val']['val'] == $element->getObject()) {
                                            unset( $newElements2[$profilePropertyId][$I] );
                                            $element->importStatus = 'match';
                                            continue;
                                        }
                                    }
                                }
                            }
                            //if the key matches then
                            //if the value matches
                            //delete the newElement
                            //else the value doesn't match
                            //if the newElement value is empty
                            //delete the dbElement
                        }

                        $element->matchKey = $key;
                    }
                    //update the property values
                    $property->save();
                } else {
                    //there's no existing property an we have to create a new one
                    $property = new \SchemaProperty();
                }
                foreach ($newElements as $key => $newElement) {
                    if ( ! empty( $newElement['id'] ) and ! isset( $oldElements[$key] )) {
                        $profileProperty = $newElement['property'];
                        //walk the old elements looking for a match on predicate + language
                        /** @var \SchemaPropertyElement[] $oldElement */
                        foreach ($dbElements as $oldElement) {
                            /** @var \SchemaPropertyElement $oldOne */
                            $oldOne = &$oldElement['element'];
                            if ($newElement['id'] == $oldOne->getProfilePropertyId()) {
                                /** @var \ProfileProperty $profileProperty */
                                if (( $profileProperty->getHasLanguage() and $newElement['lang'] == $oldOne->getLanguage() ) or ! $profileProperty->getHasLanguage()) {
                                    if ( ! empty( $newElement['val'] )) {
                                        $oldOne->setObject($newElement['val']);
                                        $oldOne->setUpdatedUserId($this->userId);
                                        $oldOne->setStatusId($row['status']);
                                        //$oldOne->save();
                                        $oldElement['status'] = "updated";
                                    } else {
                                        $oldOne->delete();
                                        $oldElement['status'] = "deleted";
                                    }
                                    //update the property value
                                    if ($profileProperty->getIsInForm()) {
                                        $this->setPropertyValue($newElement['val'],
                                                                $property,
                                                                $profileProperty->getName(),
                                                                ! $profileProperty->getIsObjectProp());
                                    }
                                    break;
                                }
                            }
                        }
                        //we looked through them all, add a new one
                        if ( ! empty( $newElement['val'] )) {
                            $addMe = new \SchemaPropertyElement();
                            $addMe->setObject($newElement['val']);
                            //$addMe->setSchemaPropertyRelatedBySchemaPropertyId($property);
                            $addMe->setCreatedUserId($this->userId);
                            $addMe->setUpdatedUserId($this->userId);
                            $addMe->setLanguage($newElement['lang']);
                            $addMe->setProfilePropertyId($newElement['id']);
                            $addMe->setStatusId($row['status']);
                            $addMe->importId = $this->importId;
                            //$addMe->save();
                            $property->addSchemaPropertyElementRelatedBySchemaPropertyId($addMe);
                            //update the property value
                            if ($profileProperty->getIsInForm()) {
                                $this->setPropertyValue($newElement['val'],
                                                        $property,
                                                        $profileProperty->getName(),
                                                        ! $profileProperty->getIsObjectProp());
                            }
                        }
                    }
                }
                //update the property
                if ($property) {
                    $property->setStatusId($row['status']);
                    $property->save();
                }
            }
            //var_dump($row);
        });

        $workflow->addItemConverter($this->mapping);
        $workflow->addFilter($filter);
        $workflow->addItemConverter($trimConverter);
        $workflow->addItemConverter($lexicalConverter);
        $workflow->addWriter(new Writer\ConsoleProgressWriter($output, $this->reader));
        $workflow->addValueConverter("4", $typeConverter);
        //add a database writer
        if ('schema' == $this->type) {
            $workflow->addWriter($schemaWriter);
        } else {
            $workflow->addWriter($vocabWriter);
        }
        /** @todo we need to make a second pass through to delete missing rows
         * for each schemaproperty in the database
         *   match to a row in the csv
         *   if the row value is empty and $this->deleteMissing
         *     delete the entire schemaproperty
         */

        $workResults               = $workflow->process();
        $this->DataWorkflowResults = $workResults;

        /** @TODO need to make a second pass through to lookup and set $element->related_schema_property_id */
        $this->processParents($this->vocabId);
        //use the prolog to configure namespaces, look up correct resources in the database
        //store the row number of the first non-meta line

        return $workResults;
    }
 /**
  * Retrieve multiple objects by pkey.
  *
  * @param      array $pks List of primary keys
  * @param      Connection $con the connection to use
  * @throws     PropelException Any exceptions caught during processing will be
  *		 rethrown wrapped into a PropelException.
  */
 public static function retrieveByPKs($pks, $con = null)
 {
     if ($con === null) {
         $con = Propel::getConnection(self::DATABASE_NAME);
     }
     $objs = null;
     if (empty($pks)) {
         $objs = array();
     } else {
         $criteria = new Criteria();
         $criteria->add(ConceptPropertyPeer::ID, $pks, Criteria::IN);
         $objs = ConceptPropertyPeer::doSelect($criteria, $con);
     }
     return $objs;
 }
 public function execute(&$value, &$error)
 {
     $property = $this->getContext()->getRequest()->getParameter('concept_property');
     $propertyId = $this->getContext()->getRequest()->getParameter('id');
     $conceptId = $this->getContext()->getRequest()->getParameter('concept_id');
     $vocabId = $this->getContext()->getUser()->getAttribute('vocabulary')->getId();
     //if it's not a prefLabel then we just check for uniqueness in the context of the Concept
     if ((int) $property['skos_property_id'] !== 19) {
         $c = new Criteria();
         $c->add(ConceptPropertyPeer::CONCEPT_ID, $conceptId);
         $c->add(ConceptPropertyPeer::LANGUAGE, $property['language']);
         $c->add(ConceptPropertyPeer::STATUS_ID, $property['status_id']);
         $c->add(ConceptPropertyPeer::OBJECT, $value);
         $object = ConceptPropertyPeer::doSelectOne($c);
         if ($object) {
             //check to see if the retrieved object has the same id
             if ($propertyId && $object->getId() == $propertyId) {
                 return true;
             } else {
                 if ($property['skos_property_id'] != $object->getSkosPropertyId()) {
                     $error = "This Concept already has this label applied to the " . $object->getSkosPropertyName() . " property.";
                 } else {
                     $error = "This Concept already has this exact property.";
                 }
                 return false;
             }
         } else {
             if (!in_array($property['skos_property_id'], array('3', '16', '21', 32, 33, 34, 35, 36, 37))) {
                 $property['related_concept_id'] = null;
                 $property['scheme_id'] = null;
                 //save the array back to the request parameter
                 //         $this->requestParameterHolder->set('concept_property', $concept_property);
             }
         }
     } else {
         //check for no duplicate prefLable in the entire vocabulary
         $c = new Criteria();
         $c->add(ConceptPeer::VOCABULARY_ID, $vocabId);
         $c->add(ConceptPropertyPeer::LANGUAGE, $property['language']);
         $c->add(ConceptPropertyPeer::STATUS_ID, $property['status_id']);
         $c->add(ConceptPropertyPeer::SKOS_PROPERTY_ID, 19);
         $c->add(ConceptPropertyPeer::OBJECT, $value);
         $c->addJoin(ConceptPropertyPeer::CONCEPT_ID, ConceptPeer::ID);
         $object = ConceptPropertyPeer::doSelectOne($c);
         if ($object) {
             //check to see if the retrieved object has the same id
             if ($propertyId && $object->getId() == $propertyId) {
                 return true;
             } else {
                 $error = "This Vocabulary already has a Concept with a prefLabel with this status and language.";
                 return false;
             }
         }
         $c = new Criteria();
         $c->add(ConceptPropertyPeer::CONCEPT_ID, $conceptId);
         $c->add(ConceptPropertyPeer::LANGUAGE, $property['language']);
         $c->add(ConceptPropertyPeer::STATUS_ID, $property['status_id']);
         $c->add(ConceptPropertyPeer::SKOS_PROPERTY_ID, 19);
         $objects = ConceptPropertyPeer::doSelect($c);
         if ($objects && ($propertyId && count($objects) > 1 or !$propertyId && count($objects))) {
             $error = "This Concept already has a prefLabel with this status and language.";
             return false;
         }
     }
     return true;
 }
 /**
  * gets the current concept object
  *
  * @return mixed current concept object, or false
  */
 public static function findCurrentConcept()
 {
     $instance = sfContext::getInstance();
     $user = $instance->getUser();
     $request = $instance->getRequest();
     $action = $instance->getActionStack()->getLastEntry()->getActionInstance();
     $attributeHolder = $user->getAttributeHolder();
     //check if there's a request parameter
     $conceptId = $request->getParameter('concept_id', '');
     //concept_id's in the query string
     if ($conceptId) {
         self::updateAdminFilters($attributeHolder, 'concept_id', $conceptId, 'concept_property');
     }
     //concept_id's not in the query string, but it's in a filter
     //note: this will still return the correct value if it's in the query string
     $conceptId = $attributeHolder->get('concept_id', '', 'sf_admin/concept_property/filters');
     $concept = $user->getCurrentConcept();
     //We got here and there's a concept_id but we didn't get the stored concept object
     if ($conceptId && !$concept) {
         //we get it from the database
         $concept = self::setLatestConcept($conceptId);
     }
     //we got here and there's a concept and a conceptid (yay)
     if ($concept and $conceptId) {
         //let's check the id of the stored concept
         $currentId = $concept->getId();
         //but what if the id of that concept doesn't match the one we have
         if ($currentId != $conceptId) {
             //we set the stored object to be the one we know
             $concept = self::setLatestConcept($conceptId);
         }
     }
     //still no concept!!!
     if (!$concept) {
         $idType = $request->getParameter('IdType', null);
         $id = $request->getParameter('id', null);
         switch ($idType) {
             case "property_id":
                 $property = ConceptPropertyPeer::retrieveByPK($id);
                 $conceptId = $property->getConceptId();
                 $concept = self::setLatestConcept($conceptId);
                 self::updateAdminFilters($attributeHolder, 'concept_id', $conceptId, 'concept_property');
                 break;
             case "concept_id":
                 break;
             default:
         }
     }
     //if we get here and there's still no vocabulary then we return false
     $concept = isset($concept) ? $concept : false;
     return $concept;
 }
/**
 * updatedb batch script
 *
 * This script is used to update the database data as needed
 *
 * @package    registry
 * @subpackage batch
 * @version    $Id$
 */
define('SF_ROOT_DIR', realpath(dirname(__FILE__) . '/..'));
define('SF_APP', 'frontend');
define('SF_ENVIRONMENT', 'test');
define('SF_DEBUG', true);
require_once SF_ROOT_DIR . DIRECTORY_SEPARATOR . 'apps' . DIRECTORY_SEPARATOR . SF_APP . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php';
// initialize database manager
$databaseManager = new sfDatabaseManager();
$databaseManager->initialize();
// batch process here
//get the concept properties
$foo = new ConceptPropertyPeer();
$selectCriteria = new Criteria(VocabularyPeer::DATABASE_NAME);
$rs = $foo->doSelect($selectCriteria);
foreach ($rs as $conceptProperty) {
    /** @var ConceptProperty **/
    $historys = $conceptProperty->getConceptPropertyHistorys();
    if (count($historys) == 0) {
        debugbreak;
    }
}
echo "Done!";
Exemplo n.º 15
0
    public function findUsedVocabularyProfileProperties()
    {
        $c = new \Criteria();
        $c->add( \ConceptPeer::VOCABULARY_ID, $this->getSchema()->getId() );
        $c->clearSelectColumns();
        $c->addSelectColumn( \ConceptPropertyPeer::CONCEPT_ID );
        $c->addSelectColumn( \ProfilePropertyPeer::ID );
        $c->addSelectColumn( \ConceptPropertyPeer::LANGUAGE );
        $c->addAscendingOrderByColumn( \ConceptPropertyPeer::CONCEPT_ID );
        $c->addJoin( \ConceptPropertyPeer::CONCEPT_ID, \ConceptPeer::ID );
        $c->addJoin( \ConceptPropertyPeer::SKOS_PROPERTY_ID, \ProfilePropertyPeer::SKOS_ID );

        $foo     = array();
        $results = \ConceptPropertyPeer::doSelectRS( $c );
        unset( $c );

        foreach ( $results as $result )
        {
            if ( ! isset( $foo[$result[0]][$result[1]][$result[2]] ) )
            {
                $foo[$result[0]][$result[1]][$result[2]] = 1;
            }
            else
            {
                $foo[$result[0]][$result[1]][$result[2]] ++;
            }
        }

        $bar = self::buildColumnArray( $foo );

        return $bar;
    }
 /**
  * Selects a collection of ConceptPropertyHistory objects pre-filled with all related objects except FileImportHistory.
  *
  * @return array Array of ConceptPropertyHistory objects.
  * @throws PropelException Any exceptions caught during processing will be
  *		 rethrown wrapped into a PropelException.
  */
 public static function doSelectJoinAllExceptFileImportHistory(Criteria $c, $con = null)
 {
     $c = clone $c;
     // Set the correct dbName if it has not been overridden
     // $c->getDbName() will return the same object if not set to another value
     // so == check is okay and faster
     if ($c->getDbName() == Propel::getDefaultDB()) {
         $c->setDbName(self::DATABASE_NAME);
     }
     ConceptPropertyHistoryPeer::addSelectColumns($c);
     $startcol2 = ConceptPropertyHistoryPeer::NUM_COLUMNS - ConceptPropertyHistoryPeer::NUM_LAZY_LOAD_COLUMNS + 1;
     ConceptPropertyPeer::addSelectColumns($c);
     $startcol3 = $startcol2 + ConceptPropertyPeer::NUM_COLUMNS;
     ConceptPeer::addSelectColumns($c);
     $startcol4 = $startcol3 + ConceptPeer::NUM_COLUMNS;
     VocabularyPeer::addSelectColumns($c);
     $startcol5 = $startcol4 + VocabularyPeer::NUM_COLUMNS;
     SkosPropertyPeer::addSelectColumns($c);
     $startcol6 = $startcol5 + SkosPropertyPeer::NUM_COLUMNS;
     VocabularyPeer::addSelectColumns($c);
     $startcol7 = $startcol6 + VocabularyPeer::NUM_COLUMNS;
     ConceptPeer::addSelectColumns($c);
     $startcol8 = $startcol7 + ConceptPeer::NUM_COLUMNS;
     StatusPeer::addSelectColumns($c);
     $startcol9 = $startcol8 + StatusPeer::NUM_COLUMNS;
     UserPeer::addSelectColumns($c);
     $startcol10 = $startcol9 + UserPeer::NUM_COLUMNS;
     $c->addJoin(ConceptPropertyHistoryPeer::CONCEPT_PROPERTY_ID, ConceptPropertyPeer::ID);
     $c->addJoin(ConceptPropertyHistoryPeer::CONCEPT_ID, ConceptPeer::ID);
     $c->addJoin(ConceptPropertyHistoryPeer::VOCABULARY_ID, VocabularyPeer::ID);
     $c->addJoin(ConceptPropertyHistoryPeer::SKOS_PROPERTY_ID, SkosPropertyPeer::ID);
     $c->addJoin(ConceptPropertyHistoryPeer::SCHEME_ID, VocabularyPeer::ID);
     $c->addJoin(ConceptPropertyHistoryPeer::RELATED_CONCEPT_ID, ConceptPeer::ID);
     $c->addJoin(ConceptPropertyHistoryPeer::STATUS_ID, StatusPeer::ID);
     $c->addJoin(ConceptPropertyHistoryPeer::CREATED_USER_ID, UserPeer::ID);
     $rs = BasePeer::doSelect($c, $con);
     $results = array();
     while ($rs->next()) {
         $omClass = ConceptPropertyHistoryPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj1 = new $cls();
         $obj1->hydrate($rs);
         $omClass = ConceptPropertyPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj2 = new $cls();
         $obj2->hydrate($rs, $startcol2);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj2 = $temp_obj1->getConceptProperty();
             //CHECKME
             if ($temp_obj2->getPrimaryKey() === $obj2->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj2->addConceptPropertyHistory($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj2->initConceptPropertyHistorys();
             $obj2->addConceptPropertyHistory($obj1);
         }
         $omClass = ConceptPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj3 = new $cls();
         $obj3->hydrate($rs, $startcol3);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj3 = $temp_obj1->getConceptRelatedByConceptId();
             //CHECKME
             if ($temp_obj3->getPrimaryKey() === $obj3->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj3->addConceptPropertyHistoryRelatedByConceptId($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj3->initConceptPropertyHistorysRelatedByConceptId();
             $obj3->addConceptPropertyHistoryRelatedByConceptId($obj1);
         }
         $omClass = VocabularyPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj4 = new $cls();
         $obj4->hydrate($rs, $startcol4);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj4 = $temp_obj1->getVocabularyRelatedByVocabularyId();
             //CHECKME
             if ($temp_obj4->getPrimaryKey() === $obj4->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj4->addConceptPropertyHistoryRelatedByVocabularyId($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj4->initConceptPropertyHistorysRelatedByVocabularyId();
             $obj4->addConceptPropertyHistoryRelatedByVocabularyId($obj1);
         }
         $omClass = SkosPropertyPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj5 = new $cls();
         $obj5->hydrate($rs, $startcol5);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj5 = $temp_obj1->getSkosProperty();
             //CHECKME
             if ($temp_obj5->getPrimaryKey() === $obj5->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj5->addConceptPropertyHistory($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj5->initConceptPropertyHistorys();
             $obj5->addConceptPropertyHistory($obj1);
         }
         $omClass = VocabularyPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj6 = new $cls();
         $obj6->hydrate($rs, $startcol6);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj6 = $temp_obj1->getVocabularyRelatedBySchemeId();
             //CHECKME
             if ($temp_obj6->getPrimaryKey() === $obj6->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj6->addConceptPropertyHistoryRelatedBySchemeId($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj6->initConceptPropertyHistorysRelatedBySchemeId();
             $obj6->addConceptPropertyHistoryRelatedBySchemeId($obj1);
         }
         $omClass = ConceptPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj7 = new $cls();
         $obj7->hydrate($rs, $startcol7);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj7 = $temp_obj1->getConceptRelatedByRelatedConceptId();
             //CHECKME
             if ($temp_obj7->getPrimaryKey() === $obj7->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj7->addConceptPropertyHistoryRelatedByRelatedConceptId($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj7->initConceptPropertyHistorysRelatedByRelatedConceptId();
             $obj7->addConceptPropertyHistoryRelatedByRelatedConceptId($obj1);
         }
         $omClass = StatusPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj8 = new $cls();
         $obj8->hydrate($rs, $startcol8);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj8 = $temp_obj1->getStatus();
             //CHECKME
             if ($temp_obj8->getPrimaryKey() === $obj8->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj8->addConceptPropertyHistory($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj8->initConceptPropertyHistorys();
             $obj8->addConceptPropertyHistory($obj1);
         }
         $omClass = UserPeer::getOMClass();
         $cls = Propel::import($omClass);
         $obj9 = new $cls();
         $obj9->hydrate($rs, $startcol9);
         $newObject = true;
         for ($j = 0, $resCount = count($results); $j < $resCount; $j++) {
             $temp_obj1 = $results[$j];
             $temp_obj9 = $temp_obj1->getUser();
             //CHECKME
             if ($temp_obj9->getPrimaryKey() === $obj9->getPrimaryKey()) {
                 $newObject = false;
                 $temp_obj9->addConceptPropertyHistory($obj1);
                 break;
             }
         }
         if ($newObject) {
             $obj9->initConceptPropertyHistorys();
             $obj9->addConceptPropertyHistory($obj1);
         }
         $results[] = $obj1;
     }
     return $results;
 }
/**
 * @param $task
 * @param $args
 *
 * Arg[0] is one of "schema" (element set), "vocab" or "vocabulary"
 * arg[1] is the vocabulary name.
 *        The file type is determined by the extension and must be one of "json", "rdf", "csv", "xml"
 * arg[2] is the vocabulary id
 * arg[3] is the batch id
 * arg[4] [optional] is -d
 *
 * @throws Exception
 */
function run_import_vocabulary($task, $args)
{
    //xdebug_break();

    //check the argument counts
    if (count($args) < 1) {
        throw new Exception('You must provide a vocabulary type.');
    }

    if (count($args) < 2) {
        throw new Exception('You must provide a file name.');
    }

    if (count($args) < 3) {
        throw new Exception('You must provide a vocabulary id.');
    }

    //set the arguments
    $type          = strtolower($args[0]);
    $filePath      = $args[1];
    $vocabId       = $args[2];
    $batchId       = isset($args[3]) ? $args[3] : "";
    $deleteMissing = (isset($args[4]) && ("-d" == $args[4]));

    //do some basic validity checks

    if (! in_array(
      $type,
      array(
        "schema",
        "vocab",
        "vocabulary"
      )
    )
    ) {
        throw new Exception('You must import into a schema or a vocab');
    }

    if ("vocabulary" == $type) {
        $type = "vocab";
    }

    if (! is_numeric($vocabId)) {
        throw new Exception('You must provide a valid ID');
    }

    //does the file exist?
    if (! file_exists($filePath)) {
        //default to the site upload path
        $filePath = $GLOBALS['uploadPath'] . $filePath;
        if (! file_exists($filePath)) {
        throw new Exception('You must supply a valid file to import: ' . $filePath);
        }
    }

    //is the file a valid type?
    if (preg_match('/^.+\.([[:alpha:]]{2,4})$/', $filePath, $matches)) {
        if (! in_array(
          strtolower($matches[1]),
          array(
            "json",
            "rdf",
            "csv",
            "xml"
          )
        )
        ) {
            throw new Exception('You must provide a valid file type based on the extension');
        }
    } else {
        throw new Exception("File type cannot be determined from the file extension");
    }

    $fileType = $matches[1];

    //is the object a valid object?
    if ('vocab' == $type) {
        $vocabObj = VocabularyPeer::retrieveByPK($vocabId);
        if (is_null($vocabObj)) {
            throw new Exception('Invalid vocabulary ID');
        }

        //set some defaults
        $baseDomain = $vocabObj->getBaseDomain();
        $language   = $vocabObj->getLanguage();
        $statusId   = $vocabObj->getStatusId();
        $userId     = $vocabObj->getCreatedUserId();
        $agentId    = $vocabObj->getAgentId();

        //get a skos property id map
        $skosMap = SkosPropertyPeer::getPropertyNames();

        //there has to be a hash or a slash
        $tSlash = preg_match('@(/$)@i', $vocabObj->getUri()) ? '' : '/';
        $tSlash = preg_match('/#$/', $vocabObj->getUri()) ? '' : $tSlash;
    } else {
        $import               = new ImportVocab($type, $filePath, $vocabId);
    }

    /* From here on the process is the same regardless of UI */
    //     check to see if file has been uploaded before
    //          check import history for file name
    $importHistory = FileImportHistoryPeer::retrieveByLastFilePath($filePath);
    //          if reimport
    //               get last import history for filename
    //               unserialize column map
    //               match column names to AP based on map
    //     look for matches in unmatched field/column names to AP (ideal)
    //     csv table of data --
    //          row1: parsed field names/column headers
    //          row2: select dropdown with available fields from object AP (pre-select known matches)
    //                each select identified by column number
    //          row3: display datatype of selected field (updated dynamically when field selected)
    //          row4-13: first 10 rows of parsed data from file
    //     require a column that can match to 'URI' (maybe we'll allow an algorithm later)
    //     require columns that are required by AP
    //     on reimport there should be a flag to 'delete missing properties' from the current data
    //     note: at some point there will be a reimport process that allows URI changing
    //          this will require that there be an OMR identifier embedded in the incoming data

    switch ($fileType) {
        case "csv":
            if ('vocab' == $type) {
                // Get array of heading names found
                $headings = $reader->getHeadings();
                $fields   = ConceptPeer::getFieldNames();

                //set the map
                //      $map[] = array("property" => "Uri", "column" => "URILocal");
                //      $map[] = array("property" => "prefLabel", "column" => "skos:prefLabel");
                //      $map[] = array("property" => "definition", "column" => "skos:definition");
                //      $map[] = array("property" => "notation", "column" => "skos:notation");
                //      $map[] = array("property" => "scopeNote", "column" => "skos:scopeNote");

                $map = array(
                  "uri"        => "URILocal",
                  "prefLabel"  => "skos:prefLabel",
                  "definition" => "skos:definition",
                  "notation"   => "skos:notation",
                  "scopeNote"  => "skos:scopeNote"
                );

                $rows = 0;

                //executeImport:

                //    serialize the column map
                try {
                    while ($row = $reader->getRow()) {
                        $rows ++;
                        //        lookup the URI (or the OMR ID if available) for a match
                        $uri        = $baseDomain . $row[$map["uri"]];
                        $concept    = ConceptPeer::getConceptByUri($uri);
                        $updateTime = time();
                        $language   = (isset($map['language'])) ? $row[$map['language']] : $vocabObj->getLanguage();

                        if (! $concept) {
                            //          create a new concept or element
                            $concept = new Concept();
                            $concept->setVocabulary($vocabObj);
                            $concept->setUri($uri);
                            /**
                             * @todo Need to handle updates for topconcept here, just like language
                             **/
                            $concept->setIsTopConcept(false);
                            $concept->updateFromRequest(
                                    $userId,
                                      fixEncoding(rtrim($row[$map['prefLabel']])),
                                      $language,
                                      $statusId
                            );
                        } //don't update the concept if the preflabel matches
                        else if ($row[$map['prefLabel']] != $concept->getPrefLabel()) {
                            $concept->updateFromRequest($userId, fixEncoding(rtrim($row[$map['prefLabel']])));
                        }

                        //there needs to be a language to lookup the properties unless it's an objectProperty
                        $rowLanguage = (isset($map['language'])) ? $row[$map['language']] : $concept->getLanguage();

                        foreach ($map as $key => $value) {
                            //we skip because we already did them
                            if (! in_array(
                              $key,
                              array(
                                'uri',
                                'prefLabel',
                                'language'
                              )
                            )
                            ) {
                                $skosId = $skosMap[$key];
                                //check to see if the property already exists
                                $property =
                                  ConceptPropertyPeer::lookupProperty($concept->getId(), $skosId, $rowLanguage);

                                //create a new property for each unmatched column
                                if (! empty($row[$value])) {
                                    if (! $property) {
                                        $property = new ConceptProperty();
                                        $property->setCreatedUserId($userId);
                                        $property->setConceptId($concept->getId());
                                        $property->setCreatedAt($updateTime);
                                        $property->setSkosPropertyId($skosId);
                                    }

                                    if (($row[$value] != $property->getObject()) ||
                                        ($rowLanguage != $property->getLanguage())
                                    ) {
                                        /**
                                         * @todo We need a check here for skos objectproperties and handle differently
                                         **/
                                        if ($rowLanguage != $property->getLanguage()) {
                                            $property->setLanguage($rowLanguage);
                                        }
                                        if ($row[$value] != $property->getObject()) {
                                            $property->setObject(fixEncoding(rtrim($row[$value])));
                                        }
                                        $property->setUpdatedUserId($userId);
                                        $property->setUpdatedAt($updateTime);
                                        $property->save();
                                    }
                                } //the row value is empty
                                else if ($deleteMissing && $property) {
                                    $property->delete();
                                }
                            }
                        }

                        //          else
                        //               lookup and update concept or element
                        //               lookup and update each property
                        //          update the history for each property, action is 'import', should be a single timestamp for all (this should be automatic)
                        //          if 'delete missing properties' is true
                        //               delete each existing, non-required property that wasn't updated by the import
                    }
                } catch(Exception $e) {
                    //          catch
                    //            if there's an error of any kind, write to error log and continue
                    echo "Error on row: " . $rows . ", " . $uri . "\n" . $e . "\n";
                    continue;
                }
                $objects = $vocabObj->countConcepts();
            } else //it's an element set
            {
                $import->setCsvReader($import->file);
                $import->processProlog();
                $import->getDataColumnIds();
                $import->processData();
                //todo: $results should be a class
                $results[$vocabId] = $import->results;
                $bacthId = $import->saveResults($batchId);
            }
            break;
        case "json":
            break;
        case "rdf":
            break;
        case "xml":
            break;
        default:
    }

    /* output to stdout*/
    //          number of objects imported (link to history, filtered on timestamp of import)
    echo " Rows imported: " . count($results[$vocabId]['success']['rows']) . "\n From File:" . $filePath . "\nUse this ID for more in this batch: " . $bacthId;
    //          number of errors (link to error log)

}
Exemplo n.º 18
0
 /**
  * checks for replated property and deletes if found
  *
  * @return return_type
  * @param  var_type $var
  */
 private function deleteReciprocalProperty($propertyId, $relatedIdFromRequest = '')
 {
     //retrieve the existing property
     if (isset($propertyId)) {
         /* @var ConceptProperty */
         $currentProperty = ConceptPropertyPeer::retrieveByPk($propertyId);
     }
     if (isset($currentProperty)) {
         //check to see if it defines a relationship
         $currentRelatedSchemeId = $currentProperty->getSchemeId();
         $currentRelatedConceptId = $currentProperty->getRelatedConceptId();
         $currentRelatedConceptInverseSkosId = $currentProperty->getProfileProperty()->getInverseProfilePropertyId();
         //if it does and it doesn't match the current relationship
         if (isset($currentRelatedConceptId) && $currentRelatedConceptId != $relatedIdFromRequest) {
             //retrieve the related property
             $c = new Criteria();
             $c->add(ConceptPropertyPeer::CONCEPT_ID, $currentRelatedConceptId);
             $c->add(ConceptPropertyPeer::SKOS_PROPERTY_ID, $currentRelatedConceptInverseSkosId);
             /* @var ConceptProperty */
             $relatedProperty = ConceptPropertyPeer::doSelectOne($c);
             if (isset($relatedProperty)) {
                 //then delete it
                 try {
                     $relatedProperty->delete();
                 } catch (PropelException $e) {
                     $this->getRequest()->setError('delete', 'Could not delete the related Concept Property.');
                 }
             }
         }
     }
     return;
 }