Skip to content

Commit

Permalink
Importar XML listo
Browse files Browse the repository at this point in the history
  • Loading branch information
Dark1024 committed Sep 22, 2013
1 parent 0fc107c commit 3832c8c
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 1 deletion.
1 change: 1 addition & 0 deletions Proyecto_OA.pro
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#-------------------------------------------------

QT += core gui
QT += printsupport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

Expand Down
5 changes: 5 additions & 0 deletions adtfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,8 @@ bool ADTFile::isOk()
{
return this->FS.good();
}

string ADTFile::getFileName()
{
return this->Nombre;
}
2 changes: 1 addition & 1 deletion adtfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ADTFile
virtual bool isBOF() const;
virtual bool flush();
virtual bool isOk();

virtual string getFileName();
protected:
string Nombre;
fstream FS;
Expand Down
7 changes: 7 additions & 0 deletions adtfilerecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,10 @@ bool ADTFileRecord::deleteRecord(PrimaryIndex* index)
this->AvailList.push_back(offset);
return true;
}

void ADTFileRecord::cleanMap()
{
while(this->indexes.size() != 0){
this->indexes.clear();
}
}
1 change: 1 addition & 0 deletions adtfilerecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ADTFileRecord:public ADTFile
PrimaryIndex* searchRecord(string);
bool deleteRecord(PrimaryIndex*);
void insertIndex(string, PrimaryIndex*);
void cleanMap();

private:
int recordLength;
Expand Down
224 changes: 224 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ void MainWindow::on_CloseFile_triggered()
while(this->fileRecord.fieldsSize() != 0){
this->fileRecord.removeField(0);
}
this->fileRecord.cleanMap();
QMessageBox::information(this,"Correcto","El archivo se cerro correctamente");
}else{
QMessageBox::critical(this,"Error","Hubo un error al momento de cerrar el archivo");
Expand All @@ -180,6 +181,7 @@ void MainWindow::on_Exit_triggered()
while(this->fileRecord.fieldsSize() != 0){
this->fileRecord.removeField(0);
}
this->fileRecord.cleanMap();
this->close();
}else{
QMessageBox::critical(this,"Error","Hubo un error al momento de guadar el archivo");
Expand Down Expand Up @@ -476,9 +478,231 @@ void MainWindow::on_deleteRecord_triggered()
return;
}else{
if(this->fileRecord.deleteRecord(index)){
vector<PrimaryIndex*> indexes = this->fileRecord.getIndexes();

stringstream ss;
for(int i = 0; i < indexes.size(); i++){
ss<<indexes.at(i)->toString();
if(i != indexes.size() -1){
ss<<'/';
}
}
this->indicesFile.seekp(0,ios_base::beg);
this->indicesFile.write(ss.str().c_str(),ss.str().length());
this->indicesFile.flush();

QMessageBox::information(this,"Correcto","Se ha podido eliminar el registro correctamente");
}else{
QMessageBox::critical(this,"Error","No se pudo eliminar el registro, no se encuentra la llave");
}
}
}

void MainWindow::on_reindex_triggered()
{
this->indicesFile.flush();
this->indicesFile.close();
this->fileRecord.cleanMap();

if(this->indicesFile.open(this->indicesFile.getFileName(),ios_base::in | ios_base::out)){
this->indicesFile.seekg(0,ios_base::end);
streamoff indexLength = this->indicesFile.tellg();

char* indexes = new char[indexLength+1];

this->indicesFile.seekg(0,ios_base::beg);
this->indicesFile.read(indexes,indexLength);

indexes[indexLength] = '\0';
string str (indexes);
QString qstr = QString::fromStdString(str);

QStringList list = qstr.split("/");

for(int i = 0; i < list.size(); i++){
QStringList list2 = list.at(i).split(",");

string key = list2.at(0).toStdString();
streamoff offset = atoll(list2.at(1).toStdString().c_str());

PrimaryIndex* newIndex = new PrimaryIndex(key,offset);
this->fileRecord.insertIndex(key,newIndex);
}
QMessageBox::information(this,"Satisfactorio","Se ha reindexado correctamente");
}else{
QMessageBox::critical(this,"Error","Hubo un error al momento de cargar al archivo de indices");
return;
}
}

