From fc6537a46c54d37cd4dbf0d2ef9519024dd039bf Mon Sep 17 00:00:00 2001 From: Tomasz Iwanek Date: Tue, 16 Oct 2012 18:28:07 +0200 Subject: [PATCH] WidgetDAO from WidgetHandle construction depracated. Update method for widget in one transaction [Issue#] N/A [Feature] This change allows construction of widgetDAO only from GUID on pkgname [Cause] Widgethandle is going to be removed from other repositories [Solution] N/A [Verification] Build wrt-commons. Run dpl tests. [SCMRequest] Should be introduced with wrt-installer change id: I05a81d43a97f2a490d490edf62a5ef874c3477f5 Methods marked as deprecated should be removed soon after wrt-installer changes. Change-Id: Ib82cbbf575d88a9ce764652fa65037a7b990340a --- modules/widget_dao/dao/widget_dao.cpp | 214 +++++++++++++++------ .../include/dpl/wrt-dao-ro/common_dao_types.h | 1 + .../include/dpl/wrt-dao-ro/widget_dao_read_only.h | 3 +- .../widget_dao/include/dpl/wrt-dao-rw/widget_dao.h | 89 +++++---- modules/widget_dao/orm/wrt_db | 4 +- 5 files changed, 212 insertions(+), 99 deletions(-) diff --git a/modules/widget_dao/dao/widget_dao.cpp b/modules/widget_dao/dao/widget_dao.cpp index 8b20f55..e899087 100644 --- a/modules/widget_dao/dao/widget_dao.cpp +++ b/modules/widget_dao/dao/widget_dao.cpp @@ -51,21 +51,6 @@ namespace WrtDB { message); \ } -#define CHECK_WIDGET_EXISTENCE(macro_transaction, macro_handle) \ - if (!WidgetDAO::isWidgetInstalled(macro_handle)) \ - { \ - macro_transaction.Commit(); \ - LogWarning("Cannot find widget. Handle: " << macro_handle); \ - ThrowMsg(WidgetDAO::Exception::WidgetNotExist, \ - "Cannot find widget. Handle: " << macro_handle); \ - } - - -WidgetDAO::WidgetDAO(DbWidgetHandle widgetHandle) : - WidgetDAOReadOnly(widgetHandle) -{ -} - WidgetDAO::WidgetDAO(DPL::OptionalString widgetGUID) : WidgetDAOReadOnly(WidgetDAOReadOnly::getHandle(widgetGUID)) { @@ -76,6 +61,11 @@ WidgetDAO::WidgetDAO(DPL::String pkgName) : { } +WidgetDAO::WidgetDAO(DbWidgetHandle handle) : + WidgetDAOReadOnly(handle) +{ +} + WidgetDAO::~WidgetDAO() { } @@ -130,7 +120,7 @@ void WidgetDAO::setPkgName(const DPL::OptionalString& pkgName) } void WidgetDAO::registerWidget( - const DbWidgetHandle& widgetHandle, + const WidgetPkgName & widgetPkgname, const WidgetRegisterInfo &widgetRegInfo, const IWacSecurity &wacSecurity) { @@ -138,46 +128,110 @@ void WidgetDAO::registerWidget( SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN { DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface()); + DbWidgetHandle widgetHandle = registerWidgetInternal(widgetPkgname, widgetRegInfo, wacSecurity); + registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations); + //TODO: remove construction from handle and move that call to registerWidgetInternal + // in new version external fiels should be register together with other informations + transaction.Commit(); + } + SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget") +} - //Register into WidgetInfo has to be first - //as all other tables depend upon that - registerWidgetInfo(widgetHandle, widgetRegInfo, wacSecurity); +void WidgetDAO::registerWidget( + WrtDB::DbWidgetHandle handle, + const WidgetRegisterInfo & widgetRegInfo, + const IWacSecurity &wacSecurity) +{ + LogDebug("Registering widget"); + SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN + { + DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface()); + registerWidgetInternal(L"", widgetRegInfo, wacSecurity, handle); + transaction.Commit(); + } + SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget") +} + +DbWidgetHandle WidgetDAO::registerWidget( + const WidgetRegisterInfo &pWidgetRegisterInfo, + const IWacSecurity &wacSecurity) +{ + //make it more precise due to very fast tests + struct timeval tv; + gettimeofday(&tv, NULL); + srand(time(NULL) + tv.tv_usec); + DbWidgetHandle widgetHandle; + do { + widgetHandle = rand(); + } while (isWidgetInstalled(widgetHandle)); + + registerWidget(widgetHandle, pWidgetRegisterInfo, wacSecurity); + return widgetHandle; +} - registerWidgetExtendedInfo(widgetHandle, widgetRegInfo); +DbWidgetHandle WidgetDAO::registerWidgetInternal( + const WidgetPkgName & widgetName, + const WidgetRegisterInfo &widgetRegInfo, + const IWacSecurity &wacSecurity, + const DPL::Optional handle) +{ + //Register into WidgetInfo has to be first + //as all other tables depend upon that + DbWidgetHandle widgetHandle = registerWidgetInfo(widgetName, widgetRegInfo, wacSecurity, handle); - registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo); + registerWidgetExtendedInfo(widgetHandle, widgetRegInfo); - registerWidgetIcons(widgetHandle, widgetRegInfo); + registerWidgetLocalizedInfo(widgetHandle, widgetRegInfo); - registerWidgetStartFile(widgetHandle, widgetRegInfo); + registerWidgetIcons(widgetHandle, widgetRegInfo); - PropertyDAO::RegisterProperties(widgetHandle, widgetRegInfo); + registerWidgetStartFile(widgetHandle, widgetRegInfo); - registerWidgetFeatures(widgetHandle, widgetRegInfo); + PropertyDAO::RegisterProperties(widgetHandle, widgetRegInfo); - registerWidgetWindowModes(widgetHandle, widgetRegInfo); + registerWidgetFeatures(widgetHandle, widgetRegInfo); - registerWidgetWarpInfo(widgetHandle, widgetRegInfo); + registerWidgetWindowModes(widgetHandle, widgetRegInfo); - registerWidgetCertificates(widgetHandle, wacSecurity); + registerWidgetWarpInfo(widgetHandle, widgetRegInfo); - CertificateChainList list; - wacSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR); - registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list); + registerWidgetCertificates(widgetHandle, wacSecurity); - list.clear(); - wacSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR); - registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list); + CertificateChainList list; + wacSecurity.getCertificateChainList(list, SIGNATURE_DISTRIBUTOR); + registerCertificatesChains(widgetHandle, SIGNATURE_DISTRIBUTOR, list); - registerWidgetSettings(widgetHandle, widgetRegInfo); + list.clear(); + wacSecurity.getCertificateChainList(list, SIGNATURE_AUTHOR); + registerCertificatesChains(widgetHandle, SIGNATURE_AUTHOR, list); - registerAppService(widgetHandle, widgetRegInfo); + registerWidgetSettings(widgetHandle, widgetRegInfo); - registerEncryptedResouceInfo(widgetHandle, widgetRegInfo); + registerAppService(widgetHandle, widgetRegInfo); + registerEncryptedResouceInfo(widgetHandle, widgetRegInfo); + + return widgetHandle; +} + +void WidgetDAO::registerOrUpdateWidget( + const WidgetPkgName & widgetName, + const WidgetRegisterInfo &widgetRegInfo, + const IWacSecurity &wacSecurity) +{ + LogDebug("Reregistering widget"); + SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN + { + DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface()); + + unregisterWidgetInternal(widgetName); + DbWidgetHandle widgetHandle = registerWidgetInternal(widgetName, widgetRegInfo, wacSecurity); + registerExternalLocations(widgetHandle, widgetRegInfo.externalLocations); + //TODO: remove construction from handle and move that call to registerWidgetInternal + // in new version external fiels should be register together with other informations transaction.Commit(); } - SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to register widget") + SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to reregister widget") } #define DO_INSERT(row, table) \ @@ -208,10 +262,11 @@ void WidgetDAO::registerWidgetExtendedInfo(DbWidgetHandle widgetHandle, DO_INSERT(row, WidgetExtendedInfo) } -void WidgetDAO::registerWidgetInfo( - const DbWidgetHandle& widgetHandle, +DbWidgetHandle WidgetDAO::registerWidgetInfo( + const WidgetPkgName & widgetName, const WidgetRegisterInfo ®Info, - const IWacSecurity &wacSecurity) + const IWacSecurity &wacSecurity, + const DPL::Optional handle) { using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt; @@ -221,7 +276,10 @@ void WidgetDAO::registerWidgetInfo( // Because of that, "Optional" is not used there WidgetInfo::Row row; - row.Set_app_id(widgetHandle); + if(!!handle) + { + row.Set_app_id(*handle); + } row.Set_widget_type(regInfo.type.appType); row.Set_widget_id(widgetConfigurationInfo.widget_id); row.Set_defaultlocale(widgetConfigurationInfo.defaultlocale); @@ -236,6 +294,7 @@ void WidgetDAO::registerWidgetInfo( row.Set_recognized(wacSecurity.isRecognized()); row.Set_wac_signed(wacSecurity.isWacSigned()); row.Set_distributor_signed(wacSecurity.isDistributorSigned()); + row.Set_pkgname(widgetName); { std::stringstream tmp; tmp << regInfo.minVersion; @@ -255,6 +314,18 @@ void WidgetDAO::registerWidgetInfo( ReThrowMsg(WidgetDAO::Exception::DatabaseError, "Failed to register widget info."); } + + if(!handle) + { + //get autoincremented value of widgetHandle + WRT_DB_SELECT(select, WidgetInfo, &WrtDatabase::interface()) + select->Where(Equals(widgetName)); + return select->GetSingleValue(); + } + else + { + return *handle; + } } void WidgetDAO::registerWidgetLocalizedInfo(DbWidgetHandle widgetHandle, @@ -565,16 +636,22 @@ void WidgetDAO::registerEncryptedResouceInfo(DbWidgetHandle widgetHandle, void WidgetDAO::registerExternalLocations(const ExternalLocationList & externals) { + registerExternalLocations(m_widgetHandle, externals); +} + +void WidgetDAO::registerExternalLocations(DbWidgetHandle widgetHandle, + const ExternalLocationList & externals) +{ SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN { using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt; DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface()); - LogDebug("Inserting external files for widgetHandle: " << m_widgetHandle); + LogDebug("Inserting external files for widgetHandle: " << widgetHandle); FOREACH(it, externals) { WidgetExternalLocations::Row row; - row.Set_app_id(m_widgetHandle); + row.Set_app_id(widgetHandle); row.Set_path(DPL::FromUTF8String(*it)); DO_INSERT(row, WidgetExternalLocations) @@ -594,35 +671,56 @@ void WidgetDAO::unregisterAllExternalLocations() del->Execute(); } -#undef DO_INSERT - -void WidgetDAO::unregisterWidget(DbWidgetHandle widgetHandle) +void WidgetDAO::unregisterWidget(const WidgetPkgName & pkgName) { - LogDebug("Unregistering widget from DB. Handle: " << widgetHandle); + LogDebug("Unregistering widget from DB. PkgName: " << pkgName); SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN { DPL::DB::ORM::wrt::ScopedTransaction transaction(&WrtDatabase::interface()); + unregisterWidgetInternal(pkgName); + transaction.Commit(); + } + SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget") +} + +void WidgetDAO::unregisterWidget(WrtDB::DbWidgetHandle handle) +{ + LogDebug("Unregistering widget from DB. Handle: " << handle); + SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN + { using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt; - - CHECK_WIDGET_EXISTENCE(transaction, widgetHandle) + ScopedTransaction transaction(&WrtDatabase::interface()); // Delete from table Widget Info - { - WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface()) - del->Where(Equals(widgetHandle)); - del->Execute(); - } - - // Deleting in other tables is done via "delete cascade" in SQL + WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface()) + del->Where(Equals(handle)); + del->Execute(); transaction.Commit(); } SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to unregister widget") } +void WidgetDAO::unregisterWidgetInternal( + const WidgetPkgName & pkgName) +{ + using namespace DPL::DB::ORM; + using namespace DPL::DB::ORM::wrt; + + DbWidgetHandle handle = getHandle(pkgName); + + // Delete from table Widget Info + WRT_DB_DELETE(del, WidgetInfo, &WrtDatabase::interface()) + del->Where(Equals(handle)); + del->Execute(); + + // Deleting in other tables is done via "delete cascade" in SQL +} + +#undef DO_INSERT + #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN #undef SQL_CONNECTION_EXCEPTION_HANDLER_END -#undef CHECK_WIDGET_EXISTENCE } // namespace WrtDB diff --git a/modules/widget_dao/include/dpl/wrt-dao-ro/common_dao_types.h b/modules/widget_dao/include/dpl/wrt-dao-ro/common_dao_types.h index c13dd75..d801a6a 100644 --- a/modules/widget_dao/include/dpl/wrt-dao-ro/common_dao_types.h +++ b/modules/widget_dao/include/dpl/wrt-dao-ro/common_dao_types.h @@ -81,6 +81,7 @@ class PluginObjectsDAO * FindWidgetModel routine. */ typedef int DbWidgetHandle; +typedef DPL::String WidgetPkgName; /** * Value of invalid widget handle diff --git a/modules/widget_dao/include/dpl/wrt-dao-ro/widget_dao_read_only.h b/modules/widget_dao/include/dpl/wrt-dao-ro/widget_dao_read_only.h index 3a11455..1c28e52 100644 --- a/modules/widget_dao/include/dpl/wrt-dao-ro/widget_dao_read_only.h +++ b/modules/widget_dao/include/dpl/wrt-dao-ro/widget_dao_read_only.h @@ -103,6 +103,7 @@ typedef std::list WidgetCertificateDataList; typedef DPL::String Locale; typedef std::set LocaleSet; +typedef std::list ExternalLocationList; /** * WidgetRegisterInfo @@ -177,6 +178,7 @@ struct WidgetRegisterInfo time_t installedTime; PkgType pType; EncryptedFileList encryptedFiles; + ExternalLocationList externalLocations; }; typedef std::list CertificateChainList; @@ -225,7 +227,6 @@ typedef std::list LanguageTagList; typedef std::list HostList; typedef std::list FingerPrintList; typedef std::list ResourceAttributeList; -typedef std::list ExternalLocationList; //TODO: if there will be many files registered std::set is better class WidgetDAOReadOnly { diff --git a/modules/widget_dao/include/dpl/wrt-dao-rw/widget_dao.h b/modules/widget_dao/include/dpl/wrt-dao-rw/widget_dao.h index e15ac5a..30b355c 100644 --- a/modules/widget_dao/include/dpl/wrt-dao-rw/widget_dao.h +++ b/modules/widget_dao/include/dpl/wrt-dao-rw/widget_dao.h @@ -44,13 +44,7 @@ class WidgetDAO : public WidgetDAOReadOnly public: typedef std::list LanguageTagsList; - /** - * This is a constructor. - * - * @param[in] widgetHandle application id of widget. - * @param[in] widgetGUID application guid of widget. - */ - WidgetDAO(DbWidgetHandle widgetHandle); + WidgetDAO(DbWidgetHandle handle); WidgetDAO(DPL::OptionalString widgetGUID); WidgetDAO(DPL::String pkgName); @@ -60,56 +54,58 @@ class WidgetDAO : public WidgetDAOReadOnly virtual ~WidgetDAO(); /** - * This method registers the widget information to the DB when it is installed. + * This method registers the widget information in the DB when it is installed. * * @see WidgetRegisterInfo * @see UnRegisterWidget() - * @param[in] widgetHandle Widget ID that will be registered. + * @param[in] widgetPkgname Widget Pkgname that will be registered. * @param[in] pWidgetRegisterInfo Specified the widget's information needed to be registered. * @param[in] wacSecurity Widget's security certificates. */ static void registerWidget( - const DbWidgetHandle& widgetHandle, - const WidgetRegisterInfo &pWidgetRegisterInfo, + const WidgetPkgName & widgetPkgname, + const WidgetRegisterInfo &widgetRegInfo, const IWacSecurity &wacSecurity); + static void registerWidget( + WrtDB::DbWidgetHandle handle, + const WidgetRegisterInfo &widgetRegInfo, + const IWacSecurity &wacSecurity) __attribute__((deprecated)); + static DbWidgetHandle registerWidget( - const WidgetRegisterInfo &pWidgetRegisterInfo, - const IWacSecurity &wacSecurity) __attribute__((deprecated)) - { - //make it more precise due to very fast tests - struct timeval tv; - gettimeofday(&tv, NULL); - srand(time(NULL) + tv.tv_usec); - DbWidgetHandle widgetHandle; - do { - widgetHandle = rand(); - } while (isWidgetInstalled(widgetHandle)); - - registerWidget(widgetHandle, pWidgetRegisterInfo, wacSecurity); - return widgetHandle; - } + const WidgetRegisterInfo &pWidgetRegisterInfo, + const IWacSecurity &wacSecurity) __attribute__((deprecated)); + + /** + * This method re-registers the widget information to the DB when it is installed. + * + * It performs unregistration and new registration of widget in db in one transaction. + * + * @see WidgetRegisterInfo + * @param[in] widgetName Widget pkgname that will be registered. + * @param[in] pWidgetRegisterInfo Specified the widget's information needed to be registered. + * @param[in] wacSecurity Widget's security certificates. + */ + static void registerOrUpdateWidget( + const WidgetPkgName & widgetName, + const WidgetRegisterInfo &widgetRegInfo, + const IWacSecurity &wacSecurity); /** * This method removes a widget's information from EmDB. * * @see RegisterWidget() - * @param[in] widgetHandle widget's app id - * @return true if succeed, false if fail. + * @param[in] pkgName widgets name to be unregistered */ - static void unregisterWidget(DbWidgetHandle widgetHandle); + static void unregisterWidget(const WidgetPkgName & pkgName); + + static void unregisterWidget(WrtDB::DbWidgetHandle handle) __attribute__((deprecated)); /* This method removes widget property */ void removeProperty(const PropertyDAOReadOnly::WidgetPropertyKey &key); /** - * @brief registerExternalLocations Inserts new rows to WidgetExternalLocations - * @param externals list of files - */ - void registerExternalLocations(const ExternalLocationList & externals); - - /** * @brief registerExternalLocations Removes rows from WidgetExternalLocations */ void unregisterAllExternalLocations(); @@ -130,12 +126,15 @@ class WidgetDAO : public WidgetDAOReadOnly */ void updateFeatureRejectStatus(const DbWidgetFeature &widgetFeature); + void registerExternalLocations(const ExternalLocationList & externals) __attribute__((deprecated)); + private: //Methods used during widget registering - static void registerWidgetInfo( - const DbWidgetHandle& widgetHandle, + static DbWidgetHandle registerWidgetInfo( + const WidgetPkgName & widgetName, const WidgetRegisterInfo ®Info, - const IWacSecurity &wacSecurity); + const IWacSecurity &wacSecurity, + const DPL::Optional handle = DPL::Optional()); static void registerWidgetExtendedInfo( DbWidgetHandle widgetHandle, const WidgetRegisterInfo ®Info); @@ -176,6 +175,20 @@ class WidgetDAO : public WidgetDAOReadOnly static void registerEncryptedResouceInfo( DbWidgetHandle widgetHandle, const WidgetRegisterInfo ®Info); + /** + * @brief registerExternalLocations Inserts new rows to WidgetExternalLocations + * @param externals list of files + */ + static void registerExternalLocations(DbWidgetHandle widgetHandle, + const ExternalLocationList & externals); + + static DbWidgetHandle registerWidgetInternal( + const WidgetPkgName & widgetName, + const WidgetRegisterInfo &widgetRegInfo, + const IWacSecurity &wacSecurity, + const DPL::Optional handle = DPL::Optional()); + static void unregisterWidgetInternal( + const WidgetPkgName & pkgName); }; } // namespace WrtDB diff --git a/modules/widget_dao/orm/wrt_db b/modules/widget_dao/orm/wrt_db index 03cc13d..0dfedb4 100644 --- a/modules/widget_dao/orm/wrt_db +++ b/modules/widget_dao/orm/wrt_db @@ -19,7 +19,7 @@ SQL( ) CREATE_TABLE(WidgetInfo) - COLUMN_NOT_NULL(app_id, INTEGER, PRIMARY KEY) + COLUMN_NOT_NULL(app_id, INTEGER, PRIMARY KEY AUTOINCREMENT) COLUMN(widget_type, INT, DEFAULT 1) COLUMN(widget_id, VARCHAR(256), DEFAULT '') COLUMN(widget_version, VARCHAR(256), DEFAULT '') @@ -38,7 +38,7 @@ CREATE_TABLE(WidgetInfo) COLUMN_NOT_NULL(back_supported, TINYINT, DEFAULT 0) COLUMN(access_network, TINYINT, DEFAULT 0) COLUMN(defaultlocale, VARCHAR(256), DEFAULT 0) - COLUMN(pkgname, VARCHAR(256), DEFAULT 0) + COLUMN(pkgname, VARCHAR(256), DEFAULT 0 UNIQUE) COLUMN(pkg_type, INT, DEFAULT 0) CREATE_TABLE_END() -- 2.7.4