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

package org.xdi.oxauth.service;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.*;
import org.jboss.seam.log.Log;
import org.xdi.model.GluuAttribute;
import org.xdi.oxauth.model.config.StaticConf;
import org.xdi.service.CacheService;

import java.util.List;

/**
 * @author Javier Rojas Blum
 * @version 0.9 March 27, 2015
 */
@Scope(ScopeType.STATELESS)
@Name("attributeService")
@AutoCreate
public class AttributeService extends org.xdi.service.AttributeService {

    private static final String CACHE_ATTRIBUTE = "AttributeCache";

    @Logger
    private Log log;

    @In
    private CacheService cacheService;

    @In
    private StaticConf staticConfiguration;
    /**
     * Get AttributeService instance
     *
     * @return AttributeService instance
     */
    public static AttributeService instance() {
        return (AttributeService) Component.getInstance(AttributeService.class);
    }

    /**
     * returns GluuAttribute by Dn
     *
     * @return GluuAttribute
     */
    public GluuAttribute getAttributeByDn(String dn) {
        GluuAttribute gluuAttribute = (GluuAttribute) cacheService.get(CACHE_ATTRIBUTE, dn);

        if (gluuAttribute == null) {
            gluuAttribute = ldapEntryManager.find(GluuAttribute.class, dn);
            cacheService.put(CACHE_ATTRIBUTE, dn, gluuAttribute);
        } else {
            log.trace("Get attribute from cache by Dn '{0}'", dn);
        }

        return gluuAttribute;
    }

    public GluuAttribute getByLdapName(String name) {
        List<GluuAttribute> gluuAttributes = getAttributesByAttribute("gluuAttributeName", name, staticConfiguration.getBaseDn().getAttributes());
        if (gluuAttributes.size() > 0) {
            for (GluuAttribute gluuAttribute : gluuAttributes) {
                if (gluuAttribute.getName() != null && gluuAttribute.getName().equals(name)) {
                    return gluuAttribute;
                }
            }
        }

        return null;
    }

    public GluuAttribute getByClaimName(String name) {
        List<GluuAttribute> gluuAttributes = getAttributesByAttribute("oxAuthClaimName", name, staticConfiguration.getBaseDn().getAttributes());
        if (gluuAttributes.size() > 0) {
            for (GluuAttribute gluuAttribute : gluuAttributes) {
                if (gluuAttribute.getOxAuthClaimName() != null && gluuAttribute.getOxAuthClaimName().equals(name)) {
                    return gluuAttribute;
                }
            }
        }

        return null;
    }

    public List<GluuAttribute> getAllAttributes() {
        return getAllAttributes(staticConfiguration.getBaseDn().getAttributes());
    }
}
@@ -168,24 +168,48 @@
 h.service;%0A%0A
+import java.util.List;%0A%0A
 import org.j
@@ -296,17 +296,187 @@
 tations.
-*
+AutoCreate;%0Aimport org.jboss.seam.annotations.In;%0Aimport org.jboss.seam.annotations.Logger;%0Aimport org.jboss.seam.annotations.Name;%0Aimport org.jboss.seam.annotations.Scope
 ;%0Aimport
@@ -620,38 +620,48 @@
 ervice;%0A
-%0A
 import 
-java.util.List
+org.xdi.util.StringHelper
 ;%0A%0A/**%0A 
@@ -3041,9 +3041,258 @@
 ;%0A    %7D%0A
+%0A%09public String getDnForAttribute(String inum) %7B%0A%09%09String attributesDn = staticConfiguration.getBaseDn().getAttributes();%0A%09%09if (StringHelper.isEmpty(inum)) %7B%0A%09%09%09return attributesDn;%0A%09%09%7D%0A%0A%09%09return String.format(%22inum=%25s,%25s%22, inum, attributesDn);%0A%09%7D%0A%0A
 %7D

Implement missing methodin AttributeService