⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 action.java

📁 CD Manager光盘资料管家婆源代码
💻 JAVA
字号:
package com.galaxyworkstation.control;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;

import com.db4o.ObjectSet;
import com.galaxyworkstation.model.Album;
import com.galaxyworkstation.model.CD;
import com.galaxyworkstation.model.CDScanner;
import com.galaxyworkstation.model.Config;
import com.galaxyworkstation.model.Database;
import com.galaxyworkstation.model.DiscScanner;
import com.galaxyworkstation.model.IndexFactory;
import com.galaxyworkstation.model.Log;
import com.galaxyworkstation.model.MyException;
import com.galaxyworkstation.model.ScanInfoPanel;
import com.galaxyworkstation.model.Search;
import com.galaxyworkstation.model.ToDisplay;
import com.galaxyworkstation.view.MainGUI;

/**
 * 该类扮演了整个程序逻辑的核心,封装了程序所需要的主要功能
 * @author 李奕
 * @version 1.0
 */
public class Action {

	private Album rootAlbum;
	private HashMap<Long, Album> albumMap;
	private HashMap<String, CD> cdMap;
	private Config config;
	private boolean CDNeedOptimize;

	/**
	 * 构造函数
	 * 初始化程序所需要的数据
	 */
	public Action(){
		rootAlbum = new Album("CD Manager", 0);		
		albumMap = new HashMap<Long, Album>();
		cdMap = new HashMap<String, CD>();
		albumMap.put(rootAlbum.getID(), rootAlbum);
		CDNeedOptimize = false;
		
		this.init();
	}
	
	/**
	 * 创建一个新专辑
	 * @param name 专辑的名字;
	 * @return 创建成功的专辑
	 */
	public Album CreateNewAlbum(String name, Album parentAlbum){
		Album newAlbum = new Album(name);
		newAlbum.setParentID(parentAlbum.getID());
		albumMap.put(newAlbum.getID(), newAlbum);
		parentAlbum.getAlbums().add(newAlbum);
		Database.getDatabase().store(newAlbum);
		return newAlbum;
	}
	
	/**
	 * 重命名给定的专辑;
	 * @param album 给定的专辑
	 * @param newName 给定专辑的新名字
	 */
	public void renameAlbum(Album album, String newName){
		album.setName(newName);
		Database.getDatabase().store(album);
	}
	
	/**
	 * 将一张专辑移动到新的专辑;
	 * @param album 给定的专辑
	 * @param toAlbum 将要移往的专辑
	 */
	public void moveAlbum(Album album, Album toAlbum){
		toAlbum.getAlbums().add(album);
		Album parentAlbum = albumMap.get(album.getParentID());
		parentAlbum.getAlbums().remove(album);
		album.setParentID(toAlbum.getID());
		Database.getDatabase().store(album);
	}
	
	/**
	 * 删除给定的专辑,包括删除其容纳的所有光盘和存储在上面的数据的索引;
	 * @param album 待删除的专辑
	 */
	public void deleteAlbum(Album album){
		CDNeedOptimize = true;
		deleteAlbumLoop(album);
		IndexFactory.closeDiscIndexReader();
		IndexFactory.closeDiscIndexSearcher();
	}
	
	/**
	 * 删除给定的专辑的辅助方法,用递归实现
	 * @param album 待删除的专辑
	 */
	private void deleteAlbumLoop(Album album){	
		CD cd;
		for(int i=0; i<album.getCDs().size(); ++i){
			cd = album.getCDs().get(i);
			deleteCD(cd);
		}
		
		Database.getDatabase().delete(album);		
		albumMap.remove(album.getID());
		Album parent = albumMap.get(album.getParentID());
		parent.getAlbums().remove(album);
		
		for(int i=0; i<album.getAlbums().size(); ++i){
			deleteAlbumLoop(album.getAlbums().get(i));
		}	
		album = null;
	}
	
