View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.adminui.usersettings;
23  
24  import org.opencastproject.util.Jsons;
25  import org.opencastproject.util.Jsons.Obj;
26  import org.opencastproject.util.Jsons.Val;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * A class used to store all of the key-value pairs that denotes the user's settings.
36   */
37  public class UserSettings {
38    /** The total number of signatures for this user in the database */
39    private int total = 0;
40    /** The maximum number of user settings and signatures to return. */
41    private int limit = 0;
42    /** The page offset into the results. */
43    private int offset = 0;
44    /** The user settings attached to this user */
45    private Map<String, Collection<UserSetting>> userSettings = new HashMap<String, Collection<UserSetting>>();
46  
47    /**
48     * Default constructor.
49     */
50    public UserSettings() {
51  
52    }
53  
54    public UserSettings(Map<String, Collection<UserSetting>> userSettings) {
55      this.userSettings = userSettings;
56    }
57  
58    public Map<String, Collection<UserSetting>> getUserSettingsMap() {
59      return userSettings;
60    }
61  
62    public Collection<UserSetting> getUserSettingsCollection() {
63      Collection<UserSetting> userSettingCollection = new ArrayList<UserSetting>();
64      for (Collection<UserSetting> collection : userSettings.values()) {
65          userSettingCollection.addAll(collection);
66      }
67      return userSettingCollection;
68    }
69  
70    public void setUserSettings(Map<String, Collection<UserSetting>> userSettings) {
71      this.userSettings = userSettings;
72    }
73  
74    public void addUserSetting(UserSetting userSetting) {
75      Collection<UserSetting> collection = userSettings.get(userSetting.getKey());
76      if (collection == null) {
77          collection = new ArrayList<UserSetting>();
78      }
79      collection.add(userSetting);
80      userSettings.put(userSetting.getKey(), collection);
81    }
82  
83    public int getTotal() {
84      return total;
85    }
86  
87    public void setTotal(int total) {
88      this.total = total;
89    }
90  
91    public int getLimit() {
92      return limit;
93    }
94  
95    public void setLimit(int limit) {
96      this.limit = limit;
97    }
98  
99    public int getOffset() {
100     return offset;
101   }
102 
103   public void setOffset(int offset) {
104     this.offset = offset;
105   }
106 
107   /**
108    * @return The JSON representation of these user settings.
109    */
110   public Obj toJson() {
111     List<Val> settingsArr = new ArrayList<Val>();
112     for (UserSetting userSetting : getUserSettingsCollection()) {
113       settingsArr.add(userSetting.toJson());
114     }
115     return Jsons.obj(Jsons.p("offset", offset), Jsons.p("limit", limit), Jsons.p("total", total),Jsons.p("results", Jsons.arr(settingsArr)));
116   }
117 }