yuxc
2024-03-12 aca35d5ae9940a77ffe3daea2eeeb48a1cd124f3
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package com.vci.server.framework.systemConfig.stafforgmanage.passwordStrategy;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.hibernate.HibernateException;
import org.hibernate.type.Type;
 
import com.vci.server.base.persistence.dao.BaseService;
import com.vci.server.base.persistence.dao.HibernateCallback;
import com.vci.server.base.persistence.dao.HibernateCallbackExt;
import com.vci.server.base.persistence.dao.HibernateTemplate;
import com.vci.server.base.persistence.dao.JDBCCallback;
import com.vci.server.base.persistence.dao.JDBCRunType;
import com.vci.server.base.persistence.dao.JDBCTemplate;
 
public class PasswordStrategyService extends BaseService{
 
    public boolean savePasswordStrategy(final PasswordStrategy entity ) {
        return (Boolean)new HibernateTemplate().runExt(new HibernateCallbackExt() {
            public Object execute(Connection conn) throws HibernateException,
            SQLException{
                PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl();
                String sql = "from PasswordStrategy ";
                boolean isDefault = entity.getIsDefault();
                List<PasswordStrategy> list = impl.findEntites(sql, new String[]{});
                if(!isDefault) {
                    boolean isDef = false;
                    for(PasswordStrategy obj : list) {
                        if(obj.getIsDefault()) {
                            isDef = true;
                            break;
                        }
                    }
                    entity.setUserEntity(userEntity);
                    if(isDef) {
                        impl.saveOrUpdate(entity);
                    }else {
                        entity.setIsDefault(true);
                        impl.saveOrUpdate(entity);
                    }
                    
                }else {
                    for(PasswordStrategy obj : list) {
                        if(obj.getIsDefault()) {
                            String hsql = "update PasswordStrategy p set p.isDefault = 0 where p.id in ?";
                            impl.deleteQueryObject(hsql, new String[]{obj.getId()}, userEntity);
                        }
                    }
                    impl.saveOrUpdate(entity);
                }
                saveCombinationPasswordstrategy(conn,entity);
                
                return true;
            }
        });
    }
    
