jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.bstek.bdf2.core.view.frame.main;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
 
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.access.AccessDeniedException;
 
import com.bstek.bdf2.core.CoreHibernateDao;
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.cache.ApplicationCache;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.exception.NoneLoginException;
import com.bstek.bdf2.core.model.Url;
import com.bstek.bdf2.core.security.SecurityUtils;
import com.bstek.bdf2.core.security.UserAuthentication;
import com.bstek.dorado.annotation.DataProvider;
 
/**
 * @since 2013-2-1
 * @author Jacky.gao
 */
public class MainFrame extends CoreHibernateDao implements InitializingBean {
    public static final String BEAN_ID = "bdf2.mainFrame";
    public static final String URL_FOR_NAVI_CACHE_KEY = "url_for_navi_cache_key_";
 
    private ApplicationCache applicationCache;
 
    @DataProvider
    @SuppressWarnings("unchecked")
    public Collection<Url> loadMeunUrls(String parentId) {
        IUser user = ContextHolder.getLoginUser();
        if (user == null) {
            throw new NoneLoginException("Please login first");
        }
        String companyId = user.getCompanyId();
        if (StringUtils.isNotEmpty(getFixedCompanyId())) {
            companyId = getFixedCompanyId();
        }
        List<Url> cacheUrls = (List<Url>) this.applicationCache
                .getCacheObject(URL_FOR_NAVI_CACHE_KEY);
        Collection<Url> urls = getCacheUrls(cacheUrls, companyId, parentId);
        UserAuthentication authentication = new UserAuthentication(user);
        Collection<Url> result = new ArrayList<Url>();
        authorityCheck(urls, authentication, result);
        return result;
    }
 
    @SuppressWarnings("unchecked")
    public Collection<Url> loadMeunUrlsBySystemId(String companyId,String systemId) {
        IUser user = ContextHolder.getLoginUser();
        if (user == null) {
            throw new NoneLoginException("Please login first");
        }
        if (StringUtils.isNotEmpty(companyId)) {
            companyId = user.getCompanyId();
        }
 
        List<Url> cacheUrls = (List<Url>) this.applicationCache
                .getCacheObject(URL_FOR_NAVI_CACHE_KEY);
 
        Collection<Url> urls = getCacheUrlsBySystemId(cacheUrls, companyId,systemId);
        UserAuthentication authentication = new UserAuthentication(user);
        Collection<Url> result = new ArrayList<Url>();
        authorityCheck(urls, authentication, result);
        return result;
    }
 
    private void authorityCheck(Collection<Url> urls,
            UserAuthentication authentication, Collection<Url> result) {
        for (Url url : urls) {
            String targetUrl = url.getUrl();
            List<Url> children = url.getChildren();
            int childrenCount = 0;
            if (children != null) {
                childrenCount = children.size();
            }
            if (childrenCount == 0 && StringUtils.isEmpty(targetUrl)) {
                continue;
            }
            if (StringUtils.isEmpty(targetUrl)) {
                targetUrl = url.getName();
            }
            try {
                SecurityUtils.checkUrl(authentication, targetUrl);
                Url newUrl = buildNewUrl(url);
                result.add(newUrl);
                if (children != null) {
                    List<Url> childrenUrls = new ArrayList<Url>();
                    newUrl.setChildren(childrenUrls);
                    authorityCheck(children, authentication, childrenUrls);
                }
            } catch (AccessDeniedException ex) {
            }
        }
    }
 
    @DataProvider
    public Collection<Url> loadContainChildMeunUrls(String parentId) {
        Collection<Url> result = this.loadMeunUrls(parentId);
        this.loadContainChildMeunUrls(result, parentId);
        return result;
    }
 
    private void loadContainChildMeunUrls(Collection<Url> result,
            String parentId) {
        for (Url url : result) {
            List<Url> childList = new ArrayList<Url>();
            childList.addAll(this.loadMeunUrls(url.getId()));
            url.setChildren(childList);
            this.loadContainChildMeunUrls(childList, url.getId());
        }
    }
 
    public ApplicationCache getApplicationCache() {
        return applicationCache;
    }
 
