jiapeng 7 gadi atpakaļ
vecāks
revīzija
e97a54b28e
1 mainītis faili ar 173 papildinājumiem un 1 dzēšanām
  1. 173 1
      src/main/java/com/ekexiu/portal/cms/TemplateService.java

+ 173 - 1
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;
@ -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,12 @@ 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
	}
213
	
198 214
	public boolean handle(Connection con) {
199 215
		Node node = this.queue.peek();
200 216
		if (node == null)
@ -246,6 +262,162 @@ public class TemplateService {
246 262
		}
247 263
	}
248 264

265

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

323
					}
324
				}
325
			} finally {
326
				try {
327
					ps.close();
328
				} catch (SQLException e) {
329

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

365
					}
366
				}
367
				flushIndex(sb, "a", pageno);
368
				sb.delete(0,sb.length());
369
			} finally {
370
				try {
371
					ps.close();
372
				} catch (SQLException e) {
373

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

406
					}
407
				}
408
				flushIndex(sb, "a", pageno);
409
				sb.delete(0,sb.length());
410
			} finally {
411
				try {
412
					ps.close();
413
				} catch (SQLException e) {
414

415
				}
416
			}
417
		} catch (Exception e) {
418
			log.error("static article index error", e);
419
		}
420
	}
249 421
	private void staticPaper(Connection con, Object key) {
250 422
		try {
251 423
			Ppaper page = this.ppaperDao.query(con, (String) key);