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
package com.vci.server.framework.funcmng.operate;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
 
import org.hibernate.HibernateException;
 
import com.vci.common.log.ServerWithLog4j;
import com.vci.common.objects.UserEntity;
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;
 
/**
 * 操作类型service
 * 
 * @author liudi
 * 
 *         2011-6-9
 */
public class OperateService extends BaseService {
 
    public OperateService(UserEntity userEntity) {
        super(userEntity);
    }
    
    public OperateService() {
    }
 
    /**
     * 保存操作类型
     * 
     * @param operateTypeSeq
     * @return
     */
    public String saveOperate(final Operate operate)  {
        return (String) new HibernateTemplate().run(new HibernateCallback() {
            @Override
            public Object execute() throws HibernateException {
                OperateDaoImpl impl = new OperateDaoImpl();
                /**检查操作是否有重名的,如果有则返回1**/
                String hql = "from Operate where name = ? and id <> '"+operate.getId()+"'";
                List<Operate> list = impl.findEntites(hql, new Object[]{operate.getName()});
                if(list.size() > 0){
                    return "1";
                }
                /**检查操作的标识是否有重复,如果有返回2**/
                hql = "from Operate where identify = ? and id <> '"+operate.getId()+"'";
                list = impl.findEntites(hql, new Object[]{operate.getIdentify()});
                if(list.size() > 0 ){
                    return "2";
                }
                /**检查操作的顺序是否有重复,如果有返回3**/
                hql = "from Operate where sequence = ? and id <> '"+operate.getId()+"'";
                list = impl.findEntites(hql, new Object[]{operate.getSequence()});
                if(list.size() > 0 ){
                    return "3";
                }
                /**检查通过,执行保存**/
                operate.setUserEntity(userEntity);
                impl.save(operate);
                return operate.getId();
            }
        });
    }
    
    /**
     * 修改操作类型
     * 
     * @param operateTypeSeq
     * @return
     */
    public String updateOperate(final Operate operate)  {
        return (String) new HibernateTemplate().run(new HibernateCallback() {
            @Override
            public Object execute() throws HibernateException {
                OperateDaoImpl impl = new OperateDaoImpl();
                /**检查操作是否有重名的,如果有则返回1**/
                String hql = "from Operate where name = ? and id <> '"+operate.getId()+"' ";
                List<Operate> list = impl.findEntites(hql, new Object[]{operate.getName()});
                if(list.size() > 0){
                    return "1";
                }
                /**检查操作的标识是否有重复,如果有返回2**/
                hql = "from Operate where identify = ? and id <> '"+operate.getId()+"' ";
                list = impl.findEntites(hql, new Object[]{operate.getIdentify()});
                if(list.size() > 0 ){
                    return "2";
                }
                /**检查操作的顺序是否有重复,如果有返回3**/
                hql = "from Operate where sequence = ? and id <> '"+operate.getId()+"' ";
                list = impl.findEntites(hql, new Object[]{operate.getSequence()});
                if(list.size() > 0 ){
                    return "3";
                }
                
                /**检查通过,执行保存**/
                operate.setUserEntity(userEntity);
                impl.saveOrUpdate(operate);
                return operate.getId();
            }
        });
    }
    
    /**
     * 检查当前操作是否被模块引用,是否有授权信息
     * @param operateId 操作ID
     * @return 0表示无引用,1表示被模块引用,2表示已经存在授权信息
     */
    public int checkOperateIsReferenced(final String operateId){
        return (Integer) new HibernateTemplate().runExt(new HibernateCallbackExt() {
            @Override
            public Object execute(Connection connection) throws HibernateException,
                    SQLException {
                PreparedStatement pstmt = null;
                ResultSet rs = null;
                String sql = " select ploid from plfuncoperation p where p.ploperoid = '"+operateId+"'";
                pstmt = connection.prepareStatement(sql);
                rs = pstmt.executeQuery();
                if(rs.next()){
                    return 1;
                }
                //TODO 此处判断是否有权限,如果有返回2
                return 0;
            }
        });
    }
    
    /**
     * 删除操作类型
     * 
     * @param operateTypeSeq
     * @return
     */
    public boolean deleteOperate(final String id)  {
        return (Boolean) new HibernateTemplate().run(new HibernateCallback() {
            @Override
            public Object execute() throws HibernateException {
                OperateDaoImpl impl = new OperateDaoImpl();
                Operate operate = impl.getById(id);
                /**此处应该检查,该操作在权限模块是否已经被授权过,如果有,权限一并删除**/
                operate.setUserEntity(userEntity);
                impl.delete(operate);
                return true;
            }
        });
    }
 
