fix ticket #325, 转移元数据入库功能到ISO19115类中

This commit is contained in:
wlx 2012-04-01 06:25:56 +00:00
parent 6236ca494d
commit 5397ca80c4
2 changed files with 152 additions and 190 deletions

View File

@ -1536,195 +1536,7 @@ class Admin_DataController extends Zend_Controller_Action
private function import($xml)
{
$iso=new ISO19115();
$iso->loadXML($xml);
$md=new MetadataTable();
$thumb=new ThumbnailTable();
$xmlt=new XmlTable();
$cgt=new CategoryTable();
$cgct=new CategoryCodeTable();
$keyt=new KeywordTable();
$dst=new DatasetSeriesTable();
$seriestable=new SeriesTable();
$db=$seriestable->getAdapter();
//save metadata
//先删除已有元数据,然后再插入新数据
$sql="delete from metadata where uuid=?";
$this->db->query($sql,array($iso->uuid));
//删除所有未用到的responsible数据
$sql="delete from responsible where id not in (select distinct(resid) from role)";
$this->db->query($sql);
$row=$md->createRow();
$trow=$thumb->createRow();
$row->uuid=$iso->uuid;
$row->title=$iso->resTitle;
$row->title_en=$iso->resAltTitle;
$row->description=$iso->idAbs;
$row->citation=$iso->citation;
$row->suppinfo=$iso->suppinfo;
$row->fileformat=$iso->fileformat;
$row->projection=$iso->projection;
//$row->author=$iso->author;
$row->datatype=$iso->datatype;
$row->filesize=$iso->filesize;
$row->ts_created=$iso->mdDateSt;
$row->timebegin=$iso->timebegin;
$row->timeend=$iso->timeend;
$row->west=$iso->geoBox['w'];
$row->south=$iso->geoBox['s'];
$row->north=$iso->geoBox['n'];
$row->east=$iso->geoBox['e'];
if (!empty($iso->doi)) $row->doi=$iso->doi;
//生成空白统计数据
$sql=$db->quoteInto("select * from mdstat where uuid=?",$iso->uuid);
if (!$db->fetchRow($sql))
{
$sql="insert into mdstat (uuid) values(?)";
$db->query($sql,array($iso->uuid));
}
try {
$id=$row->save();
//处理在线资源
if ($iso->onlineresource) foreach($iso->onlineresource as $resource)
{
$sql="insert into onlineresource (uuid,linkage,protocol,name,description) values(?,?,?,?,?)";
$db->query($sql,array($iso->uuid,$resource['linkage'],$resource['protocol'],$resource['name'],$resource['description']));
}
//处理缩略图
if (!empty($iso->thumbnail)) {
$trow->id=$id;
$trow->data=$iso->thumbnail;
$trow->filetype='jpg';
$trow->save();
} elseif ($iso->graph) {
$trow->id=$id;
//$trow->data=$iso->graph['data'];
$sql="select id as gid from geonetworkmetadata where uuid=?";
$r=$this->db->fetchRow($sql,array($iso->uuid));
if ($r)
{
$trow->data=base64_encode(file_get_contents($this->view->config->geonetwork->url.'srv/cn/resources.get?access=public&id='.$r->gid.'&fname='.urlencode($iso->graph['filename'])));
}
$trow->filetype=$iso->graph['filetype'];
$trow->filedesc=$iso->graph['filedesc'];
$trow->filename=$iso->graph['filename'];
$trow->save();
}
//todo:处理关键词
//1:N relation
foreach($iso->keyword as $keytype=>$keys)
{
foreach($keys as $key)
{
$row=$keyt->createRow();
$row->id=$id;
//统一转换为大写
//不再统一转换
$row->keyword=$key;
$row->keytype=$keytype;
$row->save();
}
}
//todo:处理数据集序列
//M:N relation
if ($iso->datasetSeries) foreach($iso->datasetSeries as $ds)
{
$where=$db->quoteInto('name = ?',$ds['seriesName']);
$trow=$seriestable->fetchRow($where);
if ($trow)
$sid=$trow->id;
else {
$trow=$seriestable->createRow();
$trow->name=$ds['seriesName'];
$sid=$trow->save();
}
$row=$dst->createRow();
$row->id=$id;
$row->sid=$sid;
$row->save();
}
//处理XML入库
$row=$xmlt->createRow();
$row->id=$id;
$row->data=$xml;
$row->save();
//处理数据分类
foreach($iso->tpCat as $cat){
if (is_numeric($cat)) {
$row=$cgt->createRow();
$row->id=$id;
$row->code=(int)$cat;
$row->save();
} else {
//是字符串geonetwork会采用这种模式
//从categorycode表中查找其对应的code
$where=$db->quoteInto('name= ?',trim($cat));
$trow=$cgct->fetchRow($where);
if ($trow) {
$row=$cgt->createRow();
$row->id=$id;
$row->code=$trow->code;
$row->save();
}
//$db=$cgt->getAdapter();
//$sql="insert into category (id,code) values($id,(select code from categorycode where name='$cat'))";
//$db->query($sql);
}
}
//处理联系人信息
//先查询再进行处理
//若用户信息发生变化,则存在问题
//无法保证数据的更新状态
foreach($iso->author as $au)
{
$inds=explode(";",$au['individual']);
foreach($inds as $ind)
{
$sql="select id from responsible where individual=? and organisation=?";
$row=$this->db->fetchRow($sql,array(trim($ind),trim($au['organisation'])));
if (!$row)
{
$sql="insert into responsible (individual,organisation,position,delivery,phone,email,city,country,administrative,postal) values(?,?,?,?,?,?,?,?,?,?)";
$this->db->query($sql,array(trim($ind),trim($au['organisation']),trim($au['position']),trim($au['delivery']),trim($au['phone']),trim($au['email']),trim($au['city']),trim($au['country']),trim($au['administrative']),trim($au['postal'])));
} elseif (count($inds)==1) {
//deal email address
if (($au['email']) && empty($row->email))
{
$sql="update responsible set email=? where id=?";
$this->db->query($sql,array(trim($au['email']),$row->id));
}
}
$sql="select id from responsible where individual=? and organisation=?";
$row=$this->db->fetchRow($sql,array(trim($ind),trim($au['organisation'])));
if ($row->id>0)
{
$sql="insert into role (resid,uuid,role) values(?,?,?)";
$this->db->query($sql,array($row->id,$iso->uuid,trim($au['role'])));
}
}
}
//处理数据限制信息
foreach($iso->limits as $uselimit)
{
$sql="select id from uselimit where uselimit=?";
$row=$this->db->fetchRow($sql,array($uselimit));
if (!$row)
{
$sql="insert into uselimit (uselimit) values (?)";
$this->db->query($sql,array($uselimit));
}
$sql="select id from uselimit where uselimit=?";
$row=$this->db->fetchRow($sql,array($uselimit));
if ($row)
{
$sql="insert into mdlimit (uuid,lid) values(?,?)";
$this->db->query($sql,array($iso->uuid,$row->id));
}
}
$iso->save("../data/import/$iso->uuid.xml");
} catch (Exception $e) {
//数据重复插入,此处忽略所有错误
print $iso->uuid.' has error: '.$e->getMessage().'<br />';
}
$iso->saveDB($this->db,$xml);
}
}

