Commits

Jiangge Zhang committed 66f7432

BUG FIX

Comments (0)

Files changed (7)

Project/assets/database.xml

 	"tag_id" INTEGER REFERENCES "tags" ("id"),
 	PRIMARY KEY("note_id", "tag_id")
 );
-INSERT INTO notebooks(name,uuid,created,machine_id,is_recycled) VALUES('默认笔记本','default',CURRENT_TIMESTAMP,'default',0);
+INSERT INTO notebooks(id,name,uuid,created,machine_id,is_recycled) VALUES(-1, '默认笔记本','default',CURRENT_TIMESTAMP,'default',0);
 </schema>

Project/src/com/noodle/note/activity/SavingNoteActivity.java

 
 import com.noodle.note.R;
 import com.noodle.note.gateway.NotebookGateway;
+import com.noodle.note.gateway.TagGateway;
 import com.noodle.note.model.Note;
 import com.noodle.note.model.Notebook;
+import com.noodle.note.model.Tag;
 import com.noodle.note.persistent.unitofwork.Session;
 
 /**
 	 * 工作单元
 	 */
 	private Session session = Session.getInstance(this);
-	
+
 	/**
 	 * 笔记本的表数据入口
 	 */
-	NotebookGateway notebookQuery = (NotebookGateway) session.query(Notebook.class);
-	
+	NotebookGateway notebookQuery = (NotebookGateway) session
+			.query(Notebook.class);
+
+	/**
+	 * 标签的表数据入口
+	 */
+
+	TagGateway tagGateway = (TagGateway) session.query(Tag.class);
+
 	/**
 	 * Note 笔记模型
 	 */
 		super.onCreate(savedInstanceState);
 		setContentView(R.layout.saving_note);
 		findViews();
+		findModel();
 		initViews();
 		setListeners();
-		findModel();
 	}
 
 	/**
 	protected void initViews() {
 		// 查询所有笔记本
 		List<Notebook> notebooks = notebookQuery.findAll();
-		
+
 		// 发布到笔记本视图
 		int itemStyle = android.R.layout.simple_spinner_dropdown_item;
 		notebookAdapter = new ArrayAdapter<Notebook>(this, itemStyle, notebooks);
 		notebookSpinner.setAdapter(notebookAdapter);
+
+		// 初始化视图数据
+		String title = note.getTitle();
+		String author = note.getAuthor();
+		//Set<Tag> tags = note.getTags();
+		// FIXME: 标签
+		/*String[] tagsTextArray = (tags.size() == 0) ? new String[0] : tags.toArray(new String[0]);
+		String tagText = StringHelper.join(tagsTextArray, ",");*/
+		Notebook notebook = note.getNotebook();
+
+		if (notebook == null) {
+			notebook = notebookQuery.findDefault();
+		}
+
+		titleTextbox.setText(title);
+		//tagTextbox.setText(tagText);
+		authorTextbox.setText(author);
+		notebookSpinner.setSelection(notebookAdapter.getPosition(notebook));
 	}
 
 	/**
 	/**
 	 * 保存笔记
 	 */
-	private void saveNote(String title, String author, String[] tags, Notebook notebook) {
+	private void saveNote(String title, String author, String[] tagsText,
+			Notebook notebook) {
 		// 验证输入
 		if (title == null || title.trim().length() == 0) {
 			Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
 			Toast.makeText(this, "请选择笔记本", Toast.LENGTH_SHORT).show();
 			return;
 		}
-		// TODO: 处理标签
+		
+		// 处理标签
+		for(String tagText : tagsText) {
+			Tag tag = tagGateway.findOrCreate(tagText);
+			note.addTag(tag);
+		}
 
 		note.setTitle(title.trim());
 		note.setAuthor(author.trim());
 		startActivity(intent);
 		this.finish();
 	}
-
 }

Project/src/com/noodle/note/gateway/NoteGateway.java

 		SQLiteDatabase db = getWritableDatabase();
 		// 执行插入操作
 		db.insert(TABLE, null, getValues(model));
+		// 插入标签情况
+		
+		
 		// 改变状态
 		Integer id = (int) DbHelper.getLastInsertedId(db);
 		AttributeInjecter.set(model, "id", id);

Project/src/com/noodle/note/gateway/TagGateway.java

 		// 选择参数
 		String[] params = { String.valueOf(id) };
 		// 获取查询结果游标
