1 /*
2  * Copyright (C) 2017-2018 Red Hat, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License Version 2.1
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef LIBDNF_TRANSACTION_COMPSGROUPITEM_HPP
22 #define LIBDNF_TRANSACTION_COMPSGROUPITEM_HPP
23 
24 #include "libdnf/error.hpp"
25 
26 #include <memory>
27 #include <vector>
28 
29 namespace libdnf {
30 
31 enum class CompsPackageType : int {
32     CONDITIONAL = 1 << 0,
33     DEFAULT = 1 << 1,
34     MANDATORY = 1 << 2,
35     OPTIONAL = 1 << 3
36 };
37 
38 class CompsGroupItem;
39 class CompsGroupPackage;
40 
41 typedef std::shared_ptr< CompsGroupItem > CompsGroupItemPtr;
42 typedef std::shared_ptr< CompsGroupPackage > CompsGroupPackagePtr;
43 
44 CompsPackageType listToCompsPackageType(const std::vector<std::string> & types);
45 CompsPackageType stringToCompsPackageType(const std::string & str);
46 std::string compsPackageTypeToString(CompsPackageType type);
47 
operator |(CompsPackageType a,CompsPackageType b)48 inline CompsPackageType operator|(CompsPackageType a, CompsPackageType b)
49 {
50     return static_cast<CompsPackageType>(static_cast<int>(a) | static_cast<int>(b));
51 }
52 
operator &(CompsPackageType a,CompsPackageType b)53 inline CompsPackageType operator&(CompsPackageType a, CompsPackageType b)
54 {
55     return static_cast<CompsPackageType>(static_cast<int>(a) & static_cast<int>(b));
56 }
57 
operator |=(CompsPackageType & a,CompsPackageType b)58 inline CompsPackageType & operator|=(CompsPackageType & a, CompsPackageType b)
59 {
60     a = a | b;
61     return a;
62 }
63 
operator &=(CompsPackageType & a,CompsPackageType b)64 inline CompsPackageType & operator&=(CompsPackageType & a, CompsPackageType b)
65 {
66     a = a & b;
67     return a;
68 }
69 
70 class InvalidCompsPackageTypeError : public Error {
71 public:
InvalidCompsPackageTypeError(const std::string & what)72     InvalidCompsPackageTypeError(const std::string & what) : Error(what) {}
73 };
74 
75 }
76 
77 #include "Item.hpp"
78 #include "TransactionItem.hpp"
79 
80 namespace libdnf {
81 
82 class CompsGroupItem : public Item {
83 public:
84     explicit CompsGroupItem(SQLite3Ptr conn);
85     CompsGroupItem(SQLite3Ptr conn, int64_t pk);
86     virtual ~CompsGroupItem() = default;
87 
getGroupId() const88     const std::string &getGroupId() const noexcept { return groupId; }
setGroupId(const std::string & value)89     void setGroupId(const std::string &value) { groupId = value; }
90 
getName() const91     const std::string &getName() const noexcept { return name; }
setName(const std::string & value)92     void setName(const std::string &value) { name = value; }
93 
getTranslatedName() const94     const std::string &getTranslatedName() const noexcept { return translatedName; }
setTranslatedName(const std::string & value)95     void setTranslatedName(const std::string &value) { translatedName = value; }
96 
getPackageTypes() const97     CompsPackageType getPackageTypes() const noexcept { return packageTypes; }
setPackageTypes(CompsPackageType value)98     void setPackageTypes(CompsPackageType value) { packageTypes = value; }
99 
100     std::string toStr() const override;
getItemType() const101     ItemType getItemType() const noexcept override { return itemType; }
102     void save() override;
103     CompsGroupPackagePtr addPackage(std::string name, bool installed, CompsPackageType pkgType);
104     std::vector< CompsGroupPackagePtr > getPackages();
105     static TransactionItemPtr getTransactionItem(SQLite3Ptr conn, const std::string &groupid);
106     static std::vector< TransactionItemPtr > getTransactionItemsByPattern(
107         SQLite3Ptr conn,
108         const std::string &pattern);
109     static std::vector< TransactionItemPtr > getTransactionItems(SQLite3Ptr conn,
110                                                                  int64_t transactionId);
111 
112 protected:
113     const ItemType itemType = ItemType::GROUP;
114     std::string groupId;
115     std::string name;
116     std::string translatedName;
117     CompsPackageType packageTypes;
118 
119     void loadPackages();
120     std::vector< CompsGroupPackagePtr > packages;
121 
122 private:
123     friend class CompsGroupPackage;
124     void dbSelect(int64_t pk);
125     void dbInsert();
126 };
127 
128 class CompsGroupPackage {
129 public:
130     explicit CompsGroupPackage(CompsGroupItem &group);
131 
getId() const132     int64_t getId() const noexcept { return id; }
setId(int64_t value)133     void setId(int64_t value) { id = value; }
134 
getGroup() const135     const CompsGroupItem &getGroup() const noexcept { return group; }
136 
getName() const137     const std::string &getName() const noexcept { return name; }
setName(const std::string & value)138     void setName(const std::string &value) { name = value; }
139 
getInstalled() const140     bool getInstalled() const noexcept { return installed; }
setInstalled(bool value)141     void setInstalled(bool value) { installed = value; }
142 
getPackageType() const143     CompsPackageType getPackageType() const noexcept { return packageType; }
setPackageType(CompsPackageType value)144     void setPackageType(CompsPackageType value) { packageType = value; }
145 
146     // virtual std::string toStr();
147     void save();
148 
149 protected:
150     int64_t id = 0;
151     CompsGroupItem &group;
152     std::string name;
153     bool installed = false;
154     CompsPackageType packageType = CompsPackageType::DEFAULT;
155 
156 private:
157     void dbInsert();
158     void dbSelectOrInsert();
159     void dbUpdate();
160 };
161 
162 } // namespace libdnf
163 
164 #endif // LIBDNF_TRANSACTION_COMPSGROUPITEM_HPP
165