	/**
	 * 创建一张新的光盘,对其存储的数据建立索引;
	 * @param cdName 光盘的名字;
	 * @param description 光盘的注释
	 * @param album 光盘所属的专辑
	 * @param path 光驱的盘符
	 * @return 创建成功的光盘
	 */
	public CD addNewCD(String cdName, String description, Album album, String path){
		CD cd = new CD(cdName, description, album);
		album.getCDs().add(cd);
		cdMap.put(cd.getID(),cd);
		Database.getDatabase().store(cd);		
		Database.getDatabase().store(new Log("创建光盘记录开始,光盘名为:"+cdName));		

		CDManager.action.getConfig().setScanning(true);
		ScanInfoPanel panel = new ScanInfoPanel(MainGUI.shell);
		ExecutorService executor = Executors.newSingleThreadExecutor();
		executor.execute(new CDScanner(cd, path));
		panel.open();
		
		MessageBox msgBox = new MessageBox(MainGUI.shell, SWT.ICON_INFORMATION
				| SWT.OK);
		msgBox.setText("成功");
		msgBox.setMessage("成功创建光盘[" + cdName + "]的索引!");
		msgBox.open();
		executor.shutdown();
		return cd;
	}
	
	/**
	 * 重命名给定的光盘;
	 * @param cd 给定的光盘
	 * @param newName 给定光盘的新名字
	 */
	public void renameCD(CD cd, String newName){
		cd.setName(newName);
		Database.getDatabase().store(cd);
	}

	/**
	 * 为给定光盘添加注释;
	 * @param cd 给定的光盘
	 */
	public void setCDDescription(CD cd, String description){
		cd.setDescription(description);
		Database.getDatabase().store(cd);
	}
	
	/**
	 * 将光盘从一张专辑移动到新的专辑;
	 * @param cd 给定的光盘
	 * @param toAlbum 将要移往的专辑
	 */
	public void moveCD(CD cd, Album toAlbum){
		toAlbum.getCDs().add(cd);
		Album parentAlbum = albumMap.get(cd.getAlbumID());
		parentAlbum.getCDs().remove(cd);
		cd.setBelongAlbum(toAlbum);
		Database.getDatabase().store(cd);
	}
	
	/**
	 * 删除给定的光盘,包括其存储的数据的索引;
	 * @param cd 给定的光盘
	 */
	public void deleteCD(CD cd) {
		cd.deleteIndex();
		CDNeedOptimize = true;
		cdMap.remove(cd.getID());
		Album parentAlbum = albumMap.get(cd.getAlbumID());
		parentAlbum.getCDs().remove(cd);
		Database.getDatabase().delete(cd);
		Database.getDatabase().store(new Log("删除光盘名为 ["+cd.getName()+"]的索引记录"));
		IndexFactory.closeCDIndexReader();
		IndexFactory.closeCDIndexSearcher();
		cd = null;
	}
	
	/**
	 * 创建整个硬盘的索引
	 */
	public void createWholeDiscIndex() {
		Database.getDatabase().store(new Log("开始对整个硬盘创建索引"));
		
		CDManager.action.getConfig().setScanning(true);
		ScanInfoPanel panel = new ScanInfoPanel(MainGUI.shell);
		ExecutorService executor = Executors.newSingleThreadExecutor();
		executor.execute(new DiscScanner());
		panel.open();
		while(config.isScanning()){}
		Database.getDatabase().store(new Log("完成对整个硬盘创建索引"));
		executor.shutdown();

	}
	
	/**
	 * 增加指定路径的索引到硬盘索引
	 * @param path 待添加索引的路径
	 */
	public void addDiscIndex(String path) {
		Database.getDatabase().store(new Log("开始对硬盘["+path+"]目录创建索引"));

		config.setScanning(true);
		ScanInfoPanel panel = new ScanInfoPanel(MainGUI.shell);
		ExecutorService executor = Executors.newSingleThreadExecutor();
		executor.execute(new DiscScanner(path));
		panel.open();
		while(config.isScanning()){}
		Database.getDatabase().store(new Log("完成对硬盘["+path+"]目录创建索引"));
		executor.shutdown();
	}
	
