카테고리 없음

ㄱㄹ

anythingsay 2026. 5. 14. 12:33

<BO_HOME>/java/lib/ 아래에서 → lib/ 로 복사
  cecore.jar  celib.jar  cesession.jar
  ceplugins_core.jar  corbaidl.jar  ebus405.jar



BOServer=your-cms-hostname      # 포트 없이 호스트명만
AdminUser=Administrator
AdminPassword=your_password
AdminQuery=SELECT SI_ID, SI_NAME, SI_DESCRIPTION, SI_GROUP_KIND, \
  SI_USERCOUNT, SI_CUID FROM CI_SYSTEMOBJECTS WHERE SI_KIND='UserGroup'




import com.crystaldecisions.sdk.framework.CrystalEnterprise;
import com.crystaldecisions.sdk.framework.IEnterpriseSession;
import com.crystaldecisions.sdk.framework.ISessionMgr;
import com.crystaldecisions.sdk.occa.infostore.IInfoObject;
import com.crystaldecisions.sdk.occa.infostore.IInfoObjects;
import com.crystaldecisions.sdk.occa.infostore.IInfoStore;

import java.io.*;
import java.util.Properties;

/**
* SAP BusinessObjects 4.3 SP4 - 그룹 조회 CLI 프로그램
*
* 사용법:
*   java -cp ".:lib/*" BOGroupViewer [config.properties 경로]
*   경로 생략 시 현재 디렉터리의 config.properties 사용
*/
public class BOGroupViewer {

    // ── ANSI 색상 상수 ─────────────────────────────────────────────
    private static final String RESET  = "\033[0m";
    private static final String BOLD   = "\033[1m";
    private static final String CYAN   = "\033[36m";
    private static final String GREEN  = "\033[32m";
    private static final String YELLOW = "\033[33m";
    private static final String RED    = "\033[31m";
    private static final String GRAY   = "\033[90m";
    private static final String WHITE  = "\033[97m";

