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_COMPSENVIRONMENTITEM_HPP
22 #define LIBDNF_TRANSACTION_COMPSENVIRONMENTITEM_HPP
23 
24 #include <memory>
25 #include <vector>
26 
27 namespace libdnf {
28 
29 class CompsEnvironmentItem;
30 typedef std::shared_ptr< CompsEnvironmentItem > CompsEnvironmentItemPtr;
31 
32 class CompsEnvironmentGroup;
33 typedef std::shared_ptr< CompsEnvironmentGroup > CompsEnvironmentGroupPtr;
34 }
35 
36 #include "Item.hpp"
37 #include "CompsGroupItem.hpp"
38 #include "TransactionItem.hpp"
39 
40 namespace libdnf {
41 
42 class CompsEnvironmentItem : public Item {
43 public:
44     explicit CompsEnvironmentItem(SQLite3Ptr conn);
45 
46     CompsEnvironmentItem(SQLite3Ptr conn, int64_t pk);
47 
48     virtual ~CompsEnvironmentItem() = default;
49 
getEnvironmentId() const50     const std::string &getEnvironmentId() const noexcept { return environmentId; }
setEnvironmentId(const std::string & value)51     void setEnvironmentId(const std::string &value) { environmentId = value; }
52 
getName() const53     const std::string &getName() const noexcept { return name; }
setName(const std::string & value)54     void setName(const std::string &value) { name = value; }
55 
getTranslatedName() const56     const std::string &getTranslatedName() const noexcept { return translatedName; }
setTranslatedName(const std::string & value)57     void setTranslatedName(const std::string &value) { translatedName = value; }
58 
getPackageTypes() const59     CompsPackageType getPackageTypes() const noexcept { return packageTypes; }
setPackageTypes(CompsPackageType value)60     void setPackageTypes(CompsPackageType value) { packageTypes = value; }
61 
62     std::string toStr() const override;
getItemType() const63     ItemType getItemType() const noexcept override { return itemType; }
64     void save() override;
65     CompsEnvironmentGroupPtr addGroup(std::string groupId,
66                                       bool installed,
67                                       CompsPackageType groupType);
68     std::vector< CompsEnvironmentGroupPtr > getGroups();
69     static TransactionItemPtr getTransactionItem(SQLite3Ptr conn, const std::string &envid);
70     static std::vector< TransactionItemPtr > getTransactionItemsByPattern(
71         SQLite3Ptr conn,
72         const std::string &pattern);
73     static std::vector< TransactionItemPtr > getTransactionItems(SQLite3Ptr conn,
74                                                                  int64_t transactionId);
75 
76 protected:
77     const ItemType itemType = ItemType::ENVIRONMENT;
78     std::string environmentId;
79     std::string name;
80     std::string translatedName;
81     CompsPackageType packageTypes = CompsPackageType::DEFAULT;
82 
83     void loadGroups();
84     std::vector< CompsEnvironmentGroupPtr > groups;
85 
86 private:
87     friend class CompsEnvironmentGroup;
88     void dbSelect(int64_t pk);
89     void dbInsert();
90 };
91 
92 class CompsEnvironmentGroup {
93 public:
94     explicit CompsEnvironmentGroup(CompsEnvironmentItem &environment);
95 
getId() const96     int64_t getId() const noexcept { return id; }
setId(int64_t value)97     void setId(int64_t value) { id = value; }
98 
getEnvironment() const99     const CompsEnvironmentItem &getEnvironment() const noexcept { return environment; }
100 
getGroupId() const101     const std::string &getGroupId() const noexcept { return groupId; }
setGroupId(const std::string & value)102     void setGroupId(const std::string &value) { groupId = value; }
103 
getInstalled() const104     bool getInstalled() const noexcept { return installed; }
setInstalled(bool value)105     void setInstalled(bool value) { installed = value; }
106 
getGroupType() const107     CompsPackageType getGroupType() const noexcept { return groupType; }
setGroupType(CompsPackageType value)108     void setGroupType(CompsPackageType value) { groupType = value; }
109 
110     // virtual std::string toStr();
111     void save();
112 
113 protected:
114     int64_t id = 0;
115     CompsEnvironmentItem &environment;
116     std::string groupId;
117     bool installed = false;
118     CompsPackageType groupType;
119 
120 private:
121     void dbInsert();
122 };
123 
124 } // namespace libdnf
125 
126 #endif // LIBDNF_TRANSACTION_COMPSENVIRONMENTITEM_HPP
127