You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
269 lines
8.2 KiB
269 lines
8.2 KiB
2 years ago
|
package com.fr.plugin.ztzzSSO.serverDS;
|
||
|
|
||
|
import com.fr.base.TableData;
|
||
|
import com.fr.data.AbstractDataModel;
|
||
|
import com.fr.general.data.TableDataException;
|
||
|
import com.fr.general.http.HttpToolbox;
|
||
|
import com.fr.json.JSONArray;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.plugin.ztzzSSO.config.PluginSimpleConfig;
|
||
|
import com.fr.plugin.ztzzSSO.utils.FRUtils;
|
||
|
import com.fr.plugin.ztzzSSO.utils.HttpUtils;
|
||
|
|
||
|
import java.io.*;
|
||
|
import java.net.HttpURLConnection;
|
||
|
import java.net.URL;
|
||
|
import java.util.*;
|
||
|
|
||
|
public class ORGHttpDataModel extends AbstractDataModel {
|
||
|
private static String[] COLUMN_NAMES = {"DISPLAY_PATH", "DISPLAY_PATH_NAME", "MDM_CODE","ORG_NAME","ORG_NAME_SHORT",
|
||
|
"FOUND_DATE","UPDATE_TIME","REG_ADDRESS","ORG_CODE","ORG_UID","ORG_PID"};
|
||
|
private int rowCount = TableData.RESULT_ALL;
|
||
|
private DatasetData datas = new DatasetData();
|
||
|
|
||
|
public ORGHttpDataModel(int count) {
|
||
|
this.rowCount = count;
|
||
|
if (this.rowCount == 0) {
|
||
|
return;
|
||
|
}
|
||
|
queryData();
|
||
|
}
|
||
|
|
||
|
//获取列的数量
|
||
|
@Override
|
||
|
public int getColumnCount() throws TableDataException {
|
||
|
return COLUMN_NAMES.length;
|
||
|
}
|
||
|
|
||
|
//获取列的名称
|
||
|
@Override
|
||
|
public String getColumnName(int i) throws TableDataException {
|
||
|
return COLUMN_NAMES[i];
|
||
|
}
|
||
|
|
||
|
//是否还有行
|
||
|
@Override
|
||
|
public boolean hasRow(int rowIndex) throws TableDataException {
|
||
|
int count = getRowCount();
|
||
|
return rowIndex < count;
|
||
|
}
|
||
|
|
||
|
//获取行数量
|
||
|
@Override
|
||
|
public int getRowCount() throws TableDataException {
|
||
|
if (this.datas == null) {
|
||
|
return 0;
|
||
|
}
|
||
|
List<List<Object>> values = this.datas.getValues();
|
||
|
if (values == null) {
|
||
|
return 0;
|
||
|
}
|
||
|
int count = values.size();
|
||
|
return count;
|
||
|
}
|
||
|
|
||
|
//获取数据
|
||
|
@Override
|
||
|
public Object getValueAt(int rowIndex, int columnIndex) throws TableDataException {
|
||
|
if (this.datas == null) {
|
||
|
return "";
|
||
|
}
|
||
|
List<List<Object>> values = this.datas.getValues();
|
||
|
if ((values == null) || (values.size() <= rowIndex)) {
|
||
|
return "";
|
||
|
}
|
||
|
List<Object> rowValues = values.get(rowIndex);
|
||
|
if ((rowValues == null) || (rowValues.size() <= columnIndex)) {
|
||
|
return "";
|
||
|
}
|
||
|
return rowValues.get(columnIndex);
|
||
|
}
|
||
|
|
||
|
//释放
|
||
|
@Override
|
||
|
public void release() throws Exception {
|
||
|
this.datas = null;
|
||
|
}
|
||
|
|
||
|
public static String httpRequest(String requestUrl, String requestMethod,
|
||
|
String outputStr, String tokenid) {
|
||
|
StringBuffer buffer = null;
|
||
|
HttpURLConnection conn = null;
|
||
|
InputStream is = null;
|
||
|
InputStreamReader isr = null;
|
||
|
try {
|
||
|
URL url = new URL(requestUrl);
|
||
|
conn = (HttpURLConnection) url.openConnection();
|
||
|
conn.setDoOutput(true);
|
||
|
conn.setDoInput(true);
|
||
|
conn.setRequestMethod(requestMethod);
|
||
|
conn.setRequestProperty("Content-Type", "application/json");
|
||
|
conn.setRequestProperty("Authorization", tokenid);
|
||
|
conn.setRequestProperty("X-MDM-UNIFIED-RESPONSE", "true");
|
||
|
conn.connect();
|
||
|
// 往服务器端写内容 也就是发起http请求需要带的参数
|
||
|
if (null != outputStr) {
|
||
|
OutputStream os = conn.getOutputStream();
|
||
|
os.write(outputStr.getBytes("utf-8"));
|
||
|
os.close();
|
||
|
}
|
||
|
|
||
|
// 读取服务器端返回的内容
|
||
|
is = conn.getInputStream();
|
||
|
isr = new InputStreamReader(is, "utf-8");
|
||
|
BufferedReader br = new BufferedReader(isr);
|
||
|
buffer = new StringBuffer();
|
||
|
String line = null;
|
||
|
while ((line = br.readLine()) != null) {
|
||
|
buffer.append(line);
|
||
|
}
|
||
|
System.out.println(buffer);
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}finally{
|
||
|
if(null != conn){
|
||
|
conn.disconnect();
|
||
|
}
|
||
|
if(null != is){
|
||
|
try {
|
||
|
is.close();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
if(null != isr){
|
||
|
try {
|
||
|
isr.close();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return buffer.toString();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 查询数据
|
||
|
*/
|
||
|
private void queryData() {
|
||
|
try {
|
||
|
String token = getToken();
|
||
|
FRUtils.FRLogInfo("orgToken:"+token);
|
||
|
createDatas(0,token);
|
||
|
} catch (Exception e) {
|
||
|
FRUtils.FRLogInfo("数据集获取组织信息,请求出错:"+e.getMessage());
|
||
|
throw new NullPointerException("数据集获取组织信息为空," + e.getMessage());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取token
|
||
|
* @return
|
||
|
*/
|
||
|
private String getToken(){
|
||
|
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
|
||
|
String tokenUrl = psc.getTokenUrl2();
|
||
|
String username = psc.getUsername();
|
||
|
String psd = psc.getPsd();
|
||
|
|
||
|
Map<String,String> header = new HashMap<String,String>();
|
||
|
header.put("Content-Type","application/json");
|
||
|
header.put("X-MDM-UNIFIED-RESPONSE","true");
|
||
|
|
||
|
JSONObject params = new JSONObject();
|
||
|
// Map<String,String> params = new HashMap<String,String>();
|
||
|
params.put("username",username);
|
||
|
params.put("password",psd);
|
||
|
|
||
|
String result = "";
|
||
|
|
||
|
try {
|
||
|
result = HttpUtils.HttpPostJson(tokenUrl,params.toString(),header);
|
||
|
} catch (Exception e) {
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
JSONObject resultJson = new JSONObject(result);
|
||
|
|
||
|
return resultJson.getString("token");
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取组织数据
|
||
|
* @param pageNum
|
||
|
* @param token
|
||
|
* @return
|
||
|
*/
|
||
|
private JSONObject getOrg(int pageNum,String token){
|
||
|
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
|
||
|
|
||
|
String orgUrl = psc.getOrgUrl()+"?pageNum="+pageNum+"&pageSize=1000";;
|
||
|
|
||
|
FRUtils.FRLogInfo("orgUrl:"+orgUrl);
|
||
|
|
||
|
String result = httpRequest(orgUrl,"POST",null,token);
|
||
|
FRUtils.FRLogInfo("orgResult:"+result);
|
||
|
|
||
|
return new JSONObject(result);
|
||
|
}
|
||
|
|
||
|
private void createDatas(int pageNum,String token) {
|
||
|
JSONObject json = getOrg(pageNum,token);
|
||
|
JSONArray jsonArray = json.getJSONArray("content");
|
||
|
addRowDatas(jsonArray);
|
||
|
|
||
|
boolean last = json.getBoolean("last");
|
||
|
if(last){
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
pageNum = pageNum+1;
|
||
|
|
||
|
createDatas(pageNum,token);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 添加行数据
|
||
|
* @param jsonArray
|
||
|
*/
|
||
|
private void addRowDatas(JSONArray jsonArray) {
|
||
|
if(jsonArray == null || jsonArray.size() <= 0){
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
for(int i =0;i< jsonArray.size();i++){
|
||
|
JSONObject org = jsonArray.getJSONObject(i);
|
||
|
|
||
|
String DISPLAY_PATH = org.getString("DISPLAY_PATH");
|
||
|
String DISPLAY_PATH_NAME = org.getString("DISPLAY_PATH_NAME");
|
||
|
String MDM_CODE = org.getString("MDM_CODE");
|
||
|
String ORG_NAME = org.getString("ORG_NAME");
|
||
|
String ORG_NAME_SHORT = org.getString("ORG_NAME_SHORT");
|
||
|
String FOUND_DATE = org.getString("FOUND_DATE");
|
||
|
String UPDATE_TIME = org.getString("UPDATE_TIME");
|
||
|
String REG_ADDRESS = org.getString("REG_ADDRESS");
|
||
|
String ORG_CODE = org.getString("ORG_CODE");
|
||
|
String ORG_UID = org.getString("ORG_UID");
|
||
|
String ORG_PID = org.getString("ORG_PID");
|
||
|
|
||
|
List<Object> rowDatas = new ArrayList<Object>();
|
||
|
|
||
|
rowDatas.add(DISPLAY_PATH);
|
||
|
rowDatas.add(DISPLAY_PATH_NAME);
|
||
|
rowDatas.add(MDM_CODE);
|
||
|
rowDatas.add(ORG_NAME);
|
||
|
rowDatas.add(ORG_NAME_SHORT);
|
||
|
rowDatas.add(FOUND_DATE);
|
||
|
rowDatas.add(UPDATE_TIME);
|
||
|
rowDatas.add(REG_ADDRESS);
|
||
|
rowDatas.add(ORG_CODE);
|
||
|
rowDatas.add(ORG_UID);
|
||
|
rowDatas.add(ORG_PID);
|
||
|
|
||
|
List<List<Object>> values = this.datas.getValues();
|
||
|
values.add(rowDatas);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|