    public void setApplicationCache(ApplicationCache applicationCache) {
        this.applicationCache = applicationCache;
    }
 
    private List<Url> getCacheUrls(List<Url> urls, String companyId,
            String parentId) {
        List<Url> resultUrls = new ArrayList<Url>();
        this.buildCacheUrls(urls, resultUrls, companyId, parentId);
        return resultUrls;
    }
 
    private List<Url> getCacheUrlsBySystemId(List<Url> urls, String companyId,String systemId) {
        List<Url> resultUrls = new ArrayList<Url>();
        this.buildCacheUrlsBySystemId(urls, resultUrls, companyId, systemId);
        return resultUrls;
    }
 
    private void buildCacheUrlsBySystemId(List<Url> urls, List<Url> resultUrls,
            String companyId, String systemId) {
        for (Url url : urls) {
            if (StringUtils.isEmpty(url.getSystemId())) {
                continue;
            }
            if (url.getCompanyId().equals(companyId) && url.getSystemId().equals(systemId)) {
                resultUrls.add(url);
            }
        }
    }
 
    private void buildCacheUrls(List<Url> urls, List<Url> resultUrls,
            String companyId, String parentId) {
        for (Url url : urls) {
            if (StringUtils.isEmpty(parentId)) {
                if (StringUtils.isEmpty(url.getParentId())
                        && url.getCompanyId() != null
                        && url.getCompanyId().equals(companyId)) {
                    resultUrls.add(url);
                }
            } else {
                if (StringUtils.isNotEmpty(url.getParentId())
                        && url.getParentId().equals(parentId)) {
                    resultUrls.add(url);
                }
            }
            if (url.getChildren() != null) {
                this.buildCacheUrls(url.getChildren(), resultUrls, companyId,
                        parentId);
            }
        }
    }
 
    public void afterPropertiesSet() throws Exception {
        cacheNavigatorUrls();
    }
 
    public void cacheNavigatorUrls() {
        Session session = this.getSessionFactory().openSession();
        try {
            List<Url> urls = this.loadUrls(null, session);
            this.applicationCache.putCacheObject(URL_FOR_NAVI_CACHE_KEY, urls);
        } finally {
            session.close();
        }
    }
 
    @SuppressWarnings("unchecked")
    private List<Url> loadUrls(String parentId, Session session) {
        String hql = "from " + Url.class.getName()
                + " u where u.forNavigation=:forNavigation";
        List<Url> urls = null;
        if (StringUtils.isNotEmpty(parentId)) {
            hql += " and u.parentId = :parentId order by u.order asc";
            urls = session.createQuery(hql).setBoolean("forNavigation", true)
                    .setString("parentId", parentId).list();
        } else {
            hql += " and u.parentId is null order by u.order asc";
            urls = session.createQuery(hql).setBoolean("forNavigation", true)
                    .list();
        }
        for (Url url : urls) {
            url.setChildren(this.loadUrls(url.getId(), session));
        }
        return urls;
    }
 
    private Url buildNewUrl(Url oldUrl) {
        Url url = new Url();
        url.setId(oldUrl.getId());
        url.setName(oldUrl.getName());
        url.setDesc(oldUrl.getDesc());
        url.setUrl(oldUrl.getUrl());
        url.setIcon(oldUrl.getIcon());
        url.setParentId(oldUrl.getParentId());
        url.setCompanyId(oldUrl.getCompanyId());
        url.setSystemId(oldUrl.getSystemId());
        url.setTarget(oldUrl.getTarget());
        return url;
    }
 
}
 
class UrlRowMapper implements RowMapper<Url> {
    public Url mapRow(ResultSet rs, int rowNum) throws SQLException {
        Url url = new Url();
        url.setId(rs.getString("ID_"));
        url.setName(rs.getString("NAME_"));
        url.setDesc(rs.getString("DESC_"));
        url.setUrl(rs.getString("URL_"));
        url.setIcon(rs.getString("ICON_"));
        url.setParentId(rs.getString("PARENT_ID_"));
        url.setCompanyId(rs.getString("COMPANY_ID_"));
        url.setSystemId(rs.getString("SYSTEM_ID_"));
        url.setTarget(rs.getString("TARGET_"));
        return url;
    }
}