ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.vci.server.framework.right.typeRight;
 
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
 
import com.vci.common.exception.VciExceptionTool;
import com.vci.common.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
import com.vci.corba.framework.data.GrandValue;
import com.vci.server.base.delegate.BaseDelegate;
import com.vci.server.base.exception.ExceptionLocalHandler;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
 
public class TypeRightDelegate extends BaseDelegate {
 
 
    public boolean saveGrand(GrandValue[] values) throws VCIError {
        Session session = HibernateSessionFactory.getSession();
        checkValidation(session, "pl_typeright", "rulename", values[0]);
        
        for (GrandValue o : values) {
            StringBuffer sql = new StringBuffer();
            sql.append("insert into PL_TYPERIGHT(ID,USERS,USERGROUPS,USERROLES,IDENTIFIER,EXPRESSIONTOSQL,ISGRANT,RULETEXT,SENIORRULETEXT,RULENAME,RULETYPE,LEXPRESSIONTOSQL,LRULETEXT,LSENIORRULETEXT) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            SQLQuery query = session.createSQLQuery(sql.toString());
            query.setString(0, ObjectUtility.getNewObjectID36());
            query.setString(1, o.users);
            query.setString(2, o.userGroups);
            query.setString(3, o.roles);
            query.setString(4, o.identifier);
            query.setString(5, o.expToSQL);
            query.setCharacter(6, (char)o.isGrand);
            query.setString(7, o.ruleText);
            query.setString(8, o.seniorRuleText);
            query.setString(9, o.ruleName);
            query.setString(10, o.ruleType);
            query.setString(11, o.lexpToSQL);
            query.setString(12, o.lruleText);
            query.setString(13, o.lseniorRuleText);
            query.executeUpdate();
        }
        return true;
    }
 
 
    public GrandValue[] queryGrand(String identifier) throws VCIError {
        try {
            Session session = HibernateSessionFactory.getSession();
            StringBuffer sql = new StringBuffer();
            sql.append("select ID,USERS,USERGROUPS,USERROLES,IDENTIFIER,EXPRESSIONTOSQL,ISGRANT,RULETEXT,SENIORRULETEXT,RULENAME,RULETYPE ,LEXPRESSIONTOSQL,LRULETEXT,LSENIORRULETEXT from PL_TYPERIGHT where IDENTIFIER like '"
                    + identifier + "$_%' escape '$'");
            SQLQuery query = session.createSQLQuery(sql.toString());
            // query.setString(0, identifier );
            List<GrandValue> dataSets = new ArrayList<GrandValue>();
            List<Object[]> rs = query.list();
            for (Object[] o : rs) {
                GrandValue value = new GrandValue();
                value.ID = (String) o[0] == null ? "" : (String) o[0];
                value.identifier = (String) o[4] == null ? "" : (String) o[4];
                value.isGrand = (byte)((Character) o[6] == null ? '0' : (Character) o[6]);
                value.roles = (String) o[3] == null ? "" : (String) o[3];
                value.users = (String) o[1] == null ? "" : (String) o[1];
                value.userGroups = (String) o[2] == null ? "" : (String) o[2];
                value.ruleText = (String) o[7] == null ? "" : (String) o[7];
                value.seniorRuleText = (String) o[8] == null ? ""
                        : (String) o[8];
                // }
                value.expToSQL = (String) o[5] == null ? "" : (String) o[5];
                value.ruleName = (String) o[9] == null ? "" : (String) o[9];
                value.ruleType = (String) o[10] == null ? "" : (String) o[10];
                value.lexpToSQL = (String) o[11] == null ? "" : (String) o[11];
                value.lruleText = (String) o[12] == null ? "" : (String) o[12];
                value.lseniorRuleText = (String) o[13] == null ? ""
                        : (String) o[13];
                dataSets.add(value);
            }
 
            return dataSets.toArray(new GrandValue[dataSets.size()]);
        } catch (Throwable e) {
            throw getLocalVciError("grandPermission0001", e);
        }
    }
 
    public boolean deleteGrand(String ruleName) throws VCIError {
        Session session = HibernateSessionFactory.getSession();
        StringBuffer sql = new StringBuffer();
        sql.append("Delete from PL_TYPERIGHT where RULENAME=?");
        SQLQuery query = session.createSQLQuery(sql.toString());
        query.setString(0, ruleName);
        query.executeUpdate();
        return true;
    }
 
    public boolean deleteTypeRuleGrand(String identifier, String ruleName) throws VCIError {
        Session session = HibernateSessionFactory.getSession();
        StringBuffer sql = new StringBuffer();
        sql.append("Delete from PL_TYPERIGHT where RULENAME=? and IDENTIFIER like '"
                + identifier + "$_%' escape '$'");
        SQLQuery query = session.createSQLQuery(sql.toString());
        query.setString(0, ruleName);
        query.executeUpdate();
        return true;
    }
 
 
    private VCIError getLocalVciError(String key, Throwable e) {
        VCIError error = null;
        if (e == null) {
            error = new VCIError(key, new String[0]);
        } else {
            error = new VCIError(key,
                    new String[] { VciExceptionTool.getExceptionStr(e), VciExceptionTool.getExceptionDetail(e) });
        }
        VCIError rsError = ExceptionLocalHandler.getInstance().getLocalString(error, "Cache");
        return rsError;
    }
 
    private void checkValidation(Session session, String Table,
            String TableCounmn, GrandValue Value) throws VCIError {
        StringBuffer sql = new StringBuffer();
        sql.append("select count(*) from ")
                .append(Table)
                .append(" Where ")
                .append(TableCounmn)
                .append("='")
                .append(Value.ruleName)
                .append("'")
                .append(" and IDENTIFIER like '" + getType(Value.identifier)
                        + "$_%' escape '$'");
        SQLQuery query = session.createSQLQuery(sql.toString());
        List<?> objects = query.list();
        Object count = objects.get(0);
        if (Integer.valueOf(count.toString()) != 0) {
            throw new VCIError("grandRight_0001", "规则名称重复,请重新填写!".split(","));
        }
 
    }
 
    private String getType(String identifier) {
        if (identifier != null && !identifier.equals("")) {
            return identifier.substring(0, identifier.indexOf("_"));
        }
        return "";
    }
}