contentstorage/caclient/stub/src/caclientproxy.cpp
changeset 61 8e5041d13c84
parent 60 f62f87b200ec
child 66 32469d7d46ff
equal deleted inserted replaced
60:f62f87b200ec 61:8e5041d13c84
    34 
    34 
    35 #include "hswidgetregistryservice.h"
    35 #include "hswidgetregistryservice.h"
    36 
    36 
    37 const char *DATABASE_CONNECTION_NAME = "CaService";
    37 const char *DATABASE_CONNECTION_NAME = "CaService";
    38 const char *DATABASE_TYPE = "QSQLITE";
    38 const char *DATABASE_TYPE = "QSQLITE";
    39 const char *DATABASE_NAME = "castoragedb";
    39 const char *DATABASE_NAME = "castorage.db";
    40 
    40 
    41 static QSqlDatabase dbConnection()
    41 static QSqlDatabase dbConnection()
    42 {
    42 {
    43     return QSqlDatabase::database(DATABASE_CONNECTION_NAME, false);
    43     return QSqlDatabase::database(DATABASE_CONNECTION_NAME, false);
    44 }
    44 }
   496     bool success(true);
   496     bool success(true);
   497     foreach(int i, entryIdList) {
   497     foreach(int i, entryIdList) {
   498         QSqlQuery query(db);
   498         QSqlQuery query(db);
   499         query.prepare(
   499         query.prepare(
   500             "SELECT ENTRY_ID, EN_TEXT, EN_DESCRIPTION, EN_TYPE_NAME, EN_FLAGS, EN_ROLE, EN_UID,  \
   500             "SELECT ENTRY_ID, EN_TEXT, EN_DESCRIPTION, EN_TYPE_NAME, EN_FLAGS, EN_ROLE, EN_UID,  \
   501                   ICON_ID, IC_BITMAP_ID, IC_MASK_ID, IC_SKINMAJOR_ID, IC_SKINMINOR_ID, IC_FILENAME \
   501                   ICON_ID, IC_FILENAME, IC_SKIN_ID, IC_APP_ID \
   502                   FROM CA_ENTRY LEFT JOIN CA_ICON ON EN_ICON_ID = ICON_ID WHERE ENTRY_ID = ?");
   502                   FROM CA_ENTRY LEFT JOIN CA_ICON ON EN_ICON_ID = ICON_ID WHERE ENTRY_ID = ?");
   503         query.addBindValue(i);
   503         query.addBindValue(i);
   504 
   504 
   505         success = query.exec();
   505         success = query.exec();
   506         if (success && query.next()) {
   506         if (success && query.next()) {
   520                     query.record().indexOf("EN_FLAGS")).toUInt()));
   520                     query.record().indexOf("EN_FLAGS")).toUInt()));
   521 
   521 
   522             CaIconDescription icon;
   522             CaIconDescription icon;
   523             CaObjectAdapter::setId(icon,
   523             CaObjectAdapter::setId(icon,
   524                                    query.value(query.record().indexOf("ICON_ID")).toInt());
   524                                    query.value(query.record().indexOf("ICON_ID")).toInt());
   525             icon.setBitmapId(query.value(query.record().indexOf(
   525 
   526                                              "IC_BITMAP_ID")).toInt());
       
   527             icon.setMaskId(query.value(query.record().indexOf(
       
   528                                            "IC_MASK_ID")).toInt());
       
   529             icon.setSkinMajorId(query.value(query.record().indexOf(
       
   530                                                 "IC_SKINMAJOR_ID")).toInt());
       
   531             icon.setSkinMinorId(query.value(query.record().indexOf(
       
   532                                                 "IC_SKINMINOR_ID")).toInt());
       
   533             icon.setFilename(query.value(query.record().indexOf(
   526             icon.setFilename(query.value(query.record().indexOf(
   534                                              "IC_FILENAME")).toString());
   527                                              "IC_FILENAME")).toString());
       
   528             icon.setSkinId(query.value(query.record().indexOf(
       
   529                                              "IC_SKIN_ID")).toString());
       
   530             icon.setApplicationId(query.value(query.record().indexOf(
       
   531                                              "IC_APP_ID")).toString());
   535             entry->setIconDescription(icon);
   532             entry->setIconDescription(icon);
   536 
   533 
   537             // attributes
   534             // attributes
   538             // UID as attribute
   535             // UID as attribute
   539             if (query.value(query.record().indexOf("EN_UID")).toString().length()
   536             if (query.value(query.record().indexOf("EN_UID")).toString().length()
   606                     QString().setNum(query.flagsOff()));
   603                     QString().setNum(query.flagsOff()));
   607     if (query.entryRoles() != 0)
   604     if (query.entryRoles() != 0)
   608         whereStatement.append(" AND ").append(QString().setNum(
   605         whereStatement.append(" AND ").append(QString().setNum(
   609                 query.entryRoles())) .append(" | EN_ROLE == ").append(
   606                 query.entryRoles())) .append(" | EN_ROLE == ").append(
   610                     QString().setNum(query.entryRoles()));
   607                     QString().setNum(query.entryRoles()));
   611     //TODO: by uid???
       
   612 
   608 
   613     if (query.entryTypeNames().count()) {
   609     if (query.entryTypeNames().count()) {
   614         whereStatement.append(" AND EN_TYPE_NAME IN (");
   610         whereStatement.append(" AND EN_TYPE_NAME IN (");
   615         for (int i = 0; i < query.entryTypeNames().count(); i++) {
   611         for (int i = 0; i < query.entryTypeNames().count(); i++) {
   616             whereStatement.append("\'" + query.entryTypeNames()[i] + "\'");
   612             whereStatement.append("\'" + query.entryTypeNames()[i] + "\'");
   618                 whereStatement.append(",");
   614                 whereStatement.append(",");
   619         }
   615         }
   620         whereStatement.append(") ");
   616         whereStatement.append(") ");
   621     }
   617     }
   622 
   618 
       
   619     QString whereAttributes;
       
   620     if (query.attributes().count()) {
       
   621         QMap<QString, QString> attributes = query.attributes();
       
   622         QMapIterator<QString, QString> atrIt(attributes);
       
   623         int j = 1;
       
   624         while (atrIt.hasNext()) {
       
   625             atrIt.next();
       
   626 
       
   627             // at1.AT_NAME = 'Attribute_Name_1' AND at1.AT_VALUE = 'Attribute_VALUE_1'
       
   628             whereAttributes.append(" AND ").append(
       
   629             " at").append(QString().setNum(j)).append(".AT_NAME = \'").append(
       
   630             atrIt.key()).append("\' ").append(
       
   631             " AND ").append(
       
   632             " at").append(QString().setNum(j)).append(".AT_VALUE = \'").append(
       
   633             atrIt.value()).append("\' ");
       
   634 
       
   635             j++;
       
   636         }
       
   637         whereStatement.append(whereAttributes);
       
   638 
       
   639     }
       
   640 
       
   641     whereStatement.append(" GROUP BY ENTRY_ID ");
       
   642 
       
   643     QString leftJoins;
       
   644     if (query.attributes().count()) {
       
   645         for (int j=1; j<=query.attributes().count(); j++) {
       
   646             // LEFT JOIN CA_ATTRIBUTE as at1 ON ENTRY_ID = at1.AT_ENTRY_ID
       
   647             leftJoins.append(
       
   648             " LEFT JOIN CA_ATTRIBUTE as at").append(QString().setNum(j)).append(
       
   649             " ON ENTRY_ID = at").append(QString().setNum(j)).append(" .AT_ENTRY_ID ");
       
   650         }
       
   651     }
       
   652 
   623     QSqlQuery sqlquery(db);
   653     QSqlQuery sqlquery(db);
   624     if (query.parentId() == 0) {
   654     if (query.parentId() == 0) {
   625         // sort
   655         // sort
   626         QString queryString("SELECT ENTRY_ID from CA_ENTRY where 1=1 ");
   656         QString queryString("SELECT ENTRY_ID from CA_ENTRY ");
       
   657         queryString.append(leftJoins);
       
   658         queryString.append(" where 1=1 ");
   627         queryString.append(whereStatement);
   659         queryString.append(whereStatement);
       
   660 
   628         modifyQueryForSortOrder(queryString, query, false);
   661         modifyQueryForSortOrder(queryString, query, false);
   629         if (query.count() > 0)
   662         if (query.count() > 0)
   630             queryString.append(" LIMIT ").append(query.count());
   663             queryString.append(" LIMIT ").append(query.count());
   631         qDebug() << "CaServicePrivate::getEntryIds query text: "
   664         qDebug() << "CaServicePrivate::getEntryIds query text: "
   632                  << queryString;
   665                  << queryString;
   637                 sourceIdList << sqlquery.value(sqlquery.record().indexOf(
   670                 sourceIdList << sqlquery.value(sqlquery.record().indexOf(
   638                                                    "ENTRY_ID")).toInt();
   671                                                    "ENTRY_ID")).toInt();
   639             }
   672             }
   640         }
   673         }
   641     } else {
   674     } else {
   642         QString
   675 
   643         queryString(
   676         QString queryString("SELECT ENTRY_ID FROM CA_ENTRY ");
   644             "SELECT ENTRY_ID FROM CA_ENTRY \
   677         QString queryString2(" LEFT JOIN CA_GROUP_ENTRY ON GE_ENTRY_ID = ENTRY_ID WHERE GE_GROUP_ID  = ? ");
   645         LEFT JOIN CA_GROUP_ENTRY ON GE_ENTRY_ID = ENTRY_ID WHERE GE_GROUP_ID  = ? ");
   678         queryString2.append(whereStatement);
   646         queryString.append(whereStatement);
   679         queryString.append(leftJoins);
       
   680         queryString.append(queryString2);
       
   681 
   647         modifyQueryForSortOrder(queryString, query, true);
   682         modifyQueryForSortOrder(queryString, query, true);
   648         if (query.count() > 0)
   683         if (query.count() > 0)
   649             queryString.append(" LIMIT ").append(query.count());
   684             queryString.append(" LIMIT ").append(query.count());
   650         qDebug() << "CaServicePrivate::getEntryIds query text: "
   685         qDebug() << "CaServicePrivate::getEntryIds query text: "
   651                  << queryString;
   686                  << queryString;
   753         error = UnknownErrorCode;
   788         error = UnknownErrorCode;
   754     }
   789     }
   755     return error;
   790     return error;
   756 }
   791 }
   757 
   792 
   758 /*!
   793 //----------------------------------------------------------------------------
   759  //TODO:
   794 //
   760  */
   795 //----------------------------------------------------------------------------
   761 void CaClientProxy::modifyQueryForSortOrder(QString &queryString,
   796 void CaClientProxy::modifyQueryForSortOrder(QString &queryString,
   762         const CaQuery &query, bool parent) const
   797         const CaQuery &query, bool parent) const
   763 {
   798 {
   764     SortAttribute sortAttribute;
   799     SortAttribute sortAttribute;
   765     Qt::SortOrder sortOrder;
   800     Qt::SortOrder sortOrder;
   803             queryString.append(" DESC ");
   838             queryString.append(" DESC ");
   804     }
   839     }
   805 
   840 
   806 }
   841 }
   807 
   842 
   808 /*!
   843 //----------------------------------------------------------------------------
   809  //TODO:
   844 //
   810  */
   845 //----------------------------------------------------------------------------
   811 bool CaClientProxy::setIconInDb(CaEntry *entryClone) const
   846 bool CaClientProxy::setIconInDb(CaEntry *entryClone) const
   812 {
   847 {
   813     //set icon information into db
   848     //set icon information into db
   814     QSqlQuery query(dbConnection());
   849     QSqlQuery query(dbConnection());
   815     query.prepare(
   850     query.prepare(
   816         "SELECT ICON_ID FROM CA_ICON WHERE IC_FILENAME = :IC_FILENAME \
   851         "SELECT ICON_ID FROM CA_ICON WHERE IC_FILENAME = :IC_FILENAME"
   817             AND IC_BITMAP_ID = :IC_BITMAP_ID \
   852         " AND IC_SKIN_ID = :IC_SKIN_ID"
   818             AND IC_MASK_ID = :IC_MASK_ID \
   853         " AND IC_APP_ID = :IC_APP_ID");
   819             AND IC_SKINMAJOR_ID = :IC_SKINMAJOR_ID \
       
   820             AND IC_SKINMINOR_ID = :IC_SKINMINOR_ID");
       
   821     query.bindValue(":IC_FILENAME",
   854     query.bindValue(":IC_FILENAME",
   822                     entryClone->iconDescription().filename());
   855                     entryClone->iconDescription().filename());
   823     query.bindValue(":IC_BITMAP_ID",
   856     query.bindValue(":IC_SKIN_ID",
   824                     entryClone->iconDescription().bitmapId());
   857                     entryClone->iconDescription().filename());
   825     query.bindValue(":IC_MASK_ID", entryClone->iconDescription().maskId());
   858     query.bindValue(":IC_APP_ID",
   826     query.bindValue(":IC_SKINMAJOR_ID",
   859                     entryClone->iconDescription().filename());
   827                     entryClone->iconDescription().skinMajorId());
   860 
   828     query.bindValue(":IC_SKINMINOR_ID",
       
   829                     entryClone->iconDescription().skinMinorId());
       
   830 
   861 
   831     bool success = query.exec();
   862     bool success = query.exec();
   832     if (success && query.next()) {
   863     if (success && query.next()) {
   833         qDebug() << "query.executedQuery() : " << query.executedQuery();
   864         qDebug() << "query.executedQuery() : " << query.executedQuery();
   834         int iconId = query.value(query.record().indexOf("ICON_ID")).toInt();
   865         int iconId = query.value(query.record().indexOf("ICON_ID")).toInt();
   835         qDebug() << "iconId = " << iconId;
   866         qDebug() << "iconId = " << iconId;
   836         CaIconDescription iconDescription = entryClone->iconDescription();
   867         CaIconDescription iconDescription = entryClone->iconDescription();
   837         if (iconId <= 0 && (iconDescription.filename() != ""
   868         if (iconId <= 0
   838                             || iconDescription.bitmapId() != 0 || iconDescription.maskId() != 0
   869             && (!iconDescription.filename().isEmpty()
   839                             || iconDescription.skinMajorId() != 0
   870                 || !iconDescription.skinId().isEmpty()
   840                             || iconDescription.skinMinorId() != 0)) {
   871                 || !iconDescription.applicationId().isEmpty())) {
   841             query.prepare(
   872             query.prepare(
   842                 "INSERT INTO CA_ICON \
   873                 "INSERT INTO CA_ICON"
   843                            (IC_FILENAME,IC_BITMAP_ID,IC_MASK_ID,IC_SKINMAJOR_ID,IC_SKINMINOR_ID) \
   874                 " (IC_FILENAME, IC_SKIN_ID, IC_APP_ID)"
   844                             VALUES ( ? , ? , ? , ? , ? )");
   875                 " VALUES ( ? , ? , ? , ? , ? )");
   845             query.addBindValue(iconDescription.filename());
   876             query.addBindValue(iconDescription.filename());
   846             query.addBindValue(iconDescription.bitmapId());
   877             query.addBindValue(iconDescription.skinId());
   847             query.addBindValue(iconDescription.maskId());
   878             query.addBindValue(iconDescription.applicationId());
   848             query.addBindValue(iconDescription.skinMajorId());
       
   849             query.addBindValue(iconDescription.skinMinorId());
       
   850             success = query.exec();
   879             success = query.exec();
   851             qDebug() << query.executedQuery();
   880             qDebug() << query.executedQuery();
   852             iconId = query.lastInsertId().toInt();
   881             iconId = query.lastInsertId().toInt();
   853         }
   882         }
   854         CaObjectAdapter::setId(iconDescription, iconId);
   883         CaObjectAdapter::setId(iconDescription, iconId);
   855         entryClone->setIconDescription(iconDescription);
   884         entryClone->setIconDescription(iconDescription);
   856     }
   885     }
   857     return success;
   886     return success;
   858 }
   887 }
   859 
   888 
   860 /*!
   889 //----------------------------------------------------------------------------
   861  //TODO:
   890 //
   862  */
   891 //----------------------------------------------------------------------------
   863 bool CaClientProxy::setEntryInDb(CaEntry *entryClone) const
   892 bool CaClientProxy::setEntryInDb(CaEntry *entryClone) const
   864 {
   893 {
   865     QSqlQuery query(dbConnection());
   894     QSqlQuery query(dbConnection());
   866     bool isNewEntry(entryClone->id() <= 0);
   895     bool isNewEntry(entryClone->id() <= 0);
   867     QString
   896     QString
   909         }
   938         }
   910     }
   939     }
   911     return success;
   940     return success;
   912 }
   941 }
   913 
   942 
   914 /*!
   943 //----------------------------------------------------------------------------
   915  //TODO:
   944 //
   916  */
   945 //----------------------------------------------------------------------------
   917 bool CaClientProxy::setAttributesInDb(CaEntry *entryClone) const
   946 bool CaClientProxy::setAttributesInDb(CaEntry *entryClone) const
   918 {
   947 {
   919     bool success = true;
   948     bool success = true;
   920     QSqlQuery query(dbConnection());
   949     QSqlQuery query(dbConnection());
   921     if (entryClone->attributes().count() > 0) {
   950     if (entryClone->attributes().count() > 0) {