Browse Source

Merge remote-tracking branch 'origin/dev' into test

jiapeng 7 years ago
parent
commit
496a2b2857
1 changed files with 182 additions and 2 deletions
  1. 182 2
      src/main/java/com/ekexiu/portal/cms/TemplateService.java

+ 182 - 2
src/main/java/com/ekexiu/portal/cms/TemplateService.java

@ -8,6 +8,8 @@ import java.io.OutputStreamWriter;
8 8
import java.io.Reader;
9 9
import java.io.StringReader;
10 10
import java.sql.Connection;
11
import java.sql.PreparedStatement;
12
import java.sql.ResultSet;
11 13
import java.sql.SQLException;
12 14
import java.util.HashMap;
13 15
import java.util.Map;
@ -50,6 +52,10 @@ public class TemplateService {
50 52
	private Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);
51 53

52 54
	private long rate = 10000;
55
	
56
	private long lastIndexStatic = 0;
57
	
58
	private long indexRate = 24*60*60*1000;
53 59

54 60
	@Autowrie
55 61
	private ArticleDao articleDao;
@ -172,7 +178,7 @@ public class TemplateService {
172 178
		}
173 179
	}
174 180

175
	public void handle(DataSource ds) {
181
	public synchronized void handle(DataSource ds) {
176 182
		Connection con;
177 183
		try {
178 184
			con = ds.getConnection();
@ -184,6 +190,11 @@ public class TemplateService {
184 190
				if (!this.handle(con))
185 191
					break;
186 192
			}
193
			long ct = System.currentTimeMillis();
194
			if((ct - this.lastIndexStatic)> this.indexRate){
195
				this.handleIndex(con);
196
				this.lastIndexStatic = ct;
197
			}
187 198
		} finally {
188 199
			if (con != null) {
189 200
				try {
@ -194,7 +205,17 @@ public class TemplateService {
194 205
		}
195 206

196 207
	}
197

208
	public void handleIndex(Connection con){
209
		this.staticArticleIndex(con);
210
		this.staticPaperIndex(con);
211
		this.staticPatentIndex(con);
212
		try {
213
			this.saveFile(new File(dir,"index.html"), "<!DOCTYPE html><html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head><body>"
214
					+ "<a href='a/index.html'>article</a><br><a href='pp/index.html'>paper</a><br><a href='pt/index.html'>patent</a></body></html>");
215
		} catch (IOException e) {
216
		}
217
	}
218
	
198 219
	public boolean handle(Connection con) {
199 220
		Node node = this.queue.peek();
200 221
		if (node == null)
@ -246,6 +267,165 @@ public class TemplateService {
246 267
		}
247 268
	}
248 269

270

271
	private void saveFile(File dest,String cnt) throws IOException{
272
		if(dest.exists()){
273
			dest.delete();
274
		}
275
		byte[] fc =cnt.getBytes(ConstData.UTF8);
276
		FileOutputStream fos = new FileOutputStream(dest);
277
		try{
278
			fos.write(fc);
279
			fos.flush();
280
		}finally{
281
			try{fos.close();}catch(IOException e){}
282
		}
283
	}
284
	
285
	
286
	private void flushIndex(StringBuilder cnt,String path,int pageno) throws IOException {
287
		StringBuilder sb = new StringBuilder();
288
		sb.append("<!DOCTYPE html><html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head><body>");		
289
		sb.append(cnt);
290
		if(pageno>1){
291
			sb.append("<a href='index_"+(pageno-1)+".html'>上一页</a>");
292
		}else if(pageno==1){
293
			sb.append("<a href='index.html'>上一页</a>");
294
		}
295
		sb.append("<a href='index_"+(pageno+1)+".html'>下一页</a></body></html>");
296
		File dest = new File(dir,path+"/index"+(pageno==0?"":("_"+pageno))+".html");
297
		this.saveFile(dest, sb.toString());
298
	}
299
	
300
	private void staticArticleIndex(Connection con) {
301
		int pageno = 0;
302
		int rn = 0;
303
		StringBuilder sb = new StringBuilder();
304
		try {
305
			PreparedStatement ps = con.prepareStatement("SELECT ARTICLE_ID,CREATE_TIME,SHARE_ID FROM ARTICLE WHERE STATUS ='1'");
306
			try {
307
				ResultSet rs = ps.executeQuery();
308
				try {
309
					while (rs.next()) {
310
						String id = rs.getString(1);
311
						String ct = rs.getString(2);
312
						long si = rs.getLong(3);
313
						sb.append("<a href='").append(ct.substring(0,8)).append("/").append(si).append(".html'>").append(rn).append("  :  ").append(id).append("</a><br>");
314
						++rn;
315
						if(rn == 200){
316
							flushIndex(sb, "a", pageno);
317
							++pageno;
318
							rn=0;
319
							sb.delete(0,sb.length());
320
						}
321
					}
322
					flushIndex(sb, "a", pageno);
323
					sb.delete(0,sb.length());
324
				} finally {
325
					try {
326
						rs.close();
327
					} catch (SQLException e) {
328

329
					}
330
				}
331
			} finally {
332
				try {
333
					ps.close();
334
				} catch (SQLException e) {
335

336
				}
337
			}
338
			
339
			
340
		} catch (Exception e) {
341
			log.error("static article index error", e);
342
		}
343
		
344
	}
345
	private void staticPaperIndex(Connection con) {
346
		int pageno = 0;
347
		int rn = 0;
348
		StringBuilder sb = new StringBuilder();
349
		try {
350
			PreparedStatement ps = con.prepareStatement("SELECT ID,CREATE_TIME,SHARE_ID FROM PPAPER");
351
			try {
352
				ResultSet rs = ps.executeQuery();
353
				try {
354
					while (rs.next()) {
355
						String id = rs.getString(1);
356
						String ct = rs.getString(2);
357
						long si = rs.getLong(3);
358
						sb.append("<a href='").append(ct.substring(0,8)).append("/").append(si).append(".html'>").append(rn).append(" : ").append(id).append("</a><br>");
359
						++rn;
360
						if(rn  ==200){
361
							flushIndex(sb, "pp", pageno);
362
							++pageno;
363
							rn=0;
364
							sb.delete(0,sb.length());
365
						}
366
					}
367
				} finally {
368
					try {
369
						rs.close();
370
					} catch (SQLException e) {
371

372
					}
373
				}
374
				flushIndex(sb, "pp", pageno);
375
				sb.delete(0,sb.length());
376
			} finally {
377
				try {
378
					ps.close();
379
				} catch (SQLException e) {
380

381
				}
382
			}
383
		} catch (Exception e) {
384
			log.error("static paper index error", e);
385
		}
386
	}
387
	private void staticPatentIndex(Connection con) {
388
		int pageno = 0;
389
		int rn = 0;
390
		StringBuilder sb = new StringBuilder();
391
		try {
392
			PreparedStatement ps = con.prepareStatement("SELECT ID,CREATE_TIME,SHARE_ID FROM PPATENT");
393
			try {
394
				ResultSet rs = ps.executeQuery();
395
				try {
396
					while (rs.next()) {
397
						String id = rs.getString(1);
398
						String ct = rs.getString(2);
399
						long si = rs.getLong(3);
400
						sb.append("<a href='").append(ct.substring(0,8)).append("/").append(si).append(".html'>").append(rn).append(" : ").append(id).append("</a>");
401
						++rn;
402
						if(rn ==200){
403
							flushIndex(sb, "pt", pageno);
404
							++pageno;
405
							rn=0;
406
							sb.delete(0,sb.length());
407
						}
408
					}
409
				} finally {
410
					try {
411
						rs.close();
412
					} catch (SQLException e) {
413

414
					}
415
				}
416
				flushIndex(sb, "pt", pageno);
417
				sb.delete(0,sb.length());
418
			} finally {
419
				try {
420
					ps.close();
421
				} catch (SQLException e) {
422

423
				}
424
			}
425
		} catch (Exception e) {
426
			log.error("static article index error", e);
427
		}
428
	}
249 429
	private void staticPaper(Connection con, Object key) {
250 430
		try {
251 431
			Ppaper page = this.ppaperDao.query(con, (String) key);