    /**
     * 获取操作类型树
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Operate> getOperateTreeList()  {
        return (List<Operate>) new HibernateTemplate().run(new HibernateCallback() {
 
            public Object execute() throws HibernateException {
                OperateDaoImpl impl = new OperateDaoImpl();
                String hql = "from Operate order by name";
                return impl.findEntities(hql);
            }
        });
    }
    /**
     * 获取没有挂到当前模块下的其他操作
     * @param moduleId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Operate> getOtherOperateListByModule(final String moduleId){
        return (List<Operate>)new HibernateTemplate().run(new HibernateCallback() {
            
            @Override
            public Object execute() throws HibernateException {
                OperateDaoImpl impl = new OperateDaoImpl();
                StringBuffer sql = new StringBuffer();
                sql.append(" select {t.*} from ploperation t where t.ploid not in (");
                sql.append(" select ploperoid from plfuncoperation where plfuncoid = '").append(moduleId).append("'");
                sql.append(" ) order by t.plname ");
                return impl.findEntites(sql.toString(), new Object[0], "t", Operate.class);
            }
        });
    }
    
    /**
     * 根据标识获取操作
     * @param identify
     * @return
     */
    public Operate getOperateByIdentify(final String identify){
        return (Operate)new HibernateTemplate().run(new HibernateCallback() {
            
            @Override
            public Object execute() throws HibernateException {
                OperateDaoImpl impl = new OperateDaoImpl();
                String hql = "from Operate where identify = ? ";
                return impl.findEntity(hql, new Object[]{identify});
            }
        });
    }
    /**
     * <p>Description:根据名称获取操作 </p>
     * 
     * @author wangxl
     * @time 2012-5-30
     * @param name
     * @return
     */
    public Operate fetchOperateTypeByName(final String name){
        return (Operate)new HibernateTemplate().run(new HibernateCallback() {
            
            @Override
            public Object execute() throws HibernateException {
                OperateDaoImpl impl = new OperateDaoImpl();
                String hql = "from Operate where name = ? ";
                return impl.findEntity(hql, new Object[]{name});
            }
        });
    }
    public Operate getOperatetById(final String id){
        return (Operate)new HibernateTemplate().run(new HibernateCallback() {
            @Override
            public Object execute() throws HibernateException {
                OperateDaoImpl impl = new OperateDaoImpl();
                return impl.loadById(id);
            }
        });
    }
    
    /**
     * 获得所有操作类型的数量
     * add by caill start 2015.12.11
     * */
    public Integer getAllOperitionsNum(){
        
        return (Integer) new HibernateTemplate().runExt(new HibernateCallbackExt() {
            
            @Override
            public Object execute(Connection connection) throws HibernateException,
                    SQLException {            
                //String[] firstLevel=new String[2];
                int size=0;
                PreparedStatement pstmt = null;
                ResultSet rs = null;
                String sql = "select * from ploperation";
                pstmt = connection.prepareStatement(sql);
                rs = pstmt.executeQuery();
                
                //计算总的数据条数
                while(rs.next()) {            
                    size++;    
                    ServerWithLog4j.logger.info(rs.getString(1));
                }
                return size;
            }
        });
    }
    /**
     *获得所有操作类型并导出到.sql文件
     * add by caill start 2015.12.11
     * */
    public String[][] getAllOperitions(final int size){
        
        return (String[][]) new HibernateTemplate().runExt(new HibernateCallbackExt() {
            
            @Override
            public Object execute(Connection connection) throws HibernateException,
                    SQLException {            
                String[][] bb=new String[size][6];    
                
                PreparedStatement pstmt = null;
                ResultSet rs = null;
                String sql = "select * from ploperation";
                pstmt = connection.prepareStatement(sql);
                rs = pstmt.executeQuery();
                int count = 0;
                while(rs.next()) {            
                    for(int i = 0;i<6;i++){
                        if(rs.getString(i+1)==null){
                            bb[count][i] = "";
                        }else{
                            bb[count][i] = rs.getString(i+1);
                        }        
                    }
                    count++;
                }
                return bb;
            }
        });
    }
    
}