void MainWindow::on_doSimpleIndexes_triggered()
{
this->indicesFile.flush();
this->indicesFile.close();
this->fileRecord.cleanMap();

if(this->indicesFile.open(this->indicesFile.getFileName(),ios_base::in | ios_base::out)){
this->indicesFile.seekg(0,ios_base::end);
streamoff indexLength = this->indicesFile.tellg();

char* indexes = new char[indexLength+1];

this->indicesFile.seekg(0,ios_base::beg);
this->indicesFile.read(indexes,indexLength);

indexes[indexLength] = '\0';
string str (indexes);
QString qstr = QString::fromStdString(str);

QStringList list = qstr.split("/");

for(int i = 0; i < list.size(); i++){
QStringList list2 = list.at(i).split(",");

string key = list2.at(0).toStdString();
streamoff offset = atoll(list2.at(1).toStdString().c_str());

PrimaryIndex* newIndex = new PrimaryIndex(key,offset);
this->fileRecord.insertIndex(key,newIndex);
}
QMessageBox::information(this,"Satisfactorio","Se han creado los indices simples");
}else{
QMessageBox::critical(this,"Error","Hubo un error al momento de cargar al archivo de indices");
return;
}
}

void MainWindow::on_PrintFile_triggered()
{
if(this->fileRecord.fieldsSize() == 0 || this->fileRecord.getIndexes().size() == 0){
QMessageBox::warning(this,"Error","El archivo no contiene campos o registros");
return;
}

QString path = QFileDialog::getExistingDirectory(this,"Imprimir archivo en PDF","");
if(!path.isEmpty()){
path += "/Registros.pdf";

QString html = "";


html += "<table border=\"1\">";

vector<Field*> fields = this->fileRecord.getFields();

html += "<tr>";
for(int i = 0; i < fields.size(); i++){
Field* currentField = fields.at(i);

html += "<th>" + QString::fromStdString(currentField->getName()) + "</th>";
}
html += "</tr>";

vector<PrimaryIndex*> indexes = this->fileRecord.getIndexes();


for(int i = 0; i < indexes.size(); i++){
PrimaryIndex* currentIndex = indexes.at(i);
Record* currentRecord = this->fileRecord.getRecord(currentIndex);

vector<string> currentVector = currentRecord->getRecord();

html += "<tr>";
for(int j = 0; j < currentVector.size(); j++){
html += "<td>" + QString::fromStdString(currentVector[j])+ "</td>";
}
html += "</tr>";
}

html +="</table>";

QTextDocument document;
document.setHtml(html);
QPrinter printer;
printer.setOutputFileName(path);
printer.setOutputFormat(QPrinter::PdfFormat);
document.print(&printer);
printer.newPage();

QMessageBox::information(this,"Satisfactorio","Los registros se han impreso en el archivo Registro.pdf");
}
}

