egovframework.com.ext.ldapumt.service.impl.DeptManageLdapDAO.java Source code

Java tutorial

Introduction

Here is the source code for egovframework.com.ext.ldapumt.service.impl.DeptManageLdapDAO.java

Source

/*
 * eGovFrame LDAP??
 * Copyright The eGovFrame Open Community (http://open.egovframe.go.kr)).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author (??K3)
 */
package egovframework.com.ext.ldapumt.service.impl;

import static org.springframework.ldap.query.LdapQueryBuilder.query;

import java.util.List;
import java.util.Map;

import egovframework.com.ext.ldapumt.service.UcorgVO;

import javax.annotation.Resource;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

import org.apache.commons.beanutils.BeanMap;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.query.ContainerCriteria;
import org.springframework.stereotype.Repository;

/**
*
*   ?  DAO? 
* @author 
* @since 2014.10.12
* @version 1.0
* @see
*
* <pre>
* << ?(Modification Information) >>
*
*   ?      ?           
*  -------    --------    ---------------------------
*   2014.10.12             ?
*
* </pre>
*/
@Repository("DeptManageLdapDAO")
public class DeptManageLdapDAO extends OrgManageLdapDAO {

    @Resource(name = "ldapTemplate")
    public LdapTemplate ldapTemplate;

    /**
     * DN?  ?? 
     * @param dn  ?? Distinguished Name
     * @return
     * @throws Exception
     */
    public List<Object> selectDeptManageSubList(String dn) throws Exception {
        List<Object> ucorgList = null;
        String filter = "objectclass=ucorg2";

        try {
            ucorgList = ldapTemplate.search(dn, filter, SearchControls.ONELEVEL_SCOPE,
                    new ObjectMapper<UcorgVO>(UcorgVO.class));
        } catch (NameNotFoundException e) {
        }

        return ucorgList;
    }

    /**
     * ouCode    
     * @param ouCode
     * @return
     * @throws Exception
     */
    public List<Object> selectDeptManageSubListByOuCode(String ouCode) throws Exception {
        ContainerCriteria criteria = query().where("objectclass").is("ucorg2").and("parentoucode").is(ouCode);

        List<Object> list = ldapTemplate.search(criteria, new ObjectMapper<UcorgVO>(UcorgVO.class));

        return list;
    }

    /**
     * ?? ? ? .
     * @param vo  Vo
     * @return deptManageVO  Vo
     * @param bannerVO
     */
    public UcorgVO selectDeptManage(UcorgVO vo) throws Exception {
        final ContainerCriteria criteria = query().where("objectclass").is("ucorg2");

        @SuppressWarnings("unchecked")
        Map<String, Object> introspected = new BeanMap(vo);

        for (String key : introspected.keySet()) {
            if (key.equals("dn") || key.equals("class") || introspected.get(key) == null
                    || introspected.get(key).equals(""))
                continue;

            ContainerCriteria c = query().where(key).is(String.valueOf(introspected.get(key)));
            criteria.and(c);
        }

        List<Object> list = null;
        try {
            list = ldapTemplate.search(criteria, new ObjectMapper<UcorgVO>(UcorgVO.class));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return (UcorgVO) list.get(0);
    }

    /**
     * ?? ? ? . 
     * @param dn
     * @return
     */
    public UcorgVO selectDeptManageByDn(String dn) {
        return (UcorgVO) selectOrgManageByDn(dn, UcorgVO.class);
    }

    /**
     *  ??  .
     * @param vo  vo
     */
    public void updateDeptManage(UcorgVO vo) throws Exception {
        updateOrg(vo);
    }

    /**
     *  .
     * @param vo  vo
     */
    public void insertDeptManage(UcorgVO vo) throws Exception {
        BasicAttribute ocattr = new BasicAttribute("objectclass");
        ocattr.add("top");
        ocattr.add("ucorg2");

        insertOrgManage(vo, ocattr);
    }

    /**
     *  ??.
     * @param oldDn ??? 
     * @param newDn ?? 
     */
    public void moveDeptManage(String oldDn, String newDn) {
        ldapTemplate.rename(oldDn, newDn);
    }

    /**
     *  .
     * @param vo  vo
     */
    public void deleteDeptManage(String dn) {
        ldapTemplate.unbind(dn, true);
    }

    /**
     *    ?.
     * @param vo  vo
     */
    public boolean hasChildren(String dn) throws NamingException {
        ContextSource contextSource = ldapTemplate.getContextSource();
        DirContext ctx = contextSource.getReadOnlyContext();

        String filter = "objectclass=*";
        SearchControls control = new SearchControls();
        control.setSearchScope(SearchControls.ONELEVEL_SCOPE);

        NamingEnumeration<SearchResult> n = ctx.search(dn, filter, control);

        if (n != null && n.hasMore()) {
            return true;
        }

        return false;
    }
}