Statistics
| Branch: | Tag: | Revision:

one / include / MarketPlaceAppPool.h @ fed93cda

History | View | Annotate | Download (6.49 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#ifndef MARKETPLACEAPP_POOL_H_
18
#define MARKETPLACEAPP_POOL_H_
19

    
20
#include "MarketPlaceApp.h"
21

    
22
class SqlDB;
23

    
24
class MarketPlaceAppPool : public PoolSQL
25
{
26
public:
27
    MarketPlaceAppPool(SqlDB * db):PoolSQL(db, MarketPlaceApp::table){};
28

    
29
    ~MarketPlaceAppPool(){};
30

    
31
    /* ---------------------------------------------------------------------- */
32
    /* Methods for DB management                                              */
33
    /* ---------------------------------------------------------------------- */
34

    
35
    /**
36
     *  Allocates a new MarketPlaceApp, writing it in the pool database.
37
     *    @param uid the user id of the MarketPlace owner
38
     *    @param gid the id of the group this object is assigned to
39
     *    @param uname name of the user
40
     *    @param gname name of the group
41
     *    @param umask permissions umask
42
     *    @param apptemplate MarketPlaceApp definition template
43
     *    @param mp_id of the MarketPlace to store de App
44
     *    @param mp_name of the MarketPlace
45
     *    @param oid the id assigned to the MarketPlace
46
     *    @param error_str Returns the error reason, if any
47
     *
48
     *    @return the oid assigned to the object, -1 in case of failure
49
     */
50
    int allocate(
51
            int                uid,
52
            int                gid,
53
            const std::string& uname,
54
            const std::string& gname,
55
            int                umask,
56
            MarketPlaceAppTemplate * apptemplate,
57
            int                mp_id,
58
            const std::string& mp_name,
59
            int *              oid,
60
            std::string&       error_str);
61

    
62
    /**
63
     *  Drops the MarketPlaceApp from the data base. The object mutex SHOULD be
64
     *  locked.
65
     *    @param objsql a pointer to the MarketPlace object
66
     *    @param error_msg Error reason, if any
67
     *    @return 0 on success, -1 DB error
68
     */
69
    int drop(PoolObjectSQL * objsql, std::string& error_msg);
70

    
71
    /**
72
     *  Imports an app into the marketplace, as reported by the monitor driver
73
     *    @param template to generate app with the from_template64 function
74
     *    @param mp_id of the MarketPlace to store de App
75
     *    @param mp_name of the MarketPlace
76
     *    @param error_str Returns the error reason, if any
77
     *
78
     *    @return the oid assigned to the object, -1 in case of failure, -2
79
     *    already imported
80
     */
81
    int import(const std::string& t64, int mp_id, const std::string& mp_name,
82
            std::string& error_str);
83

    
84
    /**
85
     *  Function to get a MarketPlaceApp from the pool
86
     *    @param oid MarketPlaceApp unique id
87
     *    @param lock locks the MarketPlaceApp mutex
88
     *    @return a pointer to the MarketPlaceApp, 0 if not loaded
89
     */
90
    MarketPlaceApp * get(int oid, bool lock)
91
    {
92
        return static_cast<MarketPlaceApp *>(PoolSQL::get(oid,lock));
93
    };
94

    
95
    /**
96
     *  Gets an object from the pool (if needed the object is loaded from the
97
     *  database).
98
     *   @param name of the object
99
     *   @param uid id of owner
100
     *   @param lock locks the object if true
101
     *
102
     *   @return a pointer to the object, 0 in case of failure
103
     */
104
    MarketPlaceApp * get(const std::string& name, int uid, bool lock)
105
    {
106
        return static_cast<MarketPlaceApp *>(PoolSQL::get(name, uid, lock));
107
    };
108

    
109
    /**
110
     *  Bootstraps the database table(s) associated to the MarketPlace pool
111
     *    @return 0 on success
112
     */
113
    static int bootstrap(SqlDB * _db)
114
    {
115
        return MarketPlaceApp::bootstrap(_db);
116
    };
117

    
118
    /**
119
     *  Dumps the MarketPlace pool in XML format. A filter can be also added to
120
     *  the query
121
     *  @param oss the output stream to dump the pool contents
122
     *  @param where filter for the objects, defaults to all
123
     *  @param limit parameters used for pagination
124
     *
125
     *  @return 0 on success
126
     */
127
    int dump(std::ostringstream& oss, const std::string& where,
128
                const std::string& limit)
129
    {
130
        return PoolSQL::dump(oss, "MARKETPLACEAPP_POOL", MarketPlaceApp::table,
131
                where, limit);
132
    };
133

    
134
    /** Update a particular MarketPlaceApp
135
     *    @param zone pointer to Zone
136
     *    @return 0 on success
137
     */
138
    int update(PoolObjectSQL * objsql);
139

    
140
    /**
141
     *  Factory method to produce objects
142
     *    @return a pointer to the new object
143
     */
144
    PoolObjectSQL * create()
145
    {
146
        return new MarketPlaceApp(-1,-1,"","", 0, 0);
147
    };
148

    
149
    /**
150
     * Erease map element
151
     */
152
    void drop_map_check(const std::string& name){
153
        if (map_check.find( name ) != map_check.end()){
154
            map<std::string,int>::iterator it;
155
            it=map_check.find(name);
156
            map_check.erase (it);
157
        }
158
    }
159

    
160
    /**
161
     * Check an element into map
162
     */
163
    bool test_map_check(const std::string& name){
164
        map_check[name]++;
165
        if (map_check[name] > 0){
166
            return true;
167
        }
168
        return false;
169
    }
170
private:
171
    map<std::string, int> map_check;
172

    
173
    
174
    void insert_map_check(const std::string& name){
175
        map_check.insert(make_pair(name, -1));
176
    }
177
    void reset_map_check(const std::string& name){
178
        if (name != "") {
179
            if (map_check.find( name ) != map_check.end()){
180
                map_check[name] = -1;
181
            }
182
            else{
183
                insert_map_check(name);
184
            }
185
        }
186
    }
187
};
188

    
189
#endif /*MARKETPLACE_POOL_H_*/