View File

@ -163,8 +163,158 @@ class ISO19115
function loadUUID($uuid)
{
}
function saveDB()
function saveDB($db,$xml)
{
$this->loadXML($xml);
//先删除已有元数据,然后再插入新数据
$sql="delete from metadata where uuid=?";
$db->query($sql,array($this->uuid));
//删除所有未用到的responsible数据
$sql="delete from responsible where id not in (select distinct(resid) from role)";
$db->query($sql);
//if (!empty($this->doi)) $row->doi=$this->doi;
//生成空白统计数据可以转移到数据库端处理todo
try
{
$sql="insert into mdstat (uuid) values(?)";
$db->query($sql,array($this->uuid));
} catch (Exception $e) {
//do nothing.
//说明数据库中已存在该信息
}
//save metadata into database
$sql="insert into metadata (uuid,title,title_en,description,citation,suppinfo,fileformat,projection,datatype,filesize,ts_created,
timebegin,timeend,west,south,north,east,doi) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$sth=$db->prepare($sql);
try {
$sth->execute(array($this->uuid,$this->resTitle,$this->resAltTitle,$this->idAbs,$this->citation,$this->suppinfo,$this->fileformat,
$this->projection,$this->datatype,$this->filesize,$this->mdDateSt,$this->timebegin,$this->timeend,$this->geoBox['w'],
$this->geoBox['s'],$this->geoBox['n'],$this->geoBox['e'],$this->doi));
$id=$db->lastInsertId('metadata','id');
//处理在线资源
if ($this->onlineresource) foreach($this->onlineresource as $resource)
{
$sql="insert into onlineresource (uuid,linkage,protocol,name,description) values(?,?,?,?,?)";
$db->query($sql,array($this->uuid,$resource['linkage'],$resource['protocol'],$resource['name'],$resource['description']));
}
//处理缩略图
if (!empty($this->thumbnail)) {
$sql="insert into thumbnail (id,data,filetype) values(?,?,?)";
$db->query($sql,array($id,$this->thumbnail,'jpg'));
} elseif ($this->graph) {
//$trow->data=$this->graph['data'];
$sql="select id as gid from geonetworkmetadata where uuid=?";
$r=$db->fetchRow($sql,array($this->uuid));
if ($r)
{
$thumb=base64_encode(file_get_contents($this->view->config->geonetwork->url.'srv/cn/resources.get?access=public&id='.$r->gid.'&fname='.urlencode($this->graph['filename'])));
$sql="insert into thumbnail (id,data,filetype,filedesc,filename) values(?,?,?,?,?)";
$db->query($sql,array($id,$thumb,$this->graph['filetype'],$this->graph['filedesc'],$this->graph['filename']));
}
}
//处理关键词
//1:N relation
foreach($this->keyword as $keytype=>$keys)
{
foreach($keys as $key)
{
$sql="insert into keyword (id,keyword,keytype) values(?,?,?)";
$db->query($sql,array($id,$key,$keytype));
}
}
//处理数据集序列
//M:N relation
if ($this->datasetSeries) foreach($this->datasetSeries as $ds)
{
$where=$db->quoteInto('name = ?',$ds['seriesName']);
$sql="select id from series where name=?";
$sth=$db->prepare($sql)
$sth->execute(array($ds['seriesName']));
$trow=$sth->fetch();
$trow=$seriestable->fetchRow($where);
if ($trow)
$sid=$trow['id'];
else {
$sql="insert into series (name) values(?)";
$db->query($sql,array($ds['seriesName']));
$sid=$db->lastInsertId('series','id');
}
$sql="insert into dataserires (id,sid) values(?,?)";
$db->query($sql,array($id,$sid));
}
//处理XML入库
$sql="insert into xml (id,data) values(?,?)";
$db->query($sql,array($id,$xml));
//处理数据分类
foreach($this->tpCat as $cat){
if (is_numeric($cat)) {
$sql="insert into category (id,code) values(?,?)";
$db->query($sql,array($id,(int)$cat));
} else {
//是字符串geonetwork会采用这种模式
//从categorycode表中查找其对应的code
$sql="insert into category (id,code) select ?,code from categorycode where name=?";
$db->query($sql,array($id, trim($cat)));
}
}
//处理联系人信息
//先查询再进行处理
//若用户信息发生变化,则存在问题
//无法保证数据的更新状态
foreach($this->author as $au)
{
$inds=explode(";",$au['individual']);
foreach($inds as $ind)
{
$sql="select id from responsible where individual=? and organisation=?";
$row=$db->fetchRow($sql,array(trim($ind),trim($au['organisation'])));
if (!$row)
{
$sql="insert into responsible (individual,organisation,position,delivery,phone,email,city,country,administrative,postal) values(?,?,?,?,?,?,?,?,?,?)";
$db->query($sql,array(trim($ind),trim($au['organisation']),trim($au['position']),trim($au['delivery']),trim($au['phone']),trim($au['email']),trim($au['city']),trim($au['country']),trim($au['administrative']),trim($au['postal'])));
} elseif (count($inds)==1) {
//deal email address
if (($au['email']) && empty($row->email))
{
$sql="update responsible set email=? where id=?";
$db->query($sql,array(trim($au['email']),$row->id));
}
}
$sql="select id from responsible where individual=? and organisation=?";
$row=$db->fetchRow($sql,array(trim($ind),trim($au['organisation'])));
if ($row->id>0)
{
$sql="insert into role (resid,uuid,role) values(?,?,?)";
$db->query($sql,array($row->id,$this->uuid,trim($au['role'])));
}
}
}
//处理数据限制信息
foreach($this->limits as $uselimit)
{
$sql="select id from uselimit where uselimit=?";
$row=$db->fetchRow($sql,array($uselimit));
if (!$row)
{
$sql="insert into uselimit (uselimit) values (?)";
$db->query($sql,array($uselimit));
}
$sql="select id from uselimit where uselimit=?";
$row=$db->fetchRow($sql,array($uselimit));
if ($row)
{
$sql="insert into mdlimit (uuid,lid) values(?,?)";
$db->query($sql,array($this->uuid,$row->id));
}
}
$this->save("../data/import/$this->uuid.xml");
} catch (Exception $e) {
//数据重复插入,此处忽略所有错误
print $this->uuid.' has error: '.$e->getMessage().'<br />';
}
}
function parse()