    public static void main(String[] args) {

        printBanner();

        // ── 1. 설정 파일 경로 결정 ──────────────────────────────────
        String configPath = (args.length > 0) ? args[0] : "config.properties";
        info("설정 파일: " + configPath);

        // ── 2. 설정 파일 읽기 ──────────────────────────────────────
        Properties props = loadConfig(configPath);
        if (props == null) {
            error("설정 파일을 읽지 못했습니다. 프로그램을 종료합니다.");
            System.exit(1);
        }

        String boServer   = require(props, "BOServer");
        String adminUser  = require(props, "AdminUser");
        String adminPass  = require(props, "AdminPassword");
        String authType   = props.getProperty("AuthType",   "secEnterprise");
        String adminQuery = require(props, "AdminQuery");
        int    pageSize   = Integer.parseInt(props.getProperty("PageSize", "200"));

        if (boServer == null || adminUser == null || adminPass == null || adminQuery == null) {
            error("필수 항목(BOServer, AdminUser, AdminPassword, AdminQuery)을 확인하세요.");
            System.exit(1);
        }

        info("서버  : " + boServer);
        info("사용자: " + adminUser);
        info("인증  : " + authType);
        info("쿼리  : " + adminQuery);
        info("최대행: " + pageSize);
        System.out.println();

        // ── 3. BO 세션 로그인 ──────────────────────────────────────
        IEnterpriseSession session = null;
        IInfoStore          store  = null;

        try {
            step("BO 서버 로그인 중...");
            ISessionMgr mgr = CrystalEnterprise.getSessionMgr();
            session = mgr.logon(adminUser, adminPass, boServer, authType);
            ok("로그인 성공");

            // ── 4. IInfoStore 취득 ─────────────────────────────────
            store = (IInfoStore) session.getService("", "InfoStore");

            // ── 5. AdminQuery 실행 ─────────────────────────────────
            step("AdminQuery 실행 중...");
            IInfoObjects results = (IInfoObjects)
                    store.query(adminQuery + " PAGESIZE " + pageSize);
            ok("조회 완료 — " + results.size() + "건");

            // ── 6. 결과 출력 ──────────────────────────────────────
            printTable(results);

        } catch (Exception e) {
            error("오류 발생: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // ── 7. 세션 정리 ──────────────────────────────────────
            if (session != null) {
                try {
                    session.logoff();
                    info("세션 로그오프 완료");
                } catch (Exception ignored) {}
            }
        }
    }

    // ── 설정 파일 파싱 ─────────────────────────────────────────────
    private static Properties loadConfig(String path) {
        Properties props = new Properties();
        File file = new File(path);
        if (!file.exists()) {
            error("파일이 없습니다: " + path);
            return null;
        }
        try (InputStream is = new FileInputStream(file)) {
            props.load(new InputStreamReader(is, "UTF-8"));
            ok("설정 파일 로드 완료 (" + props.size() + "개 항목)");
            return props;
        } catch (IOException e) {
            error("설정 파일 읽기 실패: " + e.getMessage());
            return null;
        }
    }

    private static String require(Properties p, String key) {
        String v = p.getProperty(key, "").trim();
        if (v.isEmpty()) {
            warn("필수 키 누락: " + key);
            return null;
        }
        return v;
    }

    // ── 테이블 출력 ────────────────────────────────────────────────
    private static void printTable(IInfoObjects objs) throws Exception {

        // 컬럼 너비 (가변 아님, 고정 레이아웃)
        String fmt = "%-8s  %-30s  %-16s  %-10s  %-44s%n";

        String line = repeat("─", 116);

        System.out.println();
        System.out.println(CYAN + BOLD + "  BO 그룹 목록" + RESET);
        System.out.println(CYAN + "  " + line + RESET);
        System.out.printf("  " + BOLD + fmt + RESET,
                "SI_ID", "SI_NAME", "SI_GROUP_KIND", "USERCOUNT", "SI_CUID");
        System.out.println(CYAN + "  " + line + RESET);

        if (objs.size() == 0) {
            System.out.println(YELLOW + "  (조회 결과 없음)" + RESET);
        }

        int idx = 0;
        for (Object obj : objs) {
            IInfoObject io = (IInfoObject) obj;

            String siId    = String.valueOf(io.getID());
            String siName  = safe(io.getTitle());
            String siDesc  = safe(io.getDescription());       // 설명은 별도 행
            String siKind  = safeProperty(io, "SI_GROUP_KIND");
            String siCount = safeProperty(io, "SI_USERCOUNT");
            String siCuid  = safe(io.getCUID());

            // 짝수/홀수 행 색상 구분
            String rowColor = (idx % 2 == 0) ? WHITE : GRAY;

            System.out.printf("  " + rowColor + fmt + RESET,
                    siId, truncate(siName, 30), siKind, siCount, siCuid);

            if (!siDesc.isEmpty()) {
                System.out.printf("  " + GRAY + "%-8s  ↳ %s%n" + RESET, "", truncate(siDesc, 100));
            }
            idx++;
        }

        System.out.println(CYAN + "  " + line + RESET);
        System.out.println(GREEN + BOLD + "  총 " + objs.size() + "개 그룹" + RESET);
        System.out.println();
    }

    // ── SI_* 프로퍼티 안전 취득 ────────────────────────────────────
    private static String safeProperty(IInfoObject io, String key) {
        try {
            Object v = io.properties().getProperty(key);
            return v != null ? String.valueOf(v) : "—";
        } catch (Exception e) {
            return "—";
        }
    }

    private static String safe(String s) {
        return (s == null || s.trim().isEmpty()) ? "" : s.trim();
    }

    private static String truncate(String s, int max) {
        if (s == null) return "";
        return s.length() > max ? s.substring(0, max - 1) + "…" : s;
    }

    private static String repeat(String ch, int n) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) sb.append(ch);
        return sb.toString();
    }

    // ── 로그 출력 헬퍼 ─────────────────────────────────────────────
    private static void step(String msg) { System.out.println(CYAN  + "[*] " + msg + RESET); }
    private static void ok  (String msg) { System.out.println(GREEN + "[✓] " + msg + RESET); }
    private static void info(String msg) { System.out.println(GRAY  + "[i] " + msg + RESET); }
    private static void warn(String msg) { System.out.println(YELLOW+ "[!] " + msg + RESET); }
    private static void error(String msg){ System.out.println(RED   + "[✗] " + msg + RESET); }

    private static void printBanner() {
        System.out.println(CYAN + BOLD);
        System.out.println("  ╔══════════════════════════════════════════════╗");
        System.out.println("  ║   SAP BusinessObjects 4.3 SP4                ║");
        System.out.println("  ║   Group Viewer  —  BO Java SDK Edition       ║");
        System.out.println("  ╚══════════════════════════════════════════════╝");
        System.out.println(RESET);
    }
}