summaryrefslogtreecommitdiffstats
path: root/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java
blob: 44dbcd7787e86153bd40622e0fc8534d1c533285 (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
/**
 * 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 org.fedoraproject.candlepin.util.MethodUtil;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * ObjectFactory is used to create and persist the data model.
 * @author mmccune
 */
public class ObjectFactory {

    /** Logger for this class */
    private static Logger logger = Logger.getLogger(ObjectFactory.class);

    /** the singleton instance of the ObjectFactory */
    private static ObjectFactory instance = new ObjectFactory();
    
    private static Map objects;
    

    /**
     * default constructor
     */
    protected ObjectFactory() {
        objects = new HashMap();
        initMockObjects();
    }

    /**
     * @deprecated demo method
     */
    private void initMockObjects() {
        Owner org = new Owner();
        org.setName("test-org");
        
        // User
        User u = new User();
        u.setLogin("test-login");
        u.setPassword("redhat");
        org.addUser(u);
        
        // Consumer
        Consumer c = new Consumer();
        c.setName("fake-consumer-i386");
        c.setOwner(org);
        org.addConsumer(c);
        
        this.store(org);
        this.store(u);
        this.store(c);
    }

    /**
     * Returns the instance of the ObjectFactory.
     * @return the instance of the ObjectFactory.
     */
    public static ObjectFactory get() {
        return instance;
    }
    
    /**
     * Get a List of objects by type
     * @param clazz class to lookup
     * @return List if found. null if not.
     */
    public List<Object> listObjectsByClass(Class<?> clazz) {
        List<Object> retval = null;
        retval = (List<Object>) objects.get(clazz.getName());
        if (retval == null) {
            retval = new LinkedList();
        }
        return retval;
    }

    /**
     * Lookup an Owner by UUID
     * @param clazz class to lookup
     * @param uuid to lookup
     * @return BaseModel if found, null otherwise.
     */
    public Object lookupByUUID(Class<?> clazz, String uuid) {
        return (Object) lookupByFieldName(clazz, "uuid", uuid);
    }

    /**
     * Lookup an object by a field name
     * @param clazz class to lookup
     * @param fieldName field to use
     * @param value value to match
     * @return BaseModel if found, null otherwise.
     */
    public Object lookupByFieldName(Class<?> clazz, String fieldName, String value) {
        String key = clazz.getName();
        if (!objects.containsKey(key)) {
            return null;
        }
        List typelist = (List) objects.get(key);
        for (int i = 0; i < typelist.size(); i++) {
            Object o = typelist.get(i);
            logger.debug("O: " + o);
            String getter = "get" +  fieldName.substring(0, 1).toUpperCase() +
                fieldName.substring(1);
            logger.debug("getter: " + getter);
            Object v = MethodUtil.callMethod(o, getter, new Object[0]);
            logger.debug("v: " + v);
            if (v != null && v.equals(value)) {
                return o;
            }
        }
        return null;
    }

    /**
     * Store an object
     * @param u object to store
     * @return The stored object
     */
    public Object store(Object u) {
        String key = u.getClass().getName();
        if (!objects.containsKey(key)) {
            List newtype = new LinkedList();
            newtype.add(u);
            objects.put(u.getClass().getName(), newtype);
        }
        List typelist = (List) objects.get(key);
        typelist.add(u);
        return u;
    }

    /**
     * Delete an object 
     * @param clazz to lookup  
     * @param removeMe model to remove
     */
    public void delete(Class clazz, Object removeMe) {
        String key = clazz.getName();
        List typelist = (List) objects.get(key);
        typelist.remove(removeMe);
    }
}