001/*
002 * Copyright 2011-2016 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018package com.unboundid.scim.data;
019
020import com.unboundid.scim.schema.AttributeDescriptor;
021import com.unboundid.scim.sdk.InvalidResourceException;
022import com.unboundid.scim.sdk.SCIMAttribute;
023import com.unboundid.scim.sdk.SCIMAttributeValue;
024
025import java.net.URI;
026import java.util.ArrayList;
027import java.util.Collection;
028import java.util.Date;
029
030/**
031 * A complex type containing metadata about the resource.
032 */
033public class Meta
034{
035  /**
036   * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
037   * to/from <code>Meta</code> instances.
038   */
039  public static final AttributeValueResolver<Meta> META_RESOLVER =
040      new AttributeValueResolver<Meta>() {
041        /**
042         * {@inheritDoc}
043         */
044        @Override
045        public Meta toInstance(final SCIMAttributeValue value) {
046          String l = value.getSubAttributeValue("location",
047              STRING_RESOLVER);
048          return new Meta(
049              value.getSubAttributeValue("created",
050                  DATE_RESOLVER),
051              value.getSubAttributeValue("lastModified",
052                  DATE_RESOLVER),
053              l == null ? null : URI.create(l),
054              value.getSubAttributeValue("version",
055                  STRING_RESOLVER));
056        }
057
058        /**
059         * {@inheritDoc}
060         */
061        @Override
062        public SCIMAttributeValue fromInstance(
063            final AttributeDescriptor attributeDescriptor,
064            final Meta value) throws InvalidResourceException {
065          Collection<SCIMAttribute> attributes =
066              new ArrayList<SCIMAttribute>(3);
067
068          if(value.created != null)
069          {
070            AttributeDescriptor subAttributeDescriptor =
071                attributeDescriptor.getSubAttribute("created");
072            attributes.add(SCIMAttribute.create(
073                subAttributeDescriptor,
074                SCIMAttributeValue.createDateValue(value.created)));
075          }
076          if(value.lastModified != null)
077          {
078            AttributeDescriptor subAttributeDescriptor =
079                attributeDescriptor.getSubAttribute("lastModified");
080            attributes.add(SCIMAttribute.create(
081                subAttributeDescriptor,
082                SCIMAttributeValue.createDateValue(value.lastModified)));
083          }
084          if(value.location != null)
085          {
086            AttributeDescriptor subAttributeDescriptor =
087                attributeDescriptor.getSubAttribute("location");
088            attributes.add(SCIMAttribute.create(
089                subAttributeDescriptor,
090                SCIMAttributeValue.createStringValue(
091                    value.location.toString())));
092          }
093          if(value.version != null)
094          {
095            AttributeDescriptor subAttributeDescriptor =
096                attributeDescriptor.getSubAttribute("version");
097            attributes.add(SCIMAttribute.create(
098                subAttributeDescriptor,
099                SCIMAttributeValue.createStringValue(value.version)));
100          }
101
102          return SCIMAttributeValue.createComplexValue(attributes);
103        }
104      };
105
106  private Date created;
107  private Date lastModified;
108  private URI location;
109  private String version;
110
111  /**
112   * Create an instance of the SCIM meta attribute.
113   *
114   * @param created         The time the Resource was added to the
115   *                        Service Provider.
116   * @param lastModified    The most recent time the details of a Resource
117   *                        were updated at the Service Provider.
118   * @param location        The URI of the Resource.
119   * @param version         The version of the Resource.
120   */
121  public Meta(final Date created, final Date lastModified, final URI location,
122              final String version) {
123    this.created = created;
124    this.lastModified = lastModified;
125    this.location = location;
126    this.version = version;
127  }
128
129  /**
130   * Retrieves the time the Resource was added to the Service Provider.
131   *
132   * @return The time the Resource was added to the Service Provider.
133   */
134  public Date getCreated() {
135    return created;
136  }
137
138  /**
139   * Sets the time the Resource was added to the Service Provider.
140   *
141   * @param created The time the Resource was added to the Service Provider.
142   */
143  public void setCreated(final Date created) {
144    this.created = created;
145  }
146
147  /**
148   * Retrieves the most recent time the details of a Resource were updated at
149   * the Service Provider.
150   *
151   * @return The most recent time the details of a Resource were updated at
152   * the Service Provider.
153   */
154  public Date getLastModified() {
155    return lastModified;
156  }
157
158  /**
159   * Sets the most recent time the details of a Resource were updated at
160   * the Service Provider.
161   *
162   * @param lastModified The most recent time the details of a Resource were
163   * updated at the Service Provider.
164   */
165  public void setLastModified(final Date lastModified) {
166    this.lastModified = lastModified;
167  }
168
169  /**
170   * Retrieves the URI of the Resource.
171   *
172   * @return The URI of the Resource.
173   */
174  public URI getLocation() {
175    return location;
176  }
177
178  /**
179   * Sets the URI of the Resource.
180   *
181   * @param location The URI of the Resource.
182   */
183  public void setLocation(final URI location) {
184    this.location = location;
185  }
186
187  /**
188   * Retrieves the version of the Resource.
189   *
190   * @return The version of the Resource.
191   */
192  public String getVersion() {
193    return version;
194  }
195
196  /**
197   * Sets the version of the Resource being returned.
198   *
199   * @param version The version of the Resource being returned.
200   */
201  public void setVersion(final String version) {
202    this.version = version;
203  }
204
205  /**
206   * {@inheritDoc}
207   */
208  @Override
209  public boolean equals(final Object o) {
210    if (this == o) {
211      return true;
212    }
213    if (o == null || getClass() != o.getClass()) {
214      return false;
215    }
216
217    Meta meta = (Meta) o;
218
219    if (created != null ? !created.equals(meta.created) :
220        meta.created != null) {
221      return false;
222    }
223    if (lastModified != null ? !lastModified.equals(meta.lastModified) :
224        meta.lastModified != null) {
225      return false;
226    }
227    if (location != null ? !location.equals(meta.location) :
228        meta.location != null) {
229      return false;
230    }
231    if (version != null ? !version.equals(meta.version) :
232        meta.version != null) {
233      return false;
234    }
235
236    return true;
237  }
238
239  /**
240   * {@inheritDoc}
241   */
242  @Override
243  public int hashCode() {
244    int result = created != null ? created.hashCode() : 0;
245    result = 31 * result + (lastModified != null ? lastModified.hashCode() : 0);
246    result = 31 * result + (location != null ? location.hashCode() : 0);
247    result = 31 * result + (version != null ? version.hashCode() : 0);
248    return result;
249  }
250
251  /**
252   * {@inheritDoc}
253   */
254  @Override
255  public String toString() {
256    return "Meta{" +
257        "created=" + created +
258        ", lastModified=" + lastModified +
259        ", location=" + location +
260        ", version='" + version + '\'' +
261        '}';
262  }
263}