BUG 0000 Refactoring for propel-generator/Database class to DatabasePropel class

- fixes in data editing
- fixes in pmtables import
This commit is contained in:
Erik Amaru Ortiz
2011-09-15 11:30:27 -04:00
parent 07d25773d1
commit 3a242fe8a5
13 changed files with 702 additions and 209 deletions

View File

@@ -20,7 +20,7 @@
*/
include_once 'propel/engine/EngineException.php';
include_once 'propel/engine/database/model/Database.php';
include_once 'propel/engine/database/model/DatabasePropel.php';
/**
* A class for holding application data structures.
@@ -158,7 +158,7 @@ class AppData {
*/
public function addDatabase($db)
{
if ($db instanceof Database) {
if ($db instanceof DatabasePropel) {
$db->setAppData($this);
if ($db->getPlatform() === null) {
$db->setPlatform($this->platform);
@@ -167,7 +167,7 @@ class AppData {
return $db;
} else {
// XML attributes array / hash
$d = new Database();
$d = new DatabasePropel();
$d->loadFromXML($db);
return $this->addDatabase($d); // calls self w/ different param type
}

View File

@@ -0,0 +1,463 @@
<?php
/*
* $Id: Database.php 576 2007-02-09 19:08:40Z hans $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://propel.phpdb.org>.
*/
require_once 'propel/engine/database/model/XMLElement.php';
include_once 'propel/engine/database/model/IDMethod.php';
include_once 'propel/engine/database/model/NameGenerator.php';
include_once 'propel/engine/database/model/Table.php';
/**
* A class for holding application data structures.
*
* @author Hans Lellelid <hans@xmpl.org> (Propel)
* @author Leon Messerschmidt <leon@opticode.co.za> (Torque)
* @author John McNally<jmcnally@collab.net> (Torque)
* @author Martin Poeschl<mpoeschl@marmot.at> (Torque)
* @author Daniel Rall<dlr@collab.net> (Torque)
* @author Byron Foster <byron_foster@yahoo.com> (Torque)
* @version $Revision: 576 $
* @package propel.engine.database.model
*/
class DatabasePropel extends XMLElement {
private $platform;
private $tableList = array();
private $curColumn;
private $name;
private $pkg;
private $baseClass;
private $basePeer;
private $defaultIdMethod;
private $defaultPhpType;
private $defaultPhpNamingMethod;
private $defaultTranslateMethod;
private $dbParent;
private $tablesByName = array();
private $tablesByPhpName = array();
private $heavyIndexing;
private $domainMap = array();
/**
* Sets up the Database object based on the attributes that were passed to loadFromXML().
* @see parent::loadFromXML()
*/
protected function setupObject()
{
$this->name = $this->getAttribute("name");
$this->pkg = $this->getAttribute("package");
$this->baseClass = $this->getAttribute("baseClass");
$this->basePeer = $this->getAttribute("basePeer");
$this->defaultPhpType = $this->getAttribute("defaultPhpType");
$this->defaultIdMethod = $this->getAttribute("defaultIdMethod");
$this->defaultPhpNamingMethod = $this->getAttribute("defaultPhpNamingMethod", NameGenerator::CONV_METHOD_UNDERSCORE);
$this->defaultTranslateMethod = $this->getAttribute("defaultTranslateMethod", Validator::TRANSLATE_NONE);
$this->heavyIndexing = $this->booleanValue($this->getAttribute("heavyIndexing"));
}
/**
* Returns the Platform implementation for this database.
*
* @return Platform a Platform implementation
*/
public function getPlatform()
{
return $this->platform;
}
/**
* Sets the Platform implementation for this database.
*
* @param Platform $platform A Platform implementation
*/
public function setPlatform($platform)
{
$this->platform = $platform;
}
/**
* Get the name of the Database
*/
public function getName()
{
return $this->name;
}
/**
* Set the name of the Database
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get the value of package.
* @return value of package.
*/
public function getPackage()
{
return $this->pkg;
}
/**
* Set the value of package.
* @param v Value to assign to package.
*/
public function setPackage($v)
{
$this->pkg = $v;
}
/**
* Get the value of baseClass.
* @return value of baseClass.
*/
public function getBaseClass()
{
return $this->baseClass;
}
/**
* Set the value of baseClass.
* @param v Value to assign to baseClass.
*/
public function setBaseClass($v)
{
$this->baseClass = $v;
}
/**
* Get the value of basePeer.
* @return value of basePeer.
*/
public function getBasePeer()
{
return $this->basePeer;
}
/**
* Set the value of basePeer.
* @param v Value to assign to basePeer.
*/
public function setBasePeer($v)
{
$this->basePeer = $v;
}
/**
* Get the value of defaultIdMethod.
* @return value of defaultIdMethod.
*/
public function getDefaultIdMethod()
{
return $this->defaultIdMethod;
}
/**
* Set the value of defaultIdMethod.
* @param v Value to assign to defaultIdMethod.
*/
public function setDefaultIdMethod($v)
{
$this->defaultIdMethod = $v;
}
/**
* Get the value of defaultPHPNamingMethod which specifies the
* method for converting schema names for table and column to PHP names.
* @return string The default naming conversion used by this database.
*/
public function getDefaultPhpNamingMethod()
{
return $this->defaultPhpNamingMethod;
}
/**
* Set the value of defaultPHPNamingMethod.
* @param string $v The default naming conversion for this database to use.
*/
public function setDefaultPhpNamingMethod($v)
{
$this->defaultPhpNamingMethod = $v;
}
/**
* Get the value of defaultTranslateMethod which specifies the
* method for translate validator error messages.
* @return string The default translate method.
*/
public function getDefaultTranslateMethod()
{
return $this->defaultTranslateMethod;
}
/**
* Set the value of defaultTranslateMethod.
* @param string $v The default translate method to use.
*/
public function setDefaultTranslateMethod($v)
{
$this->defaultTranslateMethod = $v;
}
/**
* Get the value of heavyIndexing.
* @return boolean Value of heavyIndexing.
*/
public function isHeavyIndexing()
{
return $this->heavyIndexing;
}
/**
* Set the value of heavyIndexing.
* @param boolean $v Value to assign to heavyIndexing.
*/
public function setHeavyIndexing($v)
{
$this->heavyIndexing = (boolean) $v;
}
/**
* Return an array of all tables
*/
public function getTables()
{
return $this->tableList;
}
/**
* Return the table with the specified name.
* @param string $name The name of the table (e.g. 'my_table')
* @return Table a Table object or null if it doesn't exist
*/
public function getTable($name)
{
if (isset($this->tablesByName[$name])) {
return $this->tablesByName[$name];
}
return null; // just to be explicit
}
/**
* Return the table with the specified phpName.
* @param string $phpName the PHP Name of the table (e.g. 'MyTable')
* @return Table a Table object or null if it doesn't exist
*/
public function getTableByPhpName($phpName)
{
if (isset($this->tablesByPhpName[$phpName])) {
return $this->tablesByPhpName[$phpName];
}
return null; // just to be explicit
}
/**
* An utility method to add a new table from an xml attribute.
*/
public function addTable($data)
{
if ($data instanceof Table) {
$tbl = $data; // alias
$tbl->setDatabase($this);
if (isset($this->tablesByName[$tbl->getName()])) {
throw new EngineException("Duplicate table declared: " . $tbl->getName());
}
$this->tableList[] = $tbl;
$this->tablesByName[ $tbl->getName() ] = $tbl;
$this->tablesByPhpName[ $tbl->getPhpName() ] = $tbl;
if ($tbl->getPackage() === null) {
$tbl->setPackage($this->getPackage());
}
return $tbl;
} else {
$tbl = new Table();
$tbl->setDatabase($this);
$tbl->loadFromXML($data);
return $this->addTable($tbl); // call self w/ different param
}
}
/**
* Set the parent of the database
*/
public function setAppData(AppData $parent)
{
$this->dbParent = $parent;
}
/**
* Get the parent of the table
*/
public function getAppData()
{
return $this->dbParent;
}
/**
* Adds Domain object from <domain> tag.
* @param mixed XML attributes (array) or Domain object.
*/
public function addDomain($data) {
if ($data instanceof Domain) {
$domain = $data; // alias
$domain->setDatabase($this);
$this->domainMap[ $domain->getName() ] = $domain;
return $domain;
} else {
$domain = new Table();
$domain->setDatabase($this);
$domain->loadFromXML($data);
return $this->addDomain($domain); // call self w/ different param
}
}
/**
* Get already configured Domain object by name.
* @return Domain
*/
public function getDomain($domainName) {
if (!isset($this->domainMap[$domainName])) {
return null;
}
return $this->domainMap[$domainName];
}
public function doFinalInitialization()
{
$tables = $this->getTables();
for($i=0,$size=count($tables); $i < $size; $i++) {
$currTable = $tables[$i];
// check schema integrity
// if idMethod="autoincrement", make sure a column is
// specified as autoIncrement="true"
// FIXME: Handle idMethod="native" via DB adapter.
/*
--- REMOVING THIS BECAUSE IT'S ANNOYING
if ($currTable->getIdMethod() == IDMethod::NATIVE ) {
$columns = $currTable->getColumns();
$foundOne = false;
for ($j=0, $cLen=count($columns); $j < $cLen && !$foundOne; $j++) {
$foundOne = $columns[$j]->isAutoIncrement();
}
if (!$foundOne) {
$errorMessage = "Table '" . $currTable->getName()
. "' is set to use native id generation, but it does not "
. "have a column which declared as the one to "
. "auto increment (i.e. autoIncrement=\"true\")";
throw new BuildException($errorMessage);
}
}
*/
$currTable->doFinalInitialization();
// setup reverse fk relations
$fks = $currTable->getForeignKeys();
for ($j=0, $fksLen=count($fks); $j < $fksLen; $j++) {
$currFK = $fks[$j];
$foreignTable = $this->getTable($currFK->getForeignTableName());
if ($foreignTable === null) {
throw new BuildException("ERROR!! Attempt to set foreign"
. " key to nonexistent table, "
. $currFK->getForeignTableName() . "!");
}
$referrers = $foreignTable->getReferrers();
if ($referrers === null || ! in_array($currFK,$referrers) ) {
$foreignTable->addReferrer($currFK);
}
// local column references
$localColumnNames = $currFK->getLocalColumns();
for($k=0,$lcnLen=count($localColumnNames); $k < $lcnLen; $k++) {
$local = $currTable->getColumn($localColumnNames[$k]);
// give notice of a schema inconsistency.
// note we do not prevent the npe as there is nothing
// that we can do, if it is to occur.
if ($local === null) {
throw new BuildException("ERROR!! Attempt to define foreign"
. " key with nonexistent column, "
. $localColumnNames[$k] . ", in table, "
. $currTable->getName() . "!");
}
//check for foreign pk's
if ($local->isPrimaryKey()) {
$currTable->setContainsForeignPK(true);
}
} // for each local col name
// foreign column references
$foreignColumnNames = $currFK->getForeignColumns();
for($k=0,$fcnLen=count($localColumnNames); $k < $fcnLen; $k++) {
$foreign = $foreignTable->getColumn($foreignColumnNames[$k]);
// if the foreign column does not exist, we may have an
// external reference or a misspelling
if ($foreign === null) {
throw new BuildException("ERROR!! Attempt to set foreign"
. " key to nonexistent column, "
. $foreignColumnNames[$k] . ", in table, "
. $foreignTable->getName() . "!");
} else {
$foreign->addReferrer($currFK);
}
} // for each foreign col ref
}
}
}
/**
* Creats a string representation of this Database.
* The representation is given in xml format.
*/
public function toString()
{
$result = "<database name=\"" . $this->getName() . '"'
. " package=\"" . $this->getPackage() . '"'
. " defaultIdMethod=\"" . $this->getDefaultIdMethod()
. '"'
. " baseClass=\"" . $this->getBaseClass() . '"'
. " basePeer=\"" . $this->getBasePeer() . '"'
. ">\n";
for ($i=0, $size=count($this->tableList); $i < $size; $i++) {
$result .= $this->tableList[$i]->toString();
}
$result .= "</database>";
return $result;
}
}

View File

@@ -95,7 +95,7 @@ class Domain extends XMLElement {
/**
* Sets the owning database object (if this domain is being setup via XML).
*/
public function setDatabase(Database $database) {
public function setDatabase(DatabasePropel $database) {
$this->database = $database;
}

View File

@@ -53,7 +53,7 @@ class XmlToData extends AbstractHandler {
*
* @param Database $database
*/
public function __construct(Database $database, $encoding = 'iso-8859-1')
public function __construct(DatabasePropel $database, $encoding = 'iso-8859-1')
{
$this->database = $database;
$this->encoding = $encoding;

View File

@@ -23,7 +23,7 @@
//include_once 'phing/tasks/ext/CapsuleTask.php';
require_once 'phing/TaskPhing.php';
include_once 'propel/engine/database/model/AppData.php';
include_once 'propel/engine/database/model/Database.php';
include_once 'propel/engine/database/model/DatabasePropel.php';
include_once 'propel/engine/database/transform/XmlToAppData.php';
/**

View File

@@ -349,7 +349,7 @@ class PropelDataDumpTask extends AbstractPropelDataModelTask {
* @param Database $database
* @return DOMDocument
*/
private function createXMLDoc(Database $database) {
private function createXMLDoc(DatabasePropel $database) {
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true; // pretty printing

View File

@@ -22,7 +22,7 @@
require_once 'propel/phing/AbstractPropelDataModelTask.php';
include_once 'propel/engine/database/model/AppData.php';
include_once 'propel/engine/database/model/Database.php';
include_once 'propel/engine/database/model/DatabasePropel.php';
include_once 'propel/engine/database/transform/XmlToAppData.php';
/**

View File

@@ -21,7 +21,7 @@
*/
include_once 'propel/engine/database/model/AppData.php';
include_once 'propel/engine/database/model/Database.php';
include_once 'propel/engine/database/model/DatabasePropel.php';
include_once 'propel/engine/database/transform/XmlToAppData.php';
include_once 'propel/engine/database/transform/XmlToData.php';

View File

@@ -280,7 +280,7 @@ class PropelOldSQLTask extends AbstractPropelDataModelTask {
//'heavyIndexing' => $db->getHeavyIndexing(),
);
$clone = new Database();
$clone = new DatabasePropel();
$clone->loadFromXML($attributes);
return $clone;
}

View File

@@ -251,7 +251,7 @@ class PropelSQLTask extends AbstractPropelDataModelTask {
//'heavyIndexing' => $db->getHeavyIndexing(),
);
$clone = new Database();
$clone = new DatabasePropel();
$clone->loadFromXML($attributes);
return $clone;
}

View File

@@ -183,6 +183,7 @@ class pmTablesProxy extends HttpProxyController
require_once 'classes/model/Fields.php';
try {
ob_start();
$data = (array) $httpData;
$data['PRO_UID'] = trim($data['PRO_UID']);
$data['columns'] = G::json_decode(stripslashes($httpData->columns)); //decofing data columns
@@ -198,16 +199,17 @@ class pmTablesProxy extends HttpProxyController
'INDEX', 'INSERT', 'OPEN', 'REVOKE', 'ROLLBACK', 'SELECT', 'SYNONYM', 'TABLE', 'UPDATE', 'VIEW',
'APP_UID', 'ROW', 'PMTABLE'
);
//$reservedWords = array_merge($reservedWords, array_change_key_case($reservedWords, CASE_LOWER));
// verify if exists.
if ($data['REP_TAB_UID'] == '') { //new report table
if ($data['REP_TAB_UID'] == '' || (isset($httpData->forceUid) && $httpData->forceUid)) { //new report table
if ($isReportTable) { //setting default columns
$defaultColumns = $this->_getReportTableDefaultColumns($data['REP_TAB_TYPE']);
$columns = array_merge($defaultColumns, $columns);
}
/** validations **/
if(is_array($oAdditionalTables->loadByName($data['REP_TAB_NAME']))) {
if(in_array($data['REP_TAB_NAME'], $reservedWords) || (isset($httpData->forceUid) && $httpData->forceUid )) {
throw new Exception('The table "' . $data['REP_TAB_NAME'] . '" already exits.');
}
@@ -225,13 +227,13 @@ class pmTablesProxy extends HttpProxyController
}
G::loadClass('pmTable');
ob_start();
$pmTable = new pmTable($data['REP_TAB_NAME']);
$pmTable->setDataSource($data['REP_TAB_CONNECTION']);
$pmTable->setColumns($columns);
$pmTable->setAlterTable($alterTable);
$pmTable->build();
unset($pmTable);
$buildResult = ob_get_contents();
ob_end_clean();
@@ -641,10 +643,15 @@ class pmTablesProxy extends HttpProxyController
{
require_once 'classes/model/AdditionalTables.php';
try {
$errors = '';
ob_start();
$overWrite = isset($_POST['form']['OVERWRITE'])? true: false;
//save the file
if ($_FILES['form']['error']['FILENAME'] == 0) {
if ($_FILES['form']['error']['FILENAME'] !== 0) {
throw new Exception('Problem while uploading file');
}
$oAdditionalTables = new AdditionalTables();
$tableNameMap = array();
$processQueue = array();
@@ -657,7 +664,7 @@ class pmTablesProxy extends HttpProxyController
$fileContent = file_get_contents($PUBLIC_ROOT_PATH.$filename);
if(strpos($fileContent, '-----== ProcessMaker Open Source Private Tables ==-----') === false) {
throw new Exception('Invalid File');
throw new Exception('Invalid File, Import abort');
}
$fp = fopen($PUBLIC_ROOT_PATH.$filename, "rb");
@@ -740,17 +747,21 @@ class pmTablesProxy extends HttpProxyController
}
$result = $this->save($tableData, $alterTable);
if (!$result->success) {
$errors .= $errors . "\n\n";
}
break;
case '@DATA':
$fstName = intval(fread($fp, 9));
$tableName = fread($fp, $fstName);
$fsData = intval(fread($fp, 9));
//var_dump($fsData);
if ($fsData > 0) {
$data = fread($fp, $fsData);
$contentData = unserialize($data);
$tableName = $tableNameMap[$tableName];
$oAdditionalTables = new AdditionalTables();
@@ -772,14 +783,11 @@ class pmTablesProxy extends HttpProxyController
}
}
$this->success = true;
$this->message = 'File Imported "'.$filename.'" Successfully';
////////////
G::loadClass('pmTable');
foreach ($processQueue as $dbsUid => $tableData) {
ob_start();
$pmTable = new pmTable();
$pmTable->buildModelFor($dbsUid);
$buildResult = ob_get_contents();
@@ -797,10 +805,22 @@ class pmTablesProxy extends HttpProxyController
}
}
if ($errors == '') {
$msg = 'File Imported "'.$filename.'" Successfully';
$result->type = 0;
}
else {
$msg = 'File Imported "'.$filename.'" but with errors' . "\n\n" . $errors;
$result->type = 1;
}
$result->success = true;
$result->message = $msg;
}
catch(Exception $e) {
$buildResult = ob_get_contents();
$result->type = 2;
$result->buildResult = ob_get_contents();
ob_end_clean();
$result->success = false;
@@ -816,6 +836,8 @@ class pmTablesProxy extends HttpProxyController
$result->trace = $e->getTraceAsString();
}
return $result;
}
/**
@@ -1010,9 +1032,12 @@ class pmTablesProxy extends HttpProxyController
*/
function _dataUpdate($row)
{
$keys = explode('-', $row->__index__);
$keys = G::decrypt($row->__index__, 'pmtable');
$keys = explode('-', $keys);
unset($row->__index__);
$params = array();
foreach ($keys as $key) {
$params[] = is_numeric($key) ? $key : "'$key'";
}

View File

@@ -260,6 +260,7 @@ Ext.onReady(function(){
forceFit:true
},
store: store,
loadMask: true,
cm: cmodel,
sm: smodel,
tbar:[ newButton,
@@ -292,14 +293,14 @@ Ext.onReady(function(){
infoGrid.on('contextmenu', function(evt){evt.preventDefault();}, this);
infoGrid.addListener('rowcontextmenu',onMessageContextMenu, this);
infoGrid.store.load();
viewport = new Ext.Viewport({
layout: 'fit',
autoScroll: false,
items: [infoGrid]
});
infoGrid.store.load();
});
//Funtion Handles Context Menu Opening

View File

@@ -114,7 +114,6 @@ Ext.onReady(function(){
contextMenuItems.push(exportButton);
if (_PLUGIN_SIMPLEREPORTS !== false) {
externalOption = new Ext.Action({
text:'',
handler: function() {
@@ -182,6 +181,7 @@ Ext.onReady(function(){
comboPageSize.setValue(pageSize);
store = new Ext.data.GroupingStore( {
autoLoad: false,
proxy : new Ext.data.HttpProxy({
url: 'pmTablesProxy/getList' + (PRO_UID? '?pro_uid='+PRO_UID: '')
}),
@@ -293,14 +293,25 @@ Ext.onReady(function(){
forceFit:true
},
store: store,
loadMask: true,
cm: cmodel,
sm: smodel,
tbar:[newButton, editButton, deleteButton,'-', dataButton,'-' , importButton, exportButton,{xtype: 'tbfill'},searchText,clearTextButton,searchButton],
tbar: [
newButton,
editButton,
deleteButton,'-',
dataButton,'-' ,
importButton,
exportButton,
'->',
searchText,
clearTextButton,
searchButton],
bbar: bbarpaging,
listeners: {
rowdblclick: EditPMTable,
render: function(){
this.loadMask = new Ext.LoadMask(this.body, {msg:_('ID_LOADING_GRID')});
this.loadMask = new Ext.LoadMask(this.body, {msg:'loading'});
}
},
view: new Ext.grid.GroupingView({
@@ -332,16 +343,13 @@ Ext.onReady(function(){
infoGrid.on('contextmenu', function(evt){evt.preventDefault();}, this);
infoGrid.addListener('rowcontextmenu',onMessageContextMenu, this);
infoGrid.store.load();
viewport = new Ext.Viewport({
layout: 'fit',
autoScroll: false,
items: [
infoGrid
]
items: [infoGrid]
});
infoGrid.store.load();
});
//Funtion Handles Context Menu Opening
@@ -482,6 +490,9 @@ ImportPMTable = function(){
url: 'pmTablesProxy/import',
waitMsg: 'Uploading file...',
success: function(o, resp){
console.log(o);
console.log(resp.response.responseText);
w.close();
infoGrid.store.reload();
@@ -511,14 +522,7 @@ ImportPMTable = function(){
}
}]
})
]/*,
listeners:{
show:function() {
this.loadMask = new Ext.LoadMask(this.body, {
msg:'Loading. Please wait...'
});
}
}*/
]
});
w.show();
}