	/**
	 * 删除全盘索引
	 */
	public void deleteWholeDiscIndex() {
		File file = new File("discindex");
		if(file.exists()){
			File[] files = file.listFiles();
			for(int i=0; i<files.length; ++i)
				files[i].delete();
			Database.getDatabase().store(new Log("删除全部的硬盘索引文件"));
			IndexFactory.closeDiscIndexReader();
			IndexFactory.closeDiscIndexSearcher();
		}
	}
	
	/**
	 * 得到给定光盘的根目录集合
	 * @param cd 给定的光盘对象
	 * @return 给定光盘的根目录集合
	 */
	public ArrayList<Document> getCDRoots(CD cd){
		return new ToDisplay().getCDRoots(cd);
	}
	
	/**
	 * 得到给定文档的子文档的集合
	 * @param doc 给定的文档对象
	 * @return 给定文档的子文档的集合
	 */
	public ArrayList<Document> getChildren(Document doc){
		return new ToDisplay().getChildren(doc);
	}
	
	/**
	 * 得到给定文档的兄弟文档的集合
	 * @param doc 给定的文档对象
	 * @return 给定文档的兄弟文档的集合
	 */
	public ArrayList<Document> getBrothers(Document doc){
		return new ToDisplay().getBrothers(doc);
	}
	
	/**
	 * 得到给定文档的长辈文档的集合
	 * @param doc 给定的文档对象
	 * @return 给定文档的长辈文档的集合
	 */
	public ArrayList<Document> getSeniors(Document doc){
		return new ToDisplay().getSeniors(doc);
	}
	
	/**
	 * 得到某个只含文件夹类型(不含文件类型)的文档集合
	 * @param docs 该数据为含有文件和文件夹的源文档集合
	 * @return 只含文件夹类型的文档集合
	 */
	public ArrayList<Document> NoFileArrayList(ArrayList<Document> docs){
		return new ToDisplay().withoutFile(docs);
	}
	
	/**
	 * 按给定关键词和给定方式搜索
	 * @param keyword 关键字
	 * @param type 搜索方式,Search.CD_SEARCH代表搜索光盘索引;Search.DISC_SEARCH代表搜索硬盘索引
	 * @return 含所有满足条件的文档集合
	 * @throws MyException 如果搜索结果超过指定范围
	 * @see MyException
	 */
	public ArrayList<Document> search(String keyword, int type) throws MyException {
		BooleanQuery.setMaxClauseCount(1024);
		return new Search().doSearch(keyword, type);
	}
	
	/**
	 * 得到一份文档所属的光盘的名字
	 * @param doc 给定的文档
	 * @return 该文档所属的名字
	 */
	public String getBelongCDName(Document doc){
		CD cd = cdMap.get(doc.get("rootID"));
		return (cd==null)?"":cd.getName();
	}
	
	/**
	 * 得到日志文件
	 * @return 日志数组
	 */
	public Log[] getLogs(){
		
		ObjectSet<Log> result=Database.getDatabase().queryByExample(Log.class);
		Log[] logArray = new Log[result.size()];
		for(int i=0; i<result.size(); ++i){
			logArray[i] = result.get(i);
		}
	    Arrays.sort(logArray);
	    return logArray;
	}
	
	/**
	 * 导出系统日志
	 * @param path 导出日志的路径
	 */
	public void exportLogs(String path){
		try {
			PrintWriter out = new PrintWriter(path+"/log.txt");
			Log[] logArray = getLogs();
			for(int i=0; i<logArray.length; ++i){
				out.println(logArray[i]);
			}
			out.close();		
		} catch (FileNotFoundException e) {
		}
	}
	
