Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 3b6ade7

Browse files
author
Julien Jomier
committed
ENH: Added support for Metadata
1 parent f5da33a commit 3b6ade7

File tree

7 files changed

+218
-1
lines changed

7 files changed

+218
-1
lines changed

core/constant/metadata.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
define("MIDAS_METADATA_GLOBAL", 0);
3+
define("MIDAS_METADATA_DOCUMENT", 1);
4+
define("MIDAS_METADATA_VIDEO", 2);
5+
define("MIDAS_METADATA_IMAGE", 3);
6+
?>

core/controllers/components/MIDAS2MigrationComponent.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require_once BASE_PATH.'/core/models/dao/ItemRevisionDao.php';
33
require_once BASE_PATH.'/core/models/dao/BitstreamDao.php';
44
require_once BASE_PATH.'/core/models/dao/ItemDao.php';
5+
require_once BASE_PATH.'/core/models/dao/MetadataDao.php';
56
require_once BASE_PATH.'/core/models/dao/AssetstoreDao.php';
67
require_once BASE_PATH.'/core/controllers/components/UploadComponent.php';
78

@@ -275,7 +276,8 @@ function migrate($userid)
275276
{
276277
set_time_limit(0);
277278
$this->userId = $userid;
278-
279+
280+
279281
// Check that we are in development mode
280282
if(Zend_Registry::get('configGlobal')->environment != 'development')
281283
{
@@ -298,6 +300,18 @@ function migrate($userid)
298300

299301
$modelLoader = new MIDAS_ModelLoader;
300302

303+
// Just to test the metadata
304+
$MetadataModel = $modelLoader->loadModel("Metadata");
305+
$Item = $modelLoader->loadModel("Item");
306+
307+
$itemDao = $Item->load(42);
308+
$itemRevisionDao = $Item->getLastRevision($itemDao);
309+
//$MetadataModel->addMetadata(MIDAS_METADATA_DOCUMENT,'contributor','author','Author of a text');
310+
$MetadataModel->addMetadataValue($itemRevisionDao,MIDAS_METADATA_DOCUMENT,
311+
'contributor','author','Julien!');
312+
313+
return false;
314+
301315
// STEP 1: Import the users
302316
$User = $modelLoader->loadModel("User");
303317
$query = pg_query("SELECT email,password,firstname,lastname FROM eperson");

core/database/upgrade/3.0.13.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
class Upgrade_3_0_13 extends MIDASUpgrade
4+
{
5+
public function preUpgrade()
6+
{
7+
$this->AddTableField('metadatadocumentvalue', 'metadatavalue_id', 'bigint(20)', 'serial',false);
8+
$this->AddTablePrimaryKey('metadatadocumentvalue', 'metadatavalue_id');
9+
$this->AddTableField('metadatavalue', 'metadatavalue_id', 'bigint(20)', 'serial',false);
10+
$this->AddTablePrimaryKey('metadatavalue', 'metadatavalue_id');
11+
}
12+
13+
public function mysql()
14+
{
15+
$this->db->query("ALTER TABLE `metadatadocumentvalue` CHANGE `metadatavalue_id`
16+
`metadatavalue_id` BIGINT( 20 ) NOT NULL AUTO_INCREMENT");
17+
$this->db->query("ALTER TABLE `metadatavalue` CHANGE `metadatavalue_id`
18+
`metadatavalue_id` BIGINT( 20 ) NOT NULL AUTO_INCREMENT");
19+
}
20+
21+
22+
public function pgsql()
23+
{
24+
}
25+
26+
public function postUpgrade()
27+
{
28+
}
29+
}
30+
?>
31+
32+

core/include.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
include_once BASE_PATH.'/core/constant/folder.php';
1919
include_once BASE_PATH.'/core/constant/feed.php';
2020
include_once BASE_PATH.'/core/constant/license.php';
21+
include_once BASE_PATH.'/core/constant/metadata.php';
2122
include_once BASE_PATH.'/core/constant/notification.php';
2223
include_once BASE_PATH.'/core/constant/user.php';
2324
include_once BASE_PATH.'/core/constant/resourcetype.php';
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/** Metadata Model Base */
3+
abstract class MetadataModelBase extends AppModel
4+
{
5+
/** Constructor*/
6+
public function __construct()
7+
{
8+
parent::__construct();
9+
$this->_name = 'metadata';
10+
$this->_key = 'metadata_id';
11+
$this->_mainData = array(
12+
'metadata_id' => array('type' => MIDAS_DATA),
13+
'metadatatype' => array('type' => MIDAS_DATA),
14+
'element' => array('type' => MIDAS_DATA),
15+
'qualifier' => array('type' => MIDAS_DATA),
16+
'description' => array('type' => MIDAS_DATA),
17+
'value' => array('type' => MIDAS_DATA),
18+
'itemrevision_id' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'ItemRevision', 'parent_column' => 'itemrevision_id', 'child_column' => 'itemrevision_id'),
19+
);
20+
$this->initialize(); // required
21+
} // end __construct()
22+
23+
abstract function getMetadata($type,$element,$qualifier);
24+
protected abstract function saveMetadataValue($metadataDao);
25+
abstract function getMetadataValueExists($metadataDao);
26+
27+
/** Add a metadata
28+
* @return MetadataDao */
29+
function addMetadata($type,$element,$qualifier,$description)
30+
{
31+
// Gets the metadata
32+
$metadata = $this->getMetadata($type,$element,$qualifier);
33+
if($metadata)
34+
{
35+
throw new Zend_Exception("Metadata already exists.");
36+
}
37+
38+
$this->loadDaoClass('MetadataDao');
39+
$metadataDao = new MetadataDao();
40+
$metadataDao->setMetadatatype($type);
41+
$metadataDao->setElement($element);
42+
$metadataDao->setQualifier($qualifier);
43+
$metadataDao->setDescription($description);
44+
45+
if(!$this->save($metadataDao))
46+
{
47+
return false;
48+
}
49+
return $metadataDao;
50+
} // end addMetadataValue()
51+
52+
/** Add a metadata to an itemRevision
53+
* @return MetadataDao */
54+
function addMetadataValue($itemRevisionDao,$type,$element,$qualifier,$value)
55+
{
56+
if(!$itemRevisionDao instanceof $itemRevisionDao)
57+
{
58+
throw new Zend_Exception("Error parameters.");
59+
}
60+
61+
// Gets the metadata
62+
$metadataDao = $this->getMetadata($type,$element,$qualifier);
63+
64+
if(!$metadataDao)
65+
{
66+
throw new Zend_Exception("Metadata ".$element.".".$qualifier." doesn't exist.
67+
You should add it before adding a value.");
68+
}
69+
$metadataDao->setItemrevisionId($itemRevisionDao->getKey());
70+
$metadataDao->setValue($value);
71+
if($this->getMetadataValueExists($metadataDao))
72+
{
73+
throw new Zend_Exception("This metadata value already exists for that revision.");
74+
}
75+
$this->saveMetadataValue($metadataDao);
76+
} // end addMetadataValue()
77+
78+
} // end class MetadataModelBase

core/models/dao/MetadataDao.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* \class MetadataDao
4+
* \brief DAO Item (table Metadata)
5+
*/
6+
class MetadataDao extends AppDao
7+
{
8+
public $_model = 'Metadata';
9+
10+
} // end class
11+
?>

core/models/pdo/MetadataModel.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
require_once BASE_PATH.'/core/models/base/MetadataModelBase.php';
3+
4+
/**
5+
* \class MetadataModel
6+
* \brief Pdo Model
7+
*/
8+
class MetadataModel extends MetadataModelBase
9+
{
10+
/** Return an item by its name
11+
* @return MetadataDao*/
12+
function getMetadata($type,$element,$qualifier)
13+
{
14+
$row = $this->database->fetchRow($this->database->select()
15+
->from('metadata')
16+
->where('metadatatype=?', $type)
17+
->where('element=?', $element)
18+
->where('qualifier=?', $qualifier));
19+
return $this->initDao('Metadata', $row);
20+
} // end function getMetadata()
21+
22+
/** Return the table name based on the type of metadata*/
23+
function getTableValueName($metadatatype)
24+
{
25+
switch($metadatatype)
26+
{
27+
case MIDAS_METADATA_GLOBAL: return 'metadatavalue';
28+
case MIDAS_METADATA_DOCUMENT: return 'metadatadocumentvalue';
29+
case MIDAS_METADATA_VIDEO: return 'metadatavideovalue';
30+
case MIDAS_METADATA_IMAGE: return 'metadataimagevalue';
31+
}
32+
return 'metadatavalue';
33+
}
34+
35+
/** Get if a metadata value already exists */
36+
function getMetadataValueExists($metadataDao)
37+
{
38+
if(!$metadataDao instanceof MetadataDao)
39+
{
40+
throw new Zend_Exception("Should be a metadata.");
41+
}
42+
43+
44+
$row = $this->database->fetchRow($this->database->select()
45+
->setIntegrityCheck(false)
46+
->from($this->getTableValueName($metadataDao->getMetadatatype()))
47+
->where('metadata_id=?', $metadataDao->getKey())
48+
->where('itemrevision_id=?', $metadataDao->getItemrevisionId())
49+
->where('value=?', $metadataDao->getValue()));
50+
51+
if(count($row)>0)
52+
{
53+
return true;
54+
}
55+
return false;
56+
} // end getMetadataValueExists()
57+
58+
/** Save a metadata value */
59+
function saveMetadataValue($metadataDao)
60+
{
61+
if(!$metadataDao instanceof MetadataDao)
62+
{
63+
throw new Zend_Exception("Should be a metadata.");
64+
}
65+
66+
$data['metadata_id'] = $metadataDao->getKey();
67+
$data['itemrevision_id'] = $metadataDao->getItemrevisionId();
68+
$data['value'] = $metadataDao->getValue();
69+
$tablename = $this->getTableValueName($metadataDao->getMetadatatype());
70+
$table = new Zend_Db_Table(array('name'=>$tablename,'primary'=>'metadata_id'));
71+
$table->insert($data);
72+
return true;
73+
} // end function saveMetadataValue()
74+
75+
} // end class

0 commit comments

Comments
 (0)