/*
 * oxCore is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.persist.model.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.gluu.persist.annotation.AttributeName;
import org.gluu.persist.annotation.AttributesList;
import org.gluu.persist.annotation.CustomObjectClass;
import org.gluu.persist.annotation.DN;
import org.gluu.persist.annotation.DataEntry;
import org.gluu.persist.annotation.ObjectClass;
import org.gluu.util.StringHelper;

/**
 * @author Javier Rojas Blum Date: 11.25.2011
 */
@DataEntry
@ObjectClass
public class SimpleUser implements Serializable {

    private static final long serialVersionUID = -1634191420188575733L;

    @DN
    private String dn;

    @AttributeName(name = "uid", consistency = true)
    private String userId;

    @AttributeName
    private Date updatedAt;

    @AttributeName(name = "oxCreationTimestamp")
    private Date createdAt;

    @AttributeName(name = "oxAuthPersistentJWT")
    private String[] oxAuthPersistentJwt;

    @AttributesList(name = "name", value = "values", multiValued = "multiValued", sortByName = true)
    protected List<CustomAttribute> customAttributes = new ArrayList<CustomAttribute>();

    @CustomObjectClass
    private String[] customObjectClasses;

    public String getDn() {
        return dn;
    }

    public void setDn(String dn) {
        this.dn = dn;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String[] getOxAuthPersistentJwt() {
        return oxAuthPersistentJwt;
    }

    public void setOxAuthPersistentJwt(String[] oxAuthPersistentJwt) {
        this.oxAuthPersistentJwt = oxAuthPersistentJwt;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public List<CustomAttribute> getCustomAttributes() {
        return customAttributes;
    }

    public void setCustomAttributes(List<CustomAttribute> customAttributes) {
        this.customAttributes = customAttributes;
    }

    public String getAttribute(String ldapAttribute) {
        String attribute = null;
        if (ldapAttribute != null && !ldapAttribute.isEmpty()) {
            for (CustomAttribute customAttribute : customAttributes) {
                if (customAttribute.getName().equals(ldapAttribute)) {
                    attribute = customAttribute.getValue();
                    break;
                }
            }
        }

        return attribute;
    }

    public List<String> getAttributeValues(String ldapAttribute) {
        List<String> values = null;
        if (ldapAttribute != null && !ldapAttribute.isEmpty()) {
            for (CustomAttribute customAttribute : customAttributes) {
                if (StringHelper.equalsIgnoreCase(customAttribute.getName(), ldapAttribute)) {
                    values = customAttribute.getValues();
                    break;
                }
            }
        }

        return values;
    }

    public String[] getCustomObjectClasses() {
        return customObjectClasses;
    }

    public void setCustomObjectClasses(String[] customObjectClasses) {
        this.customObjectClasses = customObjectClasses;
    }

}

@@ -1256,32 +1256,38 @@
 cted List%3CCustom
+Object
 Attribute%3E custo
@@ -1312,32 +1312,38 @@
 ArrayList%3CCustom
+Object
 Attribute%3E();%0A%0A 
@@ -2217,32 +2217,38 @@
 blic List%3CCustom
+Object
 Attribute%3E getCu
@@ -2349,24 +2349,30 @@
 (List%3CCustom
+Object
 Attribute%3E c
@@ -2481,37 +2481,33 @@
 bute(String 
-ldapA
+a
 ttribute
 ) %7B%0A        
@@ -2486,32 +2486,36 @@
 String attribute
+Name
 ) %7B%0A        Stri
@@ -2510,22 +2510,211 @@
         
-String
+Object objectAttribute = getObjectAttribute(attributeName);%0A%0A        return StringHelper.toString(objectAttribute);%0A    %7D%0A%0A    public Object getObjectAttribute(String attributeName) %7B%0A    %09Object
  attribu
@@ -2732,37 +2732,33 @@
         if (
-ldapA
+a
 ttribute
  != null && 
@@ -2737,32 +2737,36 @@
    if (attribute
+Name
  != null && !lda
@@ -2758,37 +2758,33 @@
 != null && !
-ldapA
+a
 ttribute
 .isEmpty()) 
@@ -2763,32 +2763,36 @@
 ll && !attribute
+Name
 .isEmpty()) %7B%0A  
@@ -2804,32 +2804,38 @@
      for (Custom
+Object
 Attribute custom
@@ -2919,29 +2919,25 @@
 .equals(
-ldapA
+a
 ttribute
 )) %7B%0A   
@@ -2920,32 +2920,36 @@
 equals(attribute
+Name
 )) %7B%0A           
@@ -3154,44 +3154,461 @@
 ing 
-ldapAttribute) %7B%0A        List%3CString
+attributeName) %7B%0A    %09List%3CObject%3E objectValues = getAttributeObjectValues(attributeName);%0A    %09if (objectValues == null) %7B%0A    %09%09return null;%0A    %09%7D%0A%0A    %09List%3CString%3E values = new ArrayList%3CString%3E(objectValues.size());%0A    %09for (Object objectValue : objectValues) %7B%0A    %09%09values.add(StringHelper.toString(objectValue));%0A    %09%7D%0A%0A        return values;%0A    %7D%0A%0A    public List%3CObject%3E getAttributeObjectValues(String attributeName) %7B%0A        List%3CObject
 %3E va
@@ -3632,29 +3632,25 @@
     if (
-ldapA
+a
 ttribute
  != null
@@ -3641,16 +3641,20 @@
 ttribute
+Name
  != null
@@ -3658,29 +3658,25 @@
 ull && !
-ldapA
+a
 ttribute
 .isEmpty
@@ -3667,16 +3667,20 @@
 ttribute
+Name
 .isEmpty
@@ -3704,24 +3704,30 @@
  for (Custom
+Object
 Attribute cu
@@ -3843,21 +3843,17 @@
 (), 
-ldapA
+a
 ttribute
 )) %7B
@@ -3848,16 +3848,20 @@
 ttribute
+Name
 )) %7B%0A   

Use CustomObjectAttribute instead of CustomAttribute in user services to use JSON data types #1445