summaryrefslogtreecommitdiffstats
path: root/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java
blob: b22249da155ed7d77b9d2b2c54843ffa3c97b111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
 * Copyright (c) 2009 Red Hat, Inc.
 *
 * This software is licensed to you under the GNU General Public License,
 * version 2 (GPLv2). There is NO WARRANTY for this software, express or
 * implied, including the implied warranties of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
 * along with this software; if not, see
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
 *
 * Red Hat trademarks are not licensed under GPLv2. No permission is
 * granted to use or replicate Red Hat trademarks that are incorporated
 * in this software or its documentation.
 */
package org.fedoraproject.candlepin.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.hibernate.annotations.ForeignKey;

/**
 * A Consumer is the entity that uses a given Entitlement. It can be a user,
 * system, or anything else we want to track as using the Entitlement.
 * 
 * Every Consumer has an Owner which may or may not own the Entitlement. The
 * Consumer's attributes or metadata is stored in a ConsumerInfo object which
 * boils down to a series of name/value pairs.
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@Entity
@Table(name="cp_consumer")
public class Consumer {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    
    @Column(nullable=false)
    private String name;
    
    @ManyToOne
    @JoinColumn(nullable=false)
    @ForeignKey(name="fk_consumer_consumer_type")
    private ConsumerType type;
    
    @ManyToOne
    @ForeignKey(name="fk_consumer_owner")
    private Owner owner;
    
    // TODO: Is this worth mapping? Do we need a hierarchy amidst consumers?
    @Transient
    private Consumer parent;

    // TODO: Are we sure we want to track this explicitly? Wouldn't we examine the 
    // entitlements we're consuming and the products associated to them for this info?
    @OneToMany
    @ForeignKey(name = "fk_consumer_product_consumer_id",
                inverseName = "fk_consumer_product_product_id")

    @JoinTable(name="cp_consumer_products",
            joinColumns=@JoinColumn(name="consumer_id"),
            inverseJoinColumns=@JoinColumn(name="product_id"))
    private Set<Product> consumedProducts;
    
    @Transient // TODO
    private Set<Entitlement> entitlements;
    
    @OneToOne(cascade=CascadeType.ALL)
    @PrimaryKeyJoinColumn
    private ConsumerInfo info;
    
    public Consumer(String name, Owner owner, ConsumerType type) {
        this.name = name;
        this.owner = owner;
        this.type = type;
        
        this.info = new ConsumerInfo();
        this.info.setConsumer(this); // TODO: ???
        this.consumedProducts = new HashSet<Product>();
        this.entitlements = new HashSet<Entitlement>();
    }

    public Consumer() {
        this.info = new ConsumerInfo();
        this.info.setConsumer(this); // TODO: ???
        this.consumedProducts = new HashSet<Product>();
        this.entitlements = new HashSet<Entitlement>();
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return Returns the type.
     */
    public ConsumerType getType() {
        return type;
    }
    
    /**
     * @param typeIn The type to set.
     */
    public void setType(ConsumerType typeIn) {
        type = typeIn;
    }

    /**
     * @return the parent
     */
    public Consumer getParent() {
        return parent;
    }

    /**
     * @param parent the parent to set
     */
    public void setParent(Consumer parent) {
        this.parent = parent;
    }

    /**
     * @return the consumedProducts
     */
    public Set<Product> getConsumedProducts() {
        return consumedProducts;
    }

    /**
     * @param consumedProducts the consumedProducts to set
     */
    public void setConsumedProducts(Set<Product> consumedProducts) {
        this.consumedProducts = consumedProducts;
    }

    /**
     * Add a Product to this Consumer.
     * @param p Product to be consumed.
     */
    public void addConsumedProduct(Product p) {
        this.consumedProducts.add(p);
    }

    /**
     * @return the owner
     */
    @XmlTransient
    public Owner getOwner() {
        return owner;
    }

    /**
     * @param owner the owner to set
     */
    public void setOwner(Owner owner) {
        this.owner = owner;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "Consumer [type=" + this.getType() + ", getName()=" +
            getName() + "]";
    }

    
    /**
     * @return Returns the info.
     */
    public ConsumerInfo getInfo() {
        return info;
    }

    
    /**
     * @param infoIn The info to set.
     */
    public void setInfo(ConsumerInfo infoIn) {
        info = infoIn;
    }
    
    /**
     * Set a metadata field
     * @param name to set
     * @param value to set
     */
    public void setMetadataField(String name, String value) {
        if (this.getInfo().getMetadata() == null) {
            this.getInfo().setMetadata(new HashMap<String, String>());
        }
        this.getInfo().getMetadata().put(name, value);
    }
    
    /**
     * Get a metadata field value
     * @param name of field to fetch
     * @return String field value.
     */
    public String getMetadataField(String name) {
       if (this.getInfo().getMetadata() != null) {
           return getInfo().getMetadata().get(name);
       }
       return null;
    }

    /**
     * @return Returns the entitlements.
     */
    public Set<Entitlement> getEntitlements() {
        return entitlements;
    }

    
    /**
     * @param entitlementsIn The entitlements to set.
     */
    public void setEntitlements(Set<Entitlement> entitlementsIn) {
        entitlements = entitlementsIn;
    }

    /**
     * Add an Entitlement to this Consumer
     * @param entitlementIn to add to this consumer
     * 
     */
    public void addEntitlement(Entitlement entitlementIn) {
        this.entitlements.add(entitlementIn);
        
    }


}