void MainWindow::on_exportXML_triggered()
{
if(!this->fileRecord.isOpen()){
QMessageBox::critical(this,"Error","No tiene un archivo abierto para exportar sus campos y registros");
return;
}

if(this->fileRecord.fieldsSize() == 0 || this->fileRecord.getIndexes().size() == 0){
QMessageBox::warning(this,"Error","El archivo no tiene campos o registros");
return;
}

QString path = QFileDialog::getExistingDirectory(this,"Exportar en archivo XML","");


if(!path.isEmpty()){
path += "/Registros.xml";

QFile file (path);

if(!file.open(QIODevice::WriteOnly)){
QMessageBox::warning(this,"Error","No se puede exportar a xml, error al abrir archivo destino");
return;
}else{
QXmlStreamWriter xmlw;
xmlw.setDevice(&file);
xmlw.writeStartDocument();

xmlw.writeStartElement("fileRecord");

vector<PrimaryIndex*> indexes = this->fileRecord.getIndexes();

for(int i = 0; i < indexes.size(); i++){
PrimaryIndex* currentIndex = indexes.at(i);

Record* currentRecord = this->fileRecord.getRecord(currentIndex);
vector<Field*> fields = currentRecord->getFields();
vector<string> record = currentRecord->getRecord();

xmlw.writeStartElement("record");

for(int j = 0; j < fields.size(); j++){
Field* currentField = fields.at(j);

xmlw.writeStartElement(QString::fromStdString(currentField->getName()));
if(currentField->isKey()){
xmlw.writeAttribute("key","true");
}else{
xmlw.writeAttribute("key","false");
}

if(currentField->getType() == 'E'){
xmlw.writeAttribute("type","integer");
}else if(currentField->getType() == 'R'){
xmlw.writeAttribute("type","real");
}else{
xmlw.writeAttribute("type","string");
}

xmlw.writeAttribute("length",QString::number(currentField->getLength()));

if(currentField->getType() == 'R'){
xmlw.writeAttribute("decimalPlaces",QString::number(currentField->getDecimalPlaces()));
}

xmlw.writeCharacters(QString::fromStdString(record[j]));
xmlw.writeEndElement();
}
xmlw.writeEndElement();
}

xmlw.writeEndElement();
xmlw.writeEndDocument();

QMessageBox::information(this,"Satisfactorio","Se ha creado correctamente el archivo XML");
}
}
}
14 changes: 14 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
#include <QLineEdit>
#include <QStandardItemModel>
#include <QMap>
#include <QTextDocument>
#include <QtPrintSupport/qprinter.h>
#include <QtPrintSupport/qprintdialog.h>
#include <QtPrintSupport/qtprintsupportglobal.h>
#include <QXmlStreamWriter>

#include <string>
#include <vector>
#include <iostream>
Expand Down Expand Up @@ -63,6 +69,14 @@ private slots:

void on_deleteRecord_triggered();

void on_reindex_triggered();

void on_doSimpleIndexes_triggered();

void on_PrintFile_triggered();

void on_exportXML_triggered();

private:
ADTFile indicesFile;
ADTFileRecord fileRecord;
Expand Down
57 changes: 57 additions & 0 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,31 @@
<addaction name="separator"/>
<addaction name="crossTables"/>
</widget>
<widget class="QMenu" name="menuIndices">
<property name="title">
<string>Indices</string>
</property>
<addaction name="doSimpleIndexes"/>
<addaction name="separator"/>
<addaction name="doBTree"/>
<addaction name="separator"/>
<addaction name="reindex"/>
</widget>
<widget class="QMenu" name="menuUtilidades">
<property name="title">
<string>Utilidades</string>
</property>
<addaction name="exportXML"/>
<addaction name="importXML"/>
<addaction name="separator"/>
<addaction name="exportJSon"/>
<addaction name="importJSon"/>
</widget>
<addaction name="menuArchivo"/>
<addaction name="menuCampos"/>
<addaction name="menuRegistros"/>
<addaction name="menuIndices"/>
<addaction name="menuUtilidades"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
Expand Down Expand Up @@ -156,6 +178,41 @@
<string>Cruzar Tablas</string>
</property>
</action>
<action name="doSimpleIndexes">
<property name="text">
<string>Crear Indices Simples</string>
</property>
</action>
<action name="doBTree">
<property name="text">
<string>Crear Indices Arbol B</string>
</property>
</action>
<action name="reindex">
<property name="text">
<string>Reindexar</string>
</property>
</action>
<action name="exportXML">
<property name="text">
<string>Exportar XML</string>
</property>
</action>
<action name="importXML">
<property name="text">
<string>Importar XML</string>
</property>
</action>
<action name="exportJSon">
<property name="text">
<string>Exportar JSon</string>
</property>
</action>
<action name="importJSon">
<property name="text">
<string>Importar JSon</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
Expand Down

0 comments on commit 3832c8c

Please sign in to comment.