-		Cursor cursor = db.query(TABLE, COLUMNS, "id=?", params, null, null, "id DESC");
+		Cursor cursor = db.query(TABLE, COLUMNS, "id=?", params, null, null,
+				"id DESC");
 		// 注入对象属性
 		List<Tag> tags = injectAll(cursor);
 		// 提取Note对象
 			selectionList.add(key + "=?");
 			paramsList.add(String.valueOf(conditions.get(key)));
 		}
-		String selection = StringHelper.join(selectionList.toArray(new String[0]), " AND ");
+		String selection = StringHelper.join(
+				selectionList.toArray(new String[0]), " AND ");
 		String[] params = (String[]) paramsList.toArray(new String[0]);
 		// 获取查询结果游标
-		Cursor cursor = db.query(TABLE, COLUMNS, selection, params, null, null, "created DESC");
+		Cursor cursor = db.query(TABLE, COLUMNS, selection, params, null, null,
+				"created DESC");
 		// 注入对象属性
 		List<Tag> tags = injectAll(cursor);
 		// 关闭游标与数据库连接
 		// 获得数据库连接
 		SQLiteDatabase db = getReadableDatabase();
 		// 获取查询结果游标
-		Cursor cursor = db.query(TABLE, COLUMNS, null, null, null, null, "created DESC");
+		Cursor cursor = db.query(TABLE, COLUMNS, null, null, null, null,
+				"created DESC");
 		// 注入对象属性
 		List<Tag> tags = injectAll(cursor);
 		// 关闭游标与数据库连接
 	}
 
 	/**
+	 * 查询关键字对应的标签,如果不存在则创建,最后将其返回
+	 * @param keyword
+	 * @return
+	 */
+	public Tag findOrCreate(String keyword) {
+		// 查询标签
+		HashMap<String, String> conditions = new HashMap<String, String>();
+		conditions.put("keyword", keyword);
+		List<Tag> tags = findBy(conditions);
+		Tag tag;
+
+		if (tags.size() == 0) {
+			// 创建新标签
+			tag = new Tag(keyword);
+			create(tag);
+		} else {
+			// 获取已有标签
+			tag = tags.get(0);
+		}
+		
+		return tag;
+	}
+
+	/**
 	 * 添加新标签
 	 */
 	@Override
 			Tag tag = findModel(cursor.getInt(cursor.getColumnIndex("id")));
 			if (null == tag) {
 				tag = new Tag();
-				AttributeInjecter<Tag> tagInjecter = new AttributeInjecter<Tag>(tag);
-				tagInjecter.setValue("id", cursor.getInt(cursor.getColumnIndex("id")));
-				tagInjecter.setValue("created", new Date(cursor.getLong(cursor.getColumnIndex("created"))));
+				AttributeInjecter<Tag> tagInjecter = new AttributeInjecter<Tag>(
+						tag);
+				tagInjecter.setValue("id",
+						cursor.getInt(cursor.getColumnIndex("id")));
+				tagInjecter.setValue(
+						"created",
+						new Date(cursor.getLong(cursor
+								.getColumnIndex("created"))));
 				tag.setWord(cursor.getString(cursor.getColumnIndex("keyword")));
 				tags.add(tag);
 			} else

Project/src/com/noodle/note/model/Note.java

 	/**
 	 * 笔记标签
 	 */
-	protected Set<Tag> tags;
+	protected Set<Tag> tags = new HashSet<Tag>();
 
 	/**
 	 * 书签
 	 */
-	protected Set<Bookmark> bookmarks;
+	protected Set<Bookmark> bookmarks = new HashSet<Bookmark>();
 
 	/**
 	 * 是否已经放入回收站
 		this.visitedCount = 0;
 		this.password = "";
 		this.savePath = "";
-		this.tags = new HashSet<Tag>();
-		this.bookmarks = new HashSet<Bookmark>();
 		this.isRemoved = false;
 	}
 
 	public void setRemoved(boolean isRemoved) {
 		this.isRemoved = isRemoved;
 	}
+	
+	public Set<Tag> getTags() {
+		return tags;
+	}
 
 	public void addTag(Tag tag) {
 		tags.add(tag);

Project/src/com/noodle/note/model/Notebook.java

 
 import java.util.Date;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Set;
 import java.util.UUID;
 
 	/**
 	 * 笔记
 	 */
-	protected Set<Note> notes;
+	protected Set<Note> notes = new HashSet<Note>();
 
 	/**
 	 * 否已经放入回收站

Project/src/com/noodle/note/model/Tag.java

 
 import java.util.Date;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Set;
 
 import com.noodle.note.persistent.EntityModel;
 	/**
 	 * 所属笔记
 	 */
-	protected Set<Note> notes;
+	protected Set<Note> notes = new HashSet<Note>();
 
 	public Tag() {
 	}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.