package com.zuoxiaolong.servlet;

/*
 * Copyright 2002-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

import javax.servlet.ServletException;

import com.zuoxiaolong.dao.*;
import com.zuoxiaolong.orm.DaoFactory;
import com.zuoxiaolong.util.JsoupUtil;
import org.jsoup.Jsoup;

import com.zuoxiaolong.mvc.RequestMapping;
import com.zuoxiaolong.reptile.Cnblogs;
import com.zuoxiaolong.util.StringUtil;

/**
 * @author 左潇龙
 * @since 2015年6月18日 上午2:28:03
 */
@RequestMapping("/admin/updateArticle.do")
public class AdminUpdateArticle extends AbstractServlet {

	@Override
	protected void service() throws ServletException, IOException {
		Integer id = Integer.valueOf(getRequest().getParameter("id"));
		String subject = getRequest().getParameter("subject");
		String html = getRequest().getParameter("content");
		String status = getRequest().getParameter("status");
        String type = getRequest().getParameter("type");
		String icon = getRequest().getParameter("icon");
        String[] categories = getRequest().getParameter("categories").split(",");
        String[] tags = getRequest().getParameter("tags").split(",");
		html = handleQuote(html);
		
		StringBuffer stringBuffer = new StringBuffer();
		JsoupUtil.appendText(Jsoup.parse(html), stringBuffer);
		DaoFactory.getDao(ArticleDao.class).saveOrUpdate(String.valueOf(id), subject, Integer.valueOf(status), Integer.valueOf(type), "左潇龙", html, stringBuffer.toString(), icon);
        if (tags == null || categories == null) {
            return;
        }
        DaoFactory.getDao(TagDao.class).delete(id);
        for (int i = 0; i < tags.length; i++) {
            String tag = tags[i].trim();
            if (tag.length() == 0) {
                continue;
            }
            Integer tagId = DaoFactory.getDao(TagDao.class).getId(tag);
            if (tagId == null) {
                tagId = DaoFactory.getDao(TagDao.class).save(tag);
            }
            DaoFactory.getDao(ArticleTagDao.class).save(id, tagId);
        }
        DaoFactory.getDao(CategoryDao.class).delete(id);
        for (int i = 0; i < categories.length; i++) {
            String categoryId = categories[i].trim();
            if (categoryId.length() == 0) {
                continue;
            }
            DaoFactory.getDao(ArticleCategoryDao.class).save(id, Integer.valueOf(categoryId));
        }
        writeText("success");
	}

}

@@ -826,16 +826,60 @@
 upUtil;%0A
+import org.apache.commons.lang.StringUtils;%0A
 import o
@@ -1257,37 +1257,20 @@
 %7B%0A%09%09
-Integer id = Integer.valueOf(
+String id = 
 getR
@@ -1296,17 +1296,16 @@
 er(%22id%22)
-)
 ;%0A%09%09Stri
@@ -1862,18 +1862,44 @@
 uffer);%0A
-%09%09
+        Integer articleId = 
 DaoFacto
@@ -1943,26 +1943,10 @@
 ate(
-String.valueOf(
 id
-)
 , su
@@ -2045,24 +2045,18 @@
  icon);%0A
-        
+%09%09
 if (tags
@@ -2158,33 +2158,40 @@
 o.class).delete(
-i
+articleI
 d);%0A        for 
@@ -2586,17 +2586,24 @@
 s).save(
-i
+articleI
 d, tagId
@@ -2667,17 +2667,24 @@
 .delete(
-i
+articleI
 d);%0A    
@@ -2932,17 +2932,24 @@
 s).save(
-i
+articleI
 d, Integ

add category and tag editor.