    public boolean updatePasswordStrategy(final PasswordStrategy entity) {
        return (Boolean)new HibernateTemplate().runExt(new HibernateCallbackExt() {
            public Object execute(Connection conn) throws HibernateException,
            SQLException{
                PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl();
                String sql = "from PasswordStrategy ";
                PasswordStrategy info = impl.getById(entity.getId());
                boolean isDefault = entity.getIsDefault();
                List<PasswordStrategy> list = impl.findEntites(sql, new String[]{});
                if(!isDefault) {
                    boolean isDef = false;
                    for(PasswordStrategy obj : list) {
                        if(obj.getIsDefault() && !(obj.getId().equals(entity.getId()))) {
                            isDef = true;
                            break;
                        }
                    }
                    if(isDef) {
                    }else {
                        info.setIsDefault(true);
                    }
                    
                }else {
                    for(PasswordStrategy obj : list) {
                        if(obj.getIsDefault() && !obj.getId().equals(entity.getId())) {
                            String hsql = "update PasswordStrategy p set p.isDefault = 0 where p.id in ?";
                            impl.deleteQueryObject(hsql, new String[]{obj.getId()}, userEntity);
                        }
                    }
                    info.setIsDefault(true);
                }
                info.setId(entity.getId());
                info.setName(entity.getName());
                info.setPasswordLen(entity.getPasswordLen());
                info.setPasswordMaxLen(entity.getPasswordMaxLen());
                info.setCharTypes(entity.getCharTypes());
                info.setRequiredType(entity.getRequiredType());
                info.setOverdueDay(entity.getOverdueDay());
                info.setRemideDay(entity.getRemideDay());
                info.setLockTime(entity.getLockTime());
                info.setRetryTime(entity.getRetryTime());
                info.setDesc(entity.getDesc());
                info.setUserEntity(userEntity);
                impl.saveOrUpdate(info);
                
                //saveCombinationPasswordstrategy(conn,entity,combinationIds);
                return true;
            }
        });
    }
    /**
     * 创建密码策略和密码组合方式的关系
     * <p>Description: </p>
     * 
     * @author wangxl
     * @time 2013-1-4
     * @param conn
     * @param entity
     * @param combinationIds
     */
    private void saveCombinationPasswordstrategy(Connection conn , PasswordStrategy entity){
        String  sql1 = "delete PLCOMBINATIONPASSWORDSTRATEGY ud where ud.PLPASSWORDSTRATEGYUID = ?";
        PreparedStatement pstmt = null;
        PreparedStatement pstmt1 = null;
        try {
            pstmt = conn.prepareStatement(sql1);
            pstmt.setString(1, entity.getId());
            pstmt.execute();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            if (pstmt != null){
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
//        for (String combinationId : combinationIds){
//            try {
//                String hql = "insert into PLCOMBINATIONPASSWORDSTRATEGY values (? , ?)";
//                pstmt1 = conn.prepareStatement(hql);
//                pstmt1.setString(1, entity.getId());
//                pstmt1.setString(2, combinationId);
                
//                pstmt1.execute();
//            }catch (SQLException e1) {
//                e1.printStackTrace();
//            } finally {
//                if (pstmt1 != null){
//                    try {
//                        pstmt1.close();
//                    } catch (SQLException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }
    }
    
    public PasswordStrategy getPasswordStrBysecrtId(final String secretId) {
        return (PasswordStrategy)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl();
                String hql = "select {u.*} from PPASSWORDSTRATEGY u where u.puid in(select o.PPWSTATEGYID from PSECRETSTATEGY o where o.PSECRETID=?) order by u.pname ";
                Object[] values = new Object[1];
                values[0] = secretId;
                List<PasswordStrategy> list = impl.findEntites(hql, values, "u", PasswordStrategy.class);
                if(list.size() > 0) {
                    return list.get(0);
                }
                return new PasswordStrategy();
            }
        });
    }
    public PasswordStrategy getPasswordObjByName(final String name) {
        return (PasswordStrategy)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl();
                String hql = "select {u.*} from PLPASSWORDSTRATEGY u where u.pname=?";
                Object[] values = new Object[1];
                values[0] = name;
                List<PasswordStrategy> list = impl.findEntites(hql, values, "u", PasswordStrategy.class);
                if(list.size() > 0) {
                    return list.get(0);
                }else {
                    return null;
                }
            }
        });
    }
    public PasswordStrategy getPasswordObjByUserId(final String userId) {
        return (PasswordStrategy)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl();
                String hql = "select {u.*} from PLPASSWORDSTRATEGY u where u.PLUID in(select o.PLPASSWORDSTRATEGYUID from PLUSERPASSWORDSTRATEGY o where o.pluseruid = ?) order by u.PNAME ";
                Object[] values = new Object[1];
                values[0] = userId;
                List<PasswordStrategy> list = impl.findEntites(hql, values, "u", PasswordStrategy.class);
                if(list.size() > 0) {
                    return list.get(0);
                }else {
                    String sql = "from PasswordStrategy u where u.isDefault =1";
                    return impl.findEntity(sql);
                }
            }
        });
    }
    public List getPasswordObjById(final String id) {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl();
                String hsql = " from PasswordStrategy u where u.id = '"+id+"'";
                return impl.findEntities(hsql);
            }
        });
    }
    @SuppressWarnings("unchecked")
    public List<PasswordStrategy> getPasswordStrategyList() {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl();
                String sql = "from PasswordStrategy p order by p.name";
                return impl.findEntities(sql);
                
            }
        });
    }
    
    @SuppressWarnings("unchecked")
    public List<PasswordStrategy> getPasswordStrategyList(final int pageNo,final int pageSize) {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl();
                
                int V_PAGE_ROWNUM_START = (pageNo - 1) * pageSize;
                int V_PAGE_ROWNUM_END = (pageNo ) * pageSize;
                
                StringBuffer sb = new StringBuffer();
                sb.append(" SELECT * ");
                sb.append("   FROM (SELECT ROW_.*, ROWNUM RN ");
                sb.append("      FROM (SELECT * from PLPASSWORDSTRATEGY R ");
                sb.append(" ORDER BY R.PNAME )");
                sb.append("  ROW_)");
                sb.append("   WHERE RN <= ");
                sb.append( V_PAGE_ROWNUM_END);
                sb.append(" AND RN > ");
                sb.append(V_PAGE_ROWNUM_START);
                
                Object[] values = new Object[0];
                List<PasswordStrategy> list = impl.findEntites(sb.toString(), values, "R", PasswordStrategy.class);
                return list;
                
            }
        });
    }
    
    /***查询密码策略总数*/
    public int getPasswordStrategyTotal() {
        int res = 0;
        String sql = "SELECT COUNT(*) FROM PLPASSWORDSTRATEGY ";
        res = (Integer)new JDBCTemplate().run(new JDBCCallback(
                new HibernateTemplate().getSessionConnection(), 
                sql, JDBCRunType.SQL, 0, false, false,new Object[]{}) {
            @Override
            public Object execute(ResultSet rst) throws SQLException {
                int ress = 0;
                while(rst.next()){
                    ress = rst.getInt(1);
                    
                }
                return ress;
            }
        });
        return res;
    }
    
    public boolean deletePasswordStrategyById(final String id) {
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl();
                PasswordStrategy entity = impl.getById(id);
                entity.setUserEntity(userEntity);
                impl.delete(entity);
                return true;
            }
        });
    }
    
    public int checkPasswordStrategyIsquotedCount(final String id ){
        return (Integer)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                int count = 0;
                PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl();
                String hsql = "select count(*) from PLUSERPASSWORDSTRATEGY ps where ps.PLPASSWORDSTRATEGYUID = '"+id+"'";
                List list = new ArrayList();
                Object[] values = new Object[0];
                Map<String,Type> map = new HashMap<String,Type>();
                list = impl.findEntitesBySQL(hsql, values,map);
                for (int i = 0 ; i < list.size(); i ++){
                    count += Integer.valueOf(String.valueOf(list.get(i))).intValue();
                }
                return count;
            }
        });
    }
    
}