~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet.example/src/org/restlet/example/ext/rdf/foaf/objects/ObjectsFacade.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
4
 * The contents of this file are subject to the terms of one of the following
 
5
 * open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
 
6
 * 1.0 (the "Licenses"). You can select the license that you prefer but you may
 
7
 * not use this file except in compliance with one of these Licenses.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
27
 * Alternatively, you can obtain a royalty free commercial license with less
 
28
 * limitations, transferable or non-transferable, directly at
 
29
 * http://www.restlet.com/products/restlet-framework
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.example.ext.rdf.foaf.objects;
 
35
 
 
36
import java.util.List;
 
37
 
 
38
import org.restlet.example.ext.rdf.foaf.data.DataFacade;
 
39
 
 
40
/**
 
41
 *
 
42
 */
 
43
public class ObjectsFacade {
 
44
 
 
45
    /** Data facade. */
 
46
    protected DataFacade dataFacade;
 
47
 
 
48
    public ObjectsFacade(DataFacade dataFacade) {
 
49
        super();
 
50
        this.dataFacade = dataFacade;
 
51
    }
 
52
 
 
53
    /**
 
54
     * Add a new Contact object in the database.
 
55
     * 
 
56
     * @param User
 
57
     *            the parent user.
 
58
     * @param contact
 
59
     *            new Contact object to be added.
 
60
     * @return the contact object completed with its identifiant.
 
61
     */
 
62
    public Contact createContact(User user, Contact contact) {
 
63
        contact = this.dataFacade.createContact(user, contact);
 
64
        user.getContacts().add(contact);
 
65
        this.dataFacade.updateUser(user);
 
66
 
 
67
        return contact;
 
68
    }
 
69
 
 
70
    /**
 
71
     * Add a new User object in the database.
 
72
     * 
 
73
     * @param user
 
74
     *            new User object to be added.
 
75
     * @return the user object completed with its identfiant.
 
76
     * @throws ObjectsException
 
77
     */
 
78
    public User createUser(User user) throws ObjectsException {
 
79
        user = this.dataFacade.createUser(user);
 
80
        return user;
 
81
    }
 
82
 
 
83
    /**
 
84
     * Delete a contact.
 
85
     * 
 
86
     * @param user
 
87
     *            the parent user.
 
88
     * @param contact
 
89
     *            the contact to be deleted.
 
90
     */
 
91
    public void deleteContact(User user, Contact contact) {
 
92
        // Remove the contact from the mailbox's list of contacts.
 
93
        boolean found = false;
 
94
        for (int i = 0; (i < user.getContacts().size()) && !found; i++) {
 
95
            final Contact contact2 = user.getContacts().get(i);
 
96
            if (contact2.getId().equals(contact.getId())) {
 
97
                user.getContacts().remove(i);
 
98
                found = true;
 
99
            }
 
100
        }
 
101
 
 
102
        this.dataFacade.deleteContact(contact);
 
103
        this.dataFacade.updateUser(user);
 
104
    }
 
105
 
 
106
    /**
 
107
     * Delete a user.
 
108
     * 
 
109
     * @param user
 
110
     *            the user to be deleted.
 
111
     */
 
112
    public void deleteUser(User user) {
 
113
        // Delete the user and its mailboxes.
 
114
        for (final Contact contact : user.getContacts()) {
 
115
            this.dataFacade.deleteContact(contact);
 
116
        }
 
117
        this.dataFacade.deleteUser(user);
 
118
    }
 
119
 
 
120
    /**
 
121
     * Get a contact by its id.
 
122
     * 
 
123
     * @param contactId
 
124
     *            the contact's id.
 
125
     * @return a Contact object or null if no contact has been found.
 
126
     */
 
127
    public Contact getContactById(String contactId) {
 
128
        return this.dataFacade.getContactById(contactId);
 
129
    }
 
130
 
 
131
    /**
 
132
     * Get a user by its id.
 
133
     * 
 
134
     * @param userId
 
135
     *            the user's id.
 
136
     * @return a User object or null if no user has been found.
 
137
     */
 
138
    public User getUserById(String userId) {
 
139
        return this.dataFacade.getUserById(userId);
 
140
    }
 
141
 
 
142
    /**
 
143
     * Get the list of all users.
 
144
     * 
 
145
     * @return the list of all users.
 
146
     */
 
147
    public List<User> getUsers() {
 
148
        return this.dataFacade.getUsers();
 
149
    }
 
150
 
 
151
    /**
 
152
     * Update a contact.
 
153
     * 
 
154
     * @param user
 
155
     *            the parent user.
 
156
     * @param contact
 
157
     *            the contact to be update.
 
158
     */
 
159
    public void updateContact(User user, Contact contact) {
 
160
        this.dataFacade.updateContact(contact);
 
161
    }
 
162
 
 
163
    /**
 
164
     * Update a user.
 
165
     * 
 
166
     * @param user
 
167
     *            the user to be upated.
 
168
     */
 
169
    public void updateUser(User user) {
 
170
        this.dataFacade.updateUser(user);
 
171
    }
 
172
 
 
173
}