contentstorage/caclient/src/caservice.cpp
changeset 66 32469d7d46ff
parent 61 8e5041d13c84
child 73 4bc7b118b3df
equal deleted inserted replaced
61:8e5041d13c84 66:32469d7d46ff
   101  ...
   101  ...
   102  CaEntry * entryFromDB = service->getEntry( newItem->id() );
   102  CaEntry * entryFromDB = service->getEntry( newItem->id() );
   103 
   103 
   104  \endcode
   104  \endcode
   105  */
   105  */
   106 CaEntry *CaService::getEntry(int entryId) const
   106 QSharedPointer<CaEntry> CaService::getEntry(int entryId) const
   107 {
   107 {
   108     QList<CaEntry *> entries = getEntries(QList<int> () << entryId);
   108     QList< QSharedPointer<CaEntry> > entries = getEntries(QList<int> () << entryId);
   109     if (entries.count()) {
   109     if (entries.count()) {
   110         // there should be exactly one entry with specified ID if present
   110         // there should be exactly one entry with specified ID if present
   111         Q_ASSERT(entries.count() == 1);
   111         Q_ASSERT(entries.count() == 1);
   112         return entries[0];
   112         return entries[0];
   113     } else {
   113     } else {
   114         return NULL;
   114         return QSharedPointer<CaEntry>();
   115     }
   115     }
   116 }
   116 }
   117 
   117 
   118 /*!
   118 /*!
   119  Search for entries.
   119  Search for entries.
   133  textEntry << entry->text();
   133  textEntry << entry->text();
   134  }
   134  }
   135 
   135 
   136  \endcode
   136  \endcode
   137  */
   137  */
   138 QList<CaEntry *> CaService::getEntries(const QList<int> &entryIdList) const
   138 QList< QSharedPointer<CaEntry> > CaService::getEntries(const QList<int> &entryIdList) const
   139 {
   139 {
   140     return m_d->getEntries(entryIdList);
   140     return m_d->getEntries(entryIdList);
   141 }
   141 }
   142 
   142 
   143 /*!
   143 /*!
   160  typeNames << entry->entryTypeName();
   160  typeNames << entry->entryTypeName();
   161  }
   161  }
   162 
   162 
   163  \endcode
   163  \endcode
   164  */
   164  */
   165 QList<CaEntry *> CaService::getEntries(const CaQuery &query) const
   165 QList< QSharedPointer<CaEntry> > CaService::getEntries(const CaQuery &query) const
   166 {
   166 {
   167     return m_d->getEntries(query);
   167     return m_d->getEntries(query);
   168 }
   168 }
   169 
   169 
   170 /*!
   170 /*!
   214  CaEntry * newGroup = service->createEntry( group );
   214  CaEntry * newGroup = service->createEntry( group );
   215  CaEntry * newItem = service->createEntry( item );
   215  CaEntry * newItem = service->createEntry( item );
   216  ...
   216  ...
   217  \endcode
   217  \endcode
   218  */
   218  */
   219 CaEntry *CaService::createEntry(const CaEntry &entry) const
   219 QSharedPointer<CaEntry> CaService::createEntry(const CaEntry &entry) const
   220 {
   220 {
   221     return m_d->createEntry(entry);
   221     return m_d->createEntry(entry);
   222 }
   222 }
   223 
   223 
   224 /*!
   224 /*!
   282  \retval true if operation was successful
   282  \retval true if operation was successful
   283 
   283 
   284  \example
   284  \example
   285  \code
   285  \code
   286  ...
   286  ...
   287  bool result = service->removeEntries( QList<CaEntry *>() << newItem );
   287  bool result = service->removeEntries( QList< QSharedPointer<CaEntry> >() << newItem );
   288  \b Output:
   288  \b Output:
   289  result == true
   289  result == true
   290  \endcode
   290  \endcode
   291  */
   291  */
   292 bool CaService::removeEntries(const QList<CaEntry *> &entryList) const
   292 bool CaService::removeEntries(const QList< QSharedPointer<CaEntry> > &entryList) const
   293 {
   293 {
   294     QList<int> idList;
   294     QList<int> idList;
   295     CaEntry *entry(NULL);
   295     QSharedPointer<CaEntry> entry;
   296     foreach(entry, entryList) {
   296     foreach(entry, entryList) {
   297         idList << entry->id();
   297         idList << entry->id();
   298     }
   298     }
   299     return removeEntries(idList);
   299     return removeEntries(idList);
   300 }
   300 }
   447  \b Output:
   447  \b Output:
   448  result == true
   448  result == true
   449  \endcode
   449  \endcode
   450  */
   450  */
   451 bool CaService::insertEntriesIntoGroup(const CaEntry &group,
   451 bool CaService::insertEntriesIntoGroup(const CaEntry &group,
   452                                        const QList<CaEntry *> &entryList, int beforeEntryId) const
   452                                        const QList< QSharedPointer<CaEntry> > &entryList, int beforeEntryId) const
   453 {
   453 {
   454     QList<int> idList;
   454     QList<int> idList;
   455     CaEntry *entry(NULL);
   455     QSharedPointer<CaEntry> entry;
   456     foreach(entry, entryList) {
   456     foreach(entry, entryList) {
   457         idList << entry->id();
   457         idList << entry->id();
   458     }
   458     }
   459     return insertEntriesIntoGroup(group.id(), idList, beforeEntryId);
   459     return insertEntriesIntoGroup(group.id(), idList, beforeEntryId);
   460 }
   460 }
   561  \b Output:
   561  \b Output:
   562  result == true
   562  result == true
   563  \endcode
   563  \endcode
   564  */
   564  */
   565 bool CaService::removeEntriesFromGroup(const CaEntry &group,
   565 bool CaService::removeEntriesFromGroup(const CaEntry &group,
   566                                        const QList<CaEntry *> &entryList) const
   566                                        const QList< QSharedPointer<CaEntry> > &entryList) const
   567 {
   567 {
   568     QList<int> idList;
   568     QList<int> idList;
   569     CaEntry *entry(NULL);
   569     QSharedPointer<CaEntry> entry;
   570     foreach(entry, entryList) {
   570     foreach(entry, entryList) {
   571         idList << entry->id();
   571         idList << entry->id();
   572     }
   572     }
   573     return removeEntriesFromGroup(group.id(), idList);
   573     return removeEntriesFromGroup(group.id(), idList);
   574 }
   574 }
   679  CaEntry itemToAppend;
   679  CaEntry itemToAppend;
   680  itemToAppend.setText( "TextAppend" );
   680  itemToAppend.setText( "TextAppend" );
   681  itemToAppend.setTypeName( "TypeNameAppend" );
   681  itemToAppend.setTypeName( "TypeNameAppend" );
   682  CaEntry * entryToAppend = service->createEntry( itemToAppend );
   682  CaEntry * entryToAppend = service->createEntry( itemToAppend );
   683  bool result = service->appendEntriesToGroup(
   683  bool result = service->appendEntriesToGroup(
   684  *newGroup, QList<CaEntry *>() << entryToAppend );
   684  *newGroup, QList< QSharedPointer<CaEntry> >() << entryToAppend );
   685  ...
   685  ...
   686  \b Output:
   686  \b Output:
   687  result == true
   687  result == true
   688  \endcode
   688  \endcode
   689  */
   689  */
   690 bool CaService::appendEntriesToGroup(const CaEntry &group,
   690 bool CaService::appendEntriesToGroup(const CaEntry &group,
   691                                      const QList<CaEntry *> &entryList) const
   691                                      const QList< QSharedPointer<CaEntry> > &entryList) const
   692 {
   692 {
   693     QList<int> idList;
   693     QList<int> idList;
   694     CaEntry *entry(NULL);
   694     QSharedPointer<CaEntry> entry;
   695     foreach(entry, entryList) {
   695     foreach(entry, entryList) {
   696         idList << entry->id();
   696         idList << entry->id();
   697     }
   697     }
   698     return appendEntriesToGroup(group.id(), idList);
   698     return appendEntriesToGroup(group.id(), idList);
   699 }
   699 }
   795  \b Output:
   795  \b Output:
   796  result == true
   796  result == true
   797  \endcode
   797  \endcode
   798  */
   798  */
   799 bool CaService::prependEntriesToGroup(const CaEntry &group,
   799 bool CaService::prependEntriesToGroup(const CaEntry &group,
   800                                       const QList<CaEntry *> &entryList) const
   800                                       const QList< QSharedPointer<CaEntry> > &entryList) const
   801 {
   801 {
   802     QList<int> idList;
   802     QList<int> idList;
   803     CaEntry *entry(NULL);
   803     QSharedPointer<CaEntry> entry;
   804     foreach(entry, entryList) {
   804     foreach(entry, entryList) {
   805         idList << entry->id();
   805         idList << entry->id();
   806     }
   806     }
   807     return prependEntriesToGroup(group.id(), idList);
   807     return prependEntriesToGroup(group.id(), idList);
   808 }
   808 }
   829  */
   829  */
   830 bool CaService::executeCommand(int entryId, const QString &command) const
   830 bool CaService::executeCommand(int entryId, const QString &command) const
   831 {
   831 {
   832     bool result = false;
   832     bool result = false;
   833     
   833     
   834     CaEntry *const temporaryEntry = getEntry(entryId);
   834     const QSharedPointer<CaEntry> temporaryEntry = getEntry(entryId);
   835     
   835     
   836     if (temporaryEntry != NULL) {
   836     if (!temporaryEntry.isNull()) {
   837         result = executeCommand(*temporaryEntry, command);
   837         result = executeCommand(*temporaryEntry, command);
   838         delete temporaryEntry;
       
   839     }
   838     }
   840     return result;
   839     return result;
   841 }
   840 }
   842 
   841 
   843 /*!
   842 /*!
   955 /*!
   954 /*!
   956  Search for entries.
   955  Search for entries.
   957  \param entryIdList list of entry ids
   956  \param entryIdList list of entry ids
   958  \retval list of entries (pointers)
   957  \retval list of entries (pointers)
   959  */
   958  */
   960 QList<CaEntry *> CaServicePrivate::getEntries(const QList<int> &entryIdList) const
   959 QList< QSharedPointer<CaEntry> > CaServicePrivate::getEntries(const QList<int> &entryIdList) const
   961 {
   960 {
   962     qDebug() << "CaServicePrivate::getEntries"
   961     qDebug() << "CaServicePrivate::getEntries"
   963              << "entryIdList:" << entryIdList;
   962              << "entryIdList:" << entryIdList;
   964 
   963 
   965     CACLIENTTEST_FUNC_ENTRY("CaServicePrivate::getEntries");
   964     CACLIENTTEST_FUNC_ENTRY("CaServicePrivate::getEntries");
   966 
   965 
   967     QList<CaEntry *> resultList;
   966     QList< QSharedPointer<CaEntry> > resultList;
   968 
   967 
   969     mErrorCode = mProxy->getData(entryIdList, resultList);
   968     mErrorCode = mProxy->getData(entryIdList, resultList);
   970 
   969 
   971     // figure out whether all entries have been retrieved and
   970     // figure out whether all entries have been retrieved and
   972     // store the operation status
   971     // store the operation status
   985 /*!
   984 /*!
   986  Fetches data from database.
   985  Fetches data from database.
   987  \param query the query information to select specific entries.
   986  \param query the query information to select specific entries.
   988  \param placeholder list of entries for a specific select.
   987  \param placeholder list of entries for a specific select.
   989  */
   988  */
   990 QList<CaEntry *> CaServicePrivate::getEntries(const CaQuery &query) const
   989 QList< QSharedPointer<CaEntry> > CaServicePrivate::getEntries(const CaQuery &query) const
   991 {
   990 {
   992     QList<CaEntry *> resultList;
   991     QList< QSharedPointer<CaEntry> > resultList;
   993 
   992 
   994     mErrorCode = mProxy->getData(query, resultList);
   993     mErrorCode = mProxy->getData(query, resultList);
   995 
   994 
   996     qDebug() << "CaServicePrivate::getEntries mErrorCode:" << mErrorCode;
   995     qDebug() << "CaServicePrivate::getEntries mErrorCode:" << mErrorCode;
   997 
   996 
  1017 /*!
  1016 /*!
  1018  Create copy of entry.
  1017  Create copy of entry.
  1019  \param query const reference to entry to copy
  1018  \param query const reference to entry to copy
  1020  \retval pointer to newely created copy
  1019  \retval pointer to newely created copy
  1021  */
  1020  */
  1022 CaEntry *CaServicePrivate::createEntry(const CaEntry &entry)
  1021 QSharedPointer<CaEntry> CaServicePrivate::createEntry(const CaEntry &entry)
  1023 {
  1022 {
  1024     qDebug() << "CaServicePrivate::createEntry"
  1023     qDebug() << "CaServicePrivate::createEntry"
  1025              << "entry id:" << entry.id();
  1024              << "entry id:" << entry.id();
  1026 
  1025 
  1027     CACLIENTTEST_FUNC_ENTRY("CaServicePrivate::createEntry");
  1026     CACLIENTTEST_FUNC_ENTRY("CaServicePrivate::createEntry");
  1028 
  1027 
  1029     CaEntry *newEntry = NULL;
       
  1030     ErrorCode addDataResult = NoErrorCode;
  1028     ErrorCode addDataResult = NoErrorCode;
  1031 
  1029 
  1032     try {
  1030     QSharedPointer<CaEntry> newEntry (new CaEntry(entry.role())); 
  1033         newEntry = new CaEntry(entry.role());
  1031 
  1034 
  1032     QScopedPointer<CaEntry> entryClone(new CaEntry(entry));
  1035         QScopedPointer<CaEntry> entryClone(new CaEntry(entry));
  1033     const int nonExistingObjectId = 0;
  1036 
  1034     CaObjectAdapter::setId(*entryClone, nonExistingObjectId);
  1037         const int nonExistingObjectId = 0;
  1035 
  1038 
  1036     addDataResult =
  1039         CaObjectAdapter::setId(*entryClone, nonExistingObjectId);
  1037         mProxy->addData(*entryClone, *newEntry);
  1040 
  1038 
  1041         addDataResult =
  1039     // return empty pointer if nothing was added
  1042             mProxy->addData(*entryClone, *newEntry);
  1040     if (addDataResult != NoErrorCode) {
  1043     } catch (const std::bad_alloc &) {
  1041         newEntry.clear();
  1044         addDataResult = OutOfMemoryErrorCode;
       
  1045     }
  1042     }
  1046 
  1043     
  1047     if (addDataResult != NoErrorCode) {
       
  1048         delete newEntry;
       
  1049         newEntry = NULL;
       
  1050     }
       
  1051 
       
  1052     mErrorCode = addDataResult;
  1044     mErrorCode = addDataResult;
  1053 
  1045 
  1054     qDebug() << "CaServicePrivate::createEntry mErrorCode:" << mErrorCode;
  1046     qDebug() << "CaServicePrivate::createEntry mErrorCode:" << mErrorCode;
  1055 
  1047 
  1056     CACLIENTTEST_FUNC_EXIT("CaServicePrivate::createEntry");
  1048     CACLIENTTEST_FUNC_EXIT("CaServicePrivate::createEntry");