org.tjm.setting.controller.VisibilityController.java Source code

Java tutorial

Introduction

Here is the source code for org.tjm.setting.controller.VisibilityController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tjm.setting.controller;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.annotation.Resource;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.tjm.identity.dao.FileIdentityDao;
import org.tjm.identity.entity.FileIdentity;
import org.tjm.identity.singleton.FileIdentityFactory;

/**
 *
 * @author larry
 */
@Controller
@RequestMapping("/setting/visibility")
public class VisibilityController {

    Logger logger = LoggerFactory.getLogger(VisibilityController.class);

    Properties props = new Properties();
    @Resource(name = "fileIdentityDao")
    private FileIdentityDao fileIdentityDao;

    //    @RequestMapping("/")
    //    public ModelAndView mainPage() {
    //        ModelAndView mav = new ModelAndView("/setting/visibility/main");
    //
    //        return mav;
    //    }
    /**
     * ???
     *
     * @param id
     * @return
     */
    @RequestMapping("/folder")
    @ResponseBody
    public String folder(String id) {
        JSONArray infoArray = new JSONArray();
        logger.warn(
                "---------------------------------------------- folder -------------------------------------------------------------");
        try {

            props.load(VisibilityController.class.getResourceAsStream("/config.properties"));

            // ?
            String onclick_path = "/";

            // id ? id window id  null ( ? id ? url )
            logger.warn("id = " + id);

            // (? ??)
            if (id != null && !id.equals("")) {
                FileIdentity fi = fileIdentityDao.findById(id);
                onclick_path = fi.getFilepath();
            } else { // ?

                // ????
                checkOutCheckInFile();
            }

            // ???
            String root_path = props.getProperty("root");
            File onclick_dir = new File(root_path + onclick_path);

            // ???
            File[] under_onclick_dir_files = onclick_dir.listFiles();

            // ??
            for (File file : under_onclick_dir_files) {

                // 
                if (file.isDirectory()) {

                    FileIdentity fi = fileIdentityDao.findByFilePath(file.getPath().replace(root_path, ""));
                    String file_id = fi.getId();

                    String this_file_path_without_onclick_path = file.getPath().replace(onclick_path, "");
                    String this_file_path_without_onclick_path_utf8 = URLEncoder.encode(
                            new String(this_file_path_without_onclick_path.getBytes("UTF-8"), "UTF-8"), "UTF-8");

                    JSONObject file_info = new JSONObject();
                    // ? id ??? id
                    file_info.put("id", file_id);
                    // ??
                    file_info.put("text", file.getName());
                    // 
                    file_info.put("state", "closed");
                    if (fi.getIsPublic()) {
                        file_info.put("checked", true);
                    } else {
                        file_info.put("checked", false);
                    }
                    // 
                    JSONObject attributes = new JSONObject();

                    attributes.put("path_name", this_file_path_without_onclick_path_utf8);
                    file_info.put("attributes", attributes);

                    // 
                    infoArray.add(file_info);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.warn(
                "---------------------------------------------- end of folder -------------------------------------------------------------");
        return infoArray.toString();
    }

    /**
     * ?
     */
    public void checkOutCheckInFile() {
        // FileIdentityFactory ??  Map 
        FileIdentityFactory.getInstance().refresh();
        // ? Map (????)
        Map<FileIdentity, String> identityMap = FileIdentityFactory.getInstance().getIdentityMap();
        Set<FileIdentity> keys = identityMap.keySet();
        // ? List
        List<FileIdentity> realFileIdentityList = new ArrayList<FileIdentity>();
        // ??
        for (FileIdentity fi : keys) {
            realFileIdentityList.add(fi);
        }
        // ? List
        List<FileIdentity> clone_realFileIdentityList = new ArrayList<FileIdentity>(realFileIdentityList);
        // DB List
        List<FileIdentity> dbFileIdentityList = fileIdentityDao.selectAll();
        //  ?  ?
        realFileIdentityList.removeAll(dbFileIdentityList);
        for (FileIdentity add_fileIdentity : realFileIdentityList) {
            logger.warn(
                    " : " + add_fileIdentity.getFilepath() + "?...");
            fileIdentityDao.save(add_fileIdentity);
            logger.warn(" : " + add_fileIdentity.getFilepath() + " ?...");
        }

        //    ??
        dbFileIdentityList.removeAll(clone_realFileIdentityList);

        for (FileIdentity delete_fileIdentity : dbFileIdentityList) {

            logger.warn(" : " + delete_fileIdentity.getFilepath()
                    + "?...");
            fileIdentityDao.remove(delete_fileIdentity.getId());
            logger.warn(" : " + delete_fileIdentity.getFilepath() + " ?...");
        }
    }

    @RequestMapping("/save")
    @ResponseBody
    public String save(String checked_ids, String unchecked_ids) {
        JSONObject result = new JSONObject();
        try {
            if (checked_ids != null && !checked_ids.equals("")) {
                String[] ids = checked_ids.split(",");
                for (String id : ids) {
                    FileIdentity fi = fileIdentityDao.findById(id);
                    fi.setIsPublic(Boolean.TRUE);
                    fileIdentityDao.update(fi);
                }
            }
            if (unchecked_ids != null && !unchecked_ids.equals("")) {
                String[] ids = unchecked_ids.split(",");
                for (String id : ids) {
                    FileIdentity fi = fileIdentityDao.findById(id);
                    fi.setIsPublic(Boolean.FALSE);
                    fileIdentityDao.update(fi);
                }
            }
        } catch (Exception e) {
            logger.error("Exception:", e);
            result.put("status", 500);
            result.put("message", "server error");
        }
        result.put("status", 200);
        result.put("message", "setting saved");
        return result.toString();
    }

}