	/**
	 * 导出光盘索引
	 * @param cd 要导出的光盘
	 * @param path 导出索引的路径
	 */
	public void exportIndex(CD cd, String path){
		try {
	    	IndexSearcher searcher = IndexFactory.getCDIndexSearcher();
			TermQuery query = new TermQuery(new Term("rootID", cd.getID()));
			Hits hits = searcher.search(query);
			Document doc;
			PrintWriter out = new PrintWriter(path+File.separator+cdMap.get(cd.getID()).getName()+".txt");
			for(int i=0; i<hits.length(); ++i){
				doc = hits.doc(i);
				out.println(doc.get("path"));
			}		
			out.close();		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除所有日志
	 */
	public void deleteLogs(){
		ObjectSet<Log> result=Database.getDatabase().queryByExample(Log.class);
		while(result.hasNext()){
			Database.getDatabase().delete(result.next());
		}
	}
	
	/**
	 * 设置密码
	 * @param newPassword 设置的新密码
	 */
	public void setPassword(String newPassword){
		config.setPassword(newPassword);
		Database.getDatabase().store(config);
	}
	
	/**
	 * 程序启动相关的初始化操作
	 */
	public void init(){
		
		ArrayList<Album> albums = new ArrayList<Album>();
		Album album, existAlbum;
		CD cd;
		
		ObjectSet<Config> findingConfig = Database.getDatabase().queryByExample(Config.class);
	    if(findingConfig == null || findingConfig.size() != 1){
	    	config = new Config();
			File file = new File("cdindex");
			if(file.exists()){
				File[] files = file.listFiles();
				for(int i=0; i<files.length; ++i)
					files[i].delete();
			}
			Database.getDatabase().store(config);
	    }
	    else {
	    	config = findingConfig.get(0);
	    }	    
	    
		ObjectSet<Album> result=Database.getDatabase().queryByExample(Album.class);
	    while(result.hasNext()) {
	    	albums.add(result.next());
	    }
	    while(albums.size() != 0){
	    	for(int i=0; i<albums.size(); ++i){
	    		album = albums.get(i);
		    	existAlbum = albumMap.get(album.getParentID());
		    	if(existAlbum != null){
		    		albums.remove(i);
		    		existAlbum.getAlbums().add(album);
		    		albumMap.put(album.getID(), album);
		    	}
	    	}
	    }
	    
	    ObjectSet<CD> cds=Database.getDatabase().queryByExample(CD.class);
	    while(cds.hasNext()) {
	    	cd = cds.next();
	    	cdMap.put(cd.getID(), cd);
	        if((existAlbum=albumMap.get(cd.getAlbumID())) != null){
	        	existAlbum.getCDs().add(cd);
	        }
	    }
	}
	
	/**
	 * 程序结束,关闭数据库连接与相应的文件流,并根据需要对索引文件进行整理
	 */
	public void exit(){
		Database.close();
		IndexFactory.closeCDIndexSearcher();
		IndexFactory.closeDiscIndexSearcher();
		IndexFactory.closeCDIndexReader();
		IndexFactory.closeDiscIndexReader();
		IndexFactory.closeCDIndexSearcher();
		
		if(CDNeedOptimize){
			boolean isIndexExists = false;
			if(IndexReader.indexExists("cdindex"))
				isIndexExists = true;
			try{
				File f = new File("cdindex" + File.separator + "write.lock");
				if(f.exists())
					f.delete();
				IndexWriter indexWriter = new IndexWriter("cdindex",  new StandardAnalyzer(), !isIndexExists, IndexWriter.MaxFieldLength.LIMITED);
				indexWriter.optimize();
				indexWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 得到根节点的专辑
	 * @return 根节点的专辑
	 */
	public Album getRootAlbum() {
		return rootAlbum;
	}

	/**
	 * 得到应用中<ID, 对应的专辑>的HashMap
	 * @return 该HashMap
	 */
	public HashMap<Long, Album> getAlbumMap() {
		return albumMap;
	}

	/**
	 * 得到应用中<ID, 对应的光盘>的HashMap
	 * @return 该HashMap
	 */
	public HashMap<String, CD> getCdMap() {
		return cdMap;
	}
	
	/**
	 * 得到应用中的Config对象
	 * @return 应用中的config对象
	 * @see Config
	 */
	public Config getConfig() {
		return config;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -