Example usage for org.apache.commons.lang3 StringUtils defaultIfEmpty

List of usage examples for org.apache.commons.lang3 StringUtils defaultIfEmpty

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils defaultIfEmpty.

Prototype

public static <T extends CharSequence> T defaultIfEmpty(final T str, final T defaultStr) 

Source Link

Document

Returns either the passed in CharSequence, or if the CharSequence is empty or null , the value of defaultStr .

 StringUtils.defaultIfEmpty(null, "NULL")  = "NULL" StringUtils.defaultIfEmpty("", "NULL")    = "NULL" StringUtils.defaultIfEmpty(" ", "NULL")   = " " StringUtils.defaultIfEmpty("bat", "NULL") = "bat" StringUtils.defaultIfEmpty("", null)      = null 

Usage

From source file:software.coolstuff.springframework.owncloud.service.impl.rest.OwncloudRestUserServiceImpl.java

private void checkCreateUser(String authenticatedUser, String uri, Ocs.Meta meta, String username) {
    if ("ok".equals(meta.getStatus())) {
        return;//from ww w .ja  v a  2  s.c om
    }

    String exceptionMessage;
    switch (meta.getStatuscode()) {
    case 101:
        log.error("Error 101: {}", meta.getMessage());
        throw new IllegalArgumentException(meta.getMessage());
    case 102:
        log.warn("Error 102: User {} already exists", username);
        throw new OwncloudUsernameAlreadyExistsException(username);
    case 103:
        log.error("Error 103: {}", meta.getMessage());
        throw new IllegalStateException(meta.getMessage());
    case 997:
        exceptionMessage = String.format("User %s is not authorized to access Resource %s", authenticatedUser,
                uri);
        log.warn("Error 997: {}", exceptionMessage);
        throw new AccessDeniedException(exceptionMessage);
    default:
        exceptionMessage = String.format("Unknown Error Code %d. Reason: %s", meta.getStatuscode(),
                StringUtils.defaultIfEmpty(meta.getMessage(), ""));
        log.error(exceptionMessage);
        throw new IllegalStateException(exceptionMessage);
    }
}

From source file:software.coolstuff.springframework.owncloud.service.impl.rest.OwncloudRestUserServiceImpl.java

private void checkDeleteUser(String authenticatedUser, String uri, Ocs.Meta meta, String username) {
    if ("ok".equals(meta.getStatus())) {
        return;/*from  w  ww.  j a  v a 2 s .  co m*/
    }

    String exceptionMessage;
    switch (meta.getStatuscode()) {
    case 101:
        log.error("Error 101: User {} not found", username);
        throw new UsernameNotFoundException(username);
    case 997:
        exceptionMessage = String.format("User %s is not authorized to access Resource %s", authenticatedUser,
                uri);
        log.warn("Error 997: {}", exceptionMessage);
        throw new AccessDeniedException(exceptionMessage);
    default:
        exceptionMessage = String.format("Unknown Error Code %d. Reason: %s", meta.getStatuscode(),
                StringUtils.defaultIfEmpty(meta.getMessage(), ""));
        log.error(exceptionMessage);
        throw new IllegalStateException(exceptionMessage);
    }
}

From source file:solidbase.core.plugins.DBReader.java

public DBReader(ResultSet result, ExportLogger counter, boolean dateAsTimestamp) throws SQLException {
    this.result = result;
    this.counter = counter;

    ResultSetMetaData metaData = result.getMetaData();
    int count = metaData.getColumnCount();
    this.columns = new Column[count];

    for (int i = 0; i < count; i++) {
        int col = i + 1;
        String name = metaData.getColumnName(col).toUpperCase();
        int type = metaData.getColumnType(col);
        if (type == Types.DATE && dateAsTimestamp)
            type = Types.TIMESTAMP;
        String table = StringUtils.upperCase(StringUtils.defaultIfEmpty(metaData.getTableName(col), null));
        String schema = StringUtils.upperCase(StringUtils.defaultIfEmpty(metaData.getSchemaName(col), null));
        this.columns[i] = new Column(name, type, table, schema);
    }//w ww . j  a va  2s  .  c  o  m
}

From source file:tw.edu.chit.struts.action.course.ReportPrintAction.java

/**
 * //from www .j  a va 2  s.  c  o  m
 * 
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @param sterm
 * @throws Exception
 */
@SuppressWarnings("unchecked")
private void printStayTimePrint(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response, String sterm) throws Exception {

    HttpSession session = request.getSession(false);
    MemberManager mm = (MemberManager) getBean(IConstants.MEMBER_MANAGER_BEAN_NAME);
    CourseManager cm = (CourseManager) getBean(IConstants.COURSE_MANAGER_BEAN_NAME);
    ScoreManager sm = (ScoreManager) getBean(IConstants.SCORE_MANAGER_BEAN_NAME);
    ServletContext context = request.getSession().getServletContext();
    Integer year = cm.getSchoolYear();
    String term = form.getString("sterm");
    List<Clazz> clazzes = sm.findClassBy(new Clazz(processClassInfo(form)),
            getUserCredential(session).getClassInChargeAry(), true);

    if (!clazzes.isEmpty()) {

        File templateXLS = new File(context.getRealPath("/WEB-INF/reports/TeachSchedAll.xls"));
        HSSFWorkbook workbook = Toolket.getHSSFWorkbook(templateXLS);

        HSSFFont fontSize12 = workbook.createFont();
        fontSize12.setFontHeightInPoints((short) 12);
        fontSize12.setFontName("Arial Unicode MS");

        HSSFSheet sheet = null;
        int sheetIndex = 0, colOffset = 1, col = 0;
        boolean isLocationNull = false;
        String departClass = null;
        Dtime dtime = null;
        Empl empl = null;
        Set<String> idnoSet = new HashSet<String>();
        Short colorForStayTime = HSSFColor.AUTOMATIC.index;
        Short colorForLifeCounseling = HSSFColor.LIGHT_GREEN.index;
        List<TeacherStayTime> tsts = null;
        List<LifeCounseling> lcs = null;
        List<Dtime> dtimes = null;
        List<Map> map = null;
        Map content = null;

        for (Clazz clazz : clazzes) {
            departClass = clazz.getClassNo();

            dtime = new Dtime();
            dtime.setDepartClass(departClass);
            dtime.setSterm(sterm);
            dtimes = cm.findDtimeBy(dtime, "cscode");
            if (!dtimes.isEmpty()) {
                for (Dtime d : dtimes) {
                    if (StringUtils.isNotBlank(d.getTechid()))
                        idnoSet.add(d.getTechid());
                }
            }
        }

        for (String idno : idnoSet) {
            empl = mm.findEmplByIdno(idno);
            if (empl != null && "1".equalsIgnoreCase(empl.getCategory())) {

                sheet = workbook.getSheetAt(sheetIndex);
                workbook.setSheetName(sheetIndex++, empl.getCname());
                isLocationNull = empl.getLocation() == null;

                Toolket.setCellValue(sheet, 0, 1, year + "" + term + "" + empl.getCname()
                        + "?" + " (:"
                        + (isLocationNull ? ""
                                : StringUtils.defaultIfEmpty(empl.getLocation().getExtension(), ""))
                        + " ?:"
                        + (isLocationNull ? "" : StringUtils.defaultIfEmpty(empl.getLocation().getRoomId(), ""))
                        + ")");
                map = cm.findCourseByTeacherTermWeekdaySched(empl.getIdno(), term.toString());

                for (int i = 0; i < 14; i++) {
                    for (int j = 0; j < 7; j++) {
                        content = map.get(j * 15 + i);
                        if (!CollectionUtils.isEmpty(content)) {
                            Toolket.setCellValue(sheet, i + 2, j + 2, (String) content.get("ClassName") + "\n"
                                    + (String) content.get("chi_name") + "\n" + (String) content.get("place"));
                        }
                    }
                }
                List<TeacherStayTime> myTsts = cm.ezGetBy(
                        " Select Week, Node1, Node2, Node3, Node4, Node5, Node6, Node7, Node8, Node9, Node10, "
                                + "        Node11, Node12, Node13, Node14 " + " From TeacherStayTime "
                                + " Where SchoolYear='" + year + "'" + "   And SchoolTerm='" + term + "' "
                                + "   And parentOid='" + empl.getOid() + "'");

                List myTsts2 = new ArrayList();

                //int colOffset = 1, col = 0;
                for (int i = 0; i < myTsts.size(); i++) {
                    //for (TeacherStayTime tst : tsts) {            
                    myTsts2.add(myTsts.get(i));
                    String s = myTsts2.get(i).toString();
                    col = Integer.parseInt(s.substring(6, 7)) + colOffset; //Week            
                    //if (tst.getNode1() != null && tst.getNode1() == 1) {            
                    if (Integer.parseInt(s.substring(15, 16)) == 1) { //Node1
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 2, col)))
                            Toolket.setCellValue(workbook, sheet, 2, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(24, 25)) == 1) { //Node2
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 3, col)))
                            Toolket.setCellValue(workbook, sheet, 3, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(33, 34)) == 1) { //Node3
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 4, col)))
                            Toolket.setCellValue(workbook, sheet, 4, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(42, 43)) == 1) { //Node4
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 5, col)))
                            Toolket.setCellValue(workbook, sheet, 5, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(51, 52)) == 1) { //Node5
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 6, col)))
                            Toolket.setCellValue(workbook, sheet, 6, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(60, 61)) == 1) { //Node6
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 7, col)))
                            Toolket.setCellValue(workbook, sheet, 7, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(69, 70)) == 1) { //Node7
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 8, col)))
                            Toolket.setCellValue(workbook, sheet, 8, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(78, 79)) == 1) { //Node8
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 9, col)))
                            Toolket.setCellValue(workbook, sheet, 9, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(87, 88)) == 1) { //Node9
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 10, col)))
                            Toolket.setCellValue(workbook, sheet, 10, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(97, 98)) == 1) { //Node10
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 11, col)))
                            Toolket.setCellValue(workbook, sheet, 11, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(107, 108)) == 1) { //Node11
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 12, col)))
                            Toolket.setCellValue(workbook, sheet, 12, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(117, 118)) == 1) { //Node12
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 13, col)))
                            Toolket.setCellValue(workbook, sheet, 13, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(127, 128)) == 1) { //Node13
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 14, col)))
                            Toolket.setCellValue(workbook, sheet, 14, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (Integer.parseInt(s.substring(137, 138)) == 1) { //Node14
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 15, col)))
                            Toolket.setCellValue(workbook, sheet, 15, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                }

                //List<LifeCounseling> lcs = empl.getLifeCounseling();
                List<LifeCounseling> myLcs = cm.ezGetBy(
                        " Select Week, Node1, Node2, Node3, Node4, Node5, Node6, Node7, Node8, Node9, Node10, "
                                + "        Node11, Node12, Node13, Node14 "
                                + " From LifeCounseling Where ParentOid='" + empl.getOid() + "'");

                List myLcs2 = new ArrayList();
                colOffset = 1;
                col = 0;
                //for (LifeCounseling lc : lcs) {
                for (int y = 0; y < myLcs.size(); y++) {

                    myLcs2.add(myLcs.get(y));
                    String st = myLcs2.get(y).toString();
                    col = Integer.parseInt(st.substring(6, 7)) + colOffset;
                    //col = lc.getWeek() + colOffset;
                    //if (lc.getNode1() != null && lc.getNode1() == 1) {
                    if (Integer.parseInt(st.substring(15, 16)) == 1) { //Node1
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 2, col)))
                            Toolket.setCellValue(workbook, sheet, 2, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode2() != null && lc.getNode2() == 1) {
                    if (Integer.parseInt(st.substring(24, 25)) == 1) { //Node2
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 3, col)))
                            Toolket.setCellValue(workbook, sheet, 3, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode3() != null && lc.getNode3() == 1) {
                    if (Integer.parseInt(st.substring(33, 34)) == 1) { //Node3
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 4, col)))
                            Toolket.setCellValue(workbook, sheet, 4, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode4() != null && lc.getNode4() == 1) {
                    if (Integer.parseInt(st.substring(42, 43)) == 1) { //Node4
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 5, col)))
                            Toolket.setCellValue(workbook, sheet, 5, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode5() != null && lc.getNode5() == 1) {
                    if (Integer.parseInt(st.substring(51, 52)) == 1) { //Node5
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 6, col)))
                            Toolket.setCellValue(workbook, sheet, 6, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode6() != null && lc.getNode6() == 1) {
                    if (Integer.parseInt(st.substring(60, 61)) == 1) { //Node6
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 7, col)))
                            Toolket.setCellValue(workbook, sheet, 7, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode7() != null && lc.getNode7() == 1) {
                    if (Integer.parseInt(st.substring(69, 70)) == 1) { //Node7
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 8, col)))
                            Toolket.setCellValue(workbook, sheet, 8, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode8() != null && lc.getNode8() == 1) {
                    if (Integer.parseInt(st.substring(78, 79)) == 1) { //Node8
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 9, col)))
                            Toolket.setCellValue(workbook, sheet, 9, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode9() != null && lc.getNode9() == 1) {
                    if (Integer.parseInt(st.substring(87, 88)) == 1) { //Node9
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 10, col)))
                            Toolket.setCellValue(workbook, sheet, 10, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode10() != null && lc.getNode10() == 1) {
                    if (Integer.parseInt(st.substring(97, 98)) == 1) { //Node10
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 11, col)))
                            Toolket.setCellValue(workbook, sheet, 11, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode11() != null && lc.getNode11() == 1) {
                    if (Integer.parseInt(st.substring(107, 108)) == 1) { //Node11
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 12, col)))
                            Toolket.setCellValue(workbook, sheet, 12, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode12() != null && lc.getNode12() == 1) {
                    if (Integer.parseInt(st.substring(117, 118)) == 1) { //Node12
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 13, col)))
                            Toolket.setCellValue(workbook, sheet, 13, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode13() != null && lc.getNode13() == 1) {
                    if (Integer.parseInt(st.substring(127, 128)) == 1) { //Node13
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 14, col)))
                            Toolket.setCellValue(workbook, sheet, 14, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode14() != null && lc.getNode14() == 1) {
                    if (Integer.parseInt(st.substring(137, 138)) == 1) { //Node14
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 15, col)))
                            Toolket.setCellValue(workbook, sheet, 15, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                }
            }
        }
        //===================================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        /*
        tsts = empl.getStayTime();
        for (TeacherStayTime tst : tsts) {
          col = tst.getWeek() + colOffset;
          if (tst.getNode1() != null && tst.getNode1() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   2, col)))
                Toolket.setCellValue(workbook, sheet, 2, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode2() != null && tst.getNode2() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   3, col)))
                Toolket.setCellValue(workbook, sheet, 3, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode3() != null && tst.getNode3() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   4, col)))
                Toolket.setCellValue(workbook, sheet, 4, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode4() != null && tst.getNode4() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   5, col)))
                Toolket.setCellValue(workbook, sheet, 5, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode5() != null && tst.getNode5() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   6, col)))
                Toolket.setCellValue(workbook, sheet, 6, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode6() != null && tst.getNode6() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   7, col)))
                Toolket.setCellValue(workbook, sheet, 7, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode7() != null && tst.getNode7() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   8, col)))
                Toolket.setCellValue(workbook, sheet, 8, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode8() != null && tst.getNode8() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   9, col)))
                Toolket.setCellValue(workbook, sheet, 9, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode9() != null && tst.getNode9() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   10, col)))
                Toolket.setCellValue(workbook, sheet, 10, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode10() != null && tst.getNode10() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   11, col)))
                Toolket.setCellValue(workbook, sheet, 11, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode11() != null && tst.getNode11() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   12, col)))
                Toolket.setCellValue(workbook, sheet, 12, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode12() != null && tst.getNode12() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   13, col)))
                Toolket.setCellValue(workbook, sheet, 13, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode13() != null && tst.getNode13() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   14, col)))
                Toolket.setCellValue(workbook, sheet, 14, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
          if (tst.getNode14() != null && tst.getNode14() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   15, col)))
                Toolket.setCellValue(workbook, sheet, 15, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForStayTime);
          }
        }
                
        lcs = empl.getLifeCounseling();
        colOffset = 1;
        col = 0;
        for (LifeCounseling lc : lcs) {
          col = lc.getWeek() + colOffset;
          if (lc.getNode1() != null && lc.getNode1() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   2, col)))
                Toolket.setCellValue(workbook, sheet, 2, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode2() != null && lc.getNode2() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   3, col)))
                Toolket.setCellValue(workbook, sheet, 3, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode3() != null && lc.getNode3() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   4, col)))
                Toolket.setCellValue(workbook, sheet, 4, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode4() != null && lc.getNode4() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   5, col)))
                Toolket.setCellValue(workbook, sheet, 5, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode5() != null && lc.getNode5() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   6, col)))
                Toolket.setCellValue(workbook, sheet, 6, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode6() != null && lc.getNode6() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   7, col)))
                Toolket.setCellValue(workbook, sheet, 7, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode7() != null && lc.getNode7() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   8, col)))
                Toolket.setCellValue(workbook, sheet, 8, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode8() != null && lc.getNode8() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   9, col)))
                Toolket.setCellValue(workbook, sheet, 9, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode9() != null && lc.getNode9() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   10, col)))
                Toolket.setCellValue(workbook, sheet, 10, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode10() != null && lc.getNode10() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   11, col)))
                Toolket.setCellValue(workbook, sheet, 11, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode11() != null && lc.getNode11() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   12, col)))
                Toolket.setCellValue(workbook, sheet, 12, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode12() != null && lc.getNode12() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   13, col)))
                Toolket.setCellValue(workbook, sheet, 13, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode13() != null && lc.getNode13() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   14, col)))
                Toolket.setCellValue(workbook, sheet, 14, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
          if (lc.getNode14() != null && lc.getNode14() == 1) {
             if (StringUtils.isEmpty(Toolket.getCellValue(sheet,
                   15, col)))
                Toolket.setCellValue(workbook, sheet, 15, col,
                      "", fontSize12,
                      HSSFCellStyle.ALIGN_CENTER, true,
                      colorForLifeCounseling);
          }
        }
                
        }  */
        //=================================>>>>>>>>>>>>>>>>>>>>>

        File tempDir = new File(
                context.getRealPath("/WEB-INF/reports/temp/" + getUserCredential(session).getMember().getIdno()
                        + (new SimpleDateFormat("yyyyMMdd").format(new Date()))));
        if (!tempDir.exists())
            tempDir.mkdirs();

        File output = new File(tempDir, "StayTimeList.xls");
        FileOutputStream fos = new FileOutputStream(output);
        workbook.write(fos);
        fos.close();

        JasperReportUtils.printXlsToFrontEnd(response, output);
        output.delete();
        tempDir.delete();

    }
}

From source file:tw.edu.chit.struts.action.deptassist.ReportPrintAction.java

/**
 * ?/*from www . j a v  a  2  s.  com*/
 * 
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @param sterm
 * @throws Exception
 */
@SuppressWarnings("unchecked")
private void printStayTimePrint(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response, String sterm) throws Exception {

    HttpSession session = request.getSession(false);
    MemberManager mm = (MemberManager) getBean(IConstants.MEMBER_MANAGER_BEAN_NAME);
    CourseManager cm = (CourseManager) getBean(IConstants.COURSE_MANAGER_BEAN_NAME);
    ScoreManager sm = (ScoreManager) getBean(IConstants.SCORE_MANAGER_BEAN_NAME);
    ServletContext context = request.getSession().getServletContext();
    Integer year = Integer.valueOf(cm.getNowBy("School_year"));
    String term = form.getString("sterm");
    List<Clazz> clazzes = sm.findClassBy(new Clazz(processClassInfo(form)),
            getUserCredential(session).getClassInChargeAry(), true);

    if (!clazzes.isEmpty()) {

        File templateXLS = new File(context.getRealPath("/WEB-INF/reports/TeachSchedAll.xls"));
        HSSFWorkbook workbook = Toolket.getHSSFWorkbook(templateXLS);

        HSSFFont fontSize12 = workbook.createFont();
        fontSize12.setFontHeightInPoints((short) 12);
        fontSize12.setFontName("Arial Unicode MS");

        HSSFSheet sheet = null;
        int sheetIndex = 0, colOffset = 1, col = 0;
        boolean isLocationNull = false;
        String departClass = null;
        Dtime dtime = null;
        Empl empl = null;
        Set<String> idnoSet = new HashSet<String>();
        Short colorForStayTime = HSSFColor.AUTOMATIC.index;
        Short colorForLifeCounseling = HSSFColor.LIGHT_GREEN.index;
        List<TeacherStayTime> tsts = null;
        List<LifeCounseling> lcs = null;
        List<Dtime> dtimes = null;
        List<Map> map = null;
        Map content = null;

        for (Clazz clazz : clazzes) {
            departClass = clazz.getClassNo();

            dtime = new Dtime();
            dtime.setDepartClass(departClass);
            dtime.setSterm(sterm);
            dtimes = cm.findDtimeBy(dtime, "cscode");
            if (!dtimes.isEmpty()) {
                for (Dtime d : dtimes) {
                    if (StringUtils.isNotBlank(d.getTechid()))
                        idnoSet.add(d.getTechid());
                }
            }
        }

        for (String idno : idnoSet) {
            empl = mm.findEmplByIdno(idno);
            if (empl != null && "1".equalsIgnoreCase(empl.getCategory())) {

                sheet = workbook.getSheetAt(sheetIndex);
                workbook.setSheetName(sheetIndex++, empl.getCname());
                isLocationNull = empl.getLocation() == null;

                Toolket.setCellValue(sheet, 0, 1, year + "" + term + "" + empl.getCname()
                        + "?" + " (:"
                        + (isLocationNull ? ""
                                : StringUtils.defaultIfEmpty(empl.getLocation().getExtension(), ""))
                        + " ?:"
                        + (isLocationNull ? "" : StringUtils.defaultIfEmpty(empl.getLocation().getRoomId(), ""))
                        + ")");

                map = cm.findCourseByTeacherTermWeekdaySched(empl.getIdno(), term.toString());

                for (int i = 0; i < 14; i++) {
                    for (int j = 0; j < 7; j++) {
                        content = map.get(j * 15 + i);
                        if (!CollectionUtils.isEmpty(content)) {
                            Toolket.setCellValue(sheet, i + 2, j + 2, (String) content.get("ClassName") + "\n"
                                    + (String) content.get("chi_name") + "\n" + (String) content.get("place"));
                        }
                    }
                }

                //tsts = empl.getStayTime();
                List<TeacherStayTime> myTsts = cm.ezGetBy(
                        " Select Week, Node1, Node2, Node3, Node4, Node5, Node6, Node7, Node8, Node9, Node10, "
                                + "        Node11, Node12, Node13, Node14 " + " From TeacherStayTime "
                                + " Where SchoolYear='" + year + "'" + "   And SchoolTerm='" + term + "' "
                                + "   And parentOid='" + empl.getOid() + "'");

                List myTsts2 = new ArrayList();

                for (int i = 0; i < myTsts.size(); i++) {
                    //for (TeacherStayTime tst : tsts) {                                    
                    myTsts2.add(myTsts.get(i));
                    String s = myTsts2.get(i).toString();
                    col = Integer.parseInt(s.substring(6, 7)) + colOffset; //Week   
                    //col = tst.getWeek() + colOffset;
                    //if (tst.getNode1() != null && tst.getNode1() == 1) {                  
                    if (Integer.parseInt(s.substring(15, 16)) == 1) { //Node1
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 2, col)))
                            Toolket.setCellValue(workbook, sheet, 2, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode2() != null && tst.getNode2() == 1) {
                    if (Integer.parseInt(s.substring(24, 25)) == 1) { //Node2
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 3, col)))
                            Toolket.setCellValue(workbook, sheet, 3, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode3() != null && tst.getNode3() == 1) {
                    if (Integer.parseInt(s.substring(33, 34)) == 1) { //Node3
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 4, col)))
                            Toolket.setCellValue(workbook, sheet, 4, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode4() != null && tst.getNode4() == 1) {
                    if (Integer.parseInt(s.substring(42, 43)) == 1) { //Node4
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 5, col)))
                            Toolket.setCellValue(workbook, sheet, 5, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode5() != null && tst.getNode5() == 1) {
                    if (Integer.parseInt(s.substring(51, 52)) == 1) { //Node5
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 6, col)))
                            Toolket.setCellValue(workbook, sheet, 6, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode6() != null && tst.getNode6() == 1) {
                    if (Integer.parseInt(s.substring(60, 61)) == 1) { //Node6
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 7, col)))
                            Toolket.setCellValue(workbook, sheet, 7, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode7() != null && tst.getNode7() == 1) {
                    if (Integer.parseInt(s.substring(69, 70)) == 1) { //Node7
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 8, col)))
                            Toolket.setCellValue(workbook, sheet, 8, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode8() != null && tst.getNode8() == 1) {
                    if (Integer.parseInt(s.substring(78, 79)) == 1) { //Node8
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 9, col)))
                            Toolket.setCellValue(workbook, sheet, 9, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode9() != null && tst.getNode9() == 1) {
                    if (Integer.parseInt(s.substring(87, 88)) == 1) { //Node9
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 10, col)))
                            Toolket.setCellValue(workbook, sheet, 10, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode10() != null && tst.getNode10() == 1) {
                    if (Integer.parseInt(s.substring(97, 98)) == 1) { //Node10
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 11, col)))
                            Toolket.setCellValue(workbook, sheet, 11, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode11() != null && tst.getNode11() == 1) {
                    if (Integer.parseInt(s.substring(107, 108)) == 1) { //Node11
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 12, col)))
                            Toolket.setCellValue(workbook, sheet, 12, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode12() != null && tst.getNode12() == 1) {
                    if (Integer.parseInt(s.substring(117, 118)) == 1) { //Node12
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 13, col)))
                            Toolket.setCellValue(workbook, sheet, 13, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode13() != null && tst.getNode13() == 1) {
                    if (Integer.parseInt(s.substring(127, 128)) == 1) { //Node13
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 14, col)))
                            Toolket.setCellValue(workbook, sheet, 14, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode14() != null && tst.getNode14() == 1) {
                    if (Integer.parseInt(s.substring(137, 138)) == 1) { //Node14
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 15, col)))
                            Toolket.setCellValue(workbook, sheet, 15, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                }

                //lcs = empl.getLifeCounseling();
                List<LifeCounseling> myLcs = cm.ezGetBy(
                        " Select Week, Node1, Node2, Node3, Node4, Node5, Node6, Node7, Node8, Node9, Node10, "
                                + "        Node11, Node12, Node13, Node14 "
                                + " From LifeCounseling Where ParentOid='" + empl.getOid() + "'");
                List myLcs2 = new ArrayList();

                colOffset = 1;
                col = 0;
                //for (LifeCounseling lc : lcs) {
                for (int y = 0; y < myLcs.size(); y++) {
                    myLcs2.add(myLcs.get(y));
                    String st = myLcs2.get(y).toString();
                    col = Integer.parseInt(st.substring(6, 7)) + colOffset;
                    //col = lc.getWeek() + colOffset;                                 
                    //if (lc.getNode1() != null && lc.getNode1() == 1) {
                    if (Integer.parseInt(st.substring(15, 16)) == 1) { //Node1
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 2, col)))
                            Toolket.setCellValue(workbook, sheet, 2, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode2() != null && lc.getNode2() == 1) {
                    if (Integer.parseInt(st.substring(24, 25)) == 1) { //Node2
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 3, col)))
                            Toolket.setCellValue(workbook, sheet, 3, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode3() != null && lc.getNode3() == 1) {
                    if (Integer.parseInt(st.substring(33, 34)) == 1) { //Node3
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 4, col)))
                            Toolket.setCellValue(workbook, sheet, 4, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode4() != null && lc.getNode4() == 1) {
                    if (Integer.parseInt(st.substring(42, 43)) == 1) { //Node4
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 5, col)))
                            Toolket.setCellValue(workbook, sheet, 5, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode5() != null && lc.getNode5() == 1) {
                    if (Integer.parseInt(st.substring(51, 52)) == 1) { //Node5
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 6, col)))
                            Toolket.setCellValue(workbook, sheet, 6, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode6() != null && lc.getNode6() == 1) {
                    if (Integer.parseInt(st.substring(60, 61)) == 1) { //Node6
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 7, col)))
                            Toolket.setCellValue(workbook, sheet, 7, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode7() != null && lc.getNode7() == 1) {
                    if (Integer.parseInt(st.substring(69, 70)) == 1) { //Node7
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 8, col)))
                            Toolket.setCellValue(workbook, sheet, 8, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode8() != null && lc.getNode8() == 1) {
                    if (Integer.parseInt(st.substring(78, 79)) == 1) { //Node8
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 9, col)))
                            Toolket.setCellValue(workbook, sheet, 9, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode9() != null && lc.getNode9() == 1) {
                    if (Integer.parseInt(st.substring(87, 88)) == 1) { //Node9
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 10, col)))
                            Toolket.setCellValue(workbook, sheet, 10, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode10() != null && lc.getNode10() == 1) {
                    if (Integer.parseInt(st.substring(97, 98)) == 1) { //Node10
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 11, col)))
                            Toolket.setCellValue(workbook, sheet, 11, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode11() != null && lc.getNode11() == 1) {
                    if (Integer.parseInt(st.substring(107, 108)) == 1) { //Node11
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 12, col)))
                            Toolket.setCellValue(workbook, sheet, 12, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode12() != null && lc.getNode12() == 1) {
                    if (Integer.parseInt(st.substring(117, 118)) == 1) { //Node12
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 13, col)))
                            Toolket.setCellValue(workbook, sheet, 13, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode13() != null && lc.getNode13() == 1) {
                    if (Integer.parseInt(st.substring(127, 128)) == 1) { //Node13
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 14, col)))
                            Toolket.setCellValue(workbook, sheet, 14, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode14() != null && lc.getNode14() == 1) {
                    if (Integer.parseInt(st.substring(137, 138)) == 1) { //Node14
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 15, col)))
                            Toolket.setCellValue(workbook, sheet, 15, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                }

            }
        }

        File tempDir = new File(
                context.getRealPath("/WEB-INF/reports/temp/" + getUserCredential(session).getMember().getIdno()
                        + (new SimpleDateFormat("yyyyMMdd").format(new Date()))));
        if (!tempDir.exists())
            tempDir.mkdirs();

        File output = new File(tempDir, "StayTimeList.xls");
        FileOutputStream fos = new FileOutputStream(output);
        workbook.write(fos);
        fos.close();

        JasperReportUtils.printXlsToFrontEnd(response, output);
        output.delete();
        tempDir.delete();

    }
}

From source file:tw.edu.chit.struts.action.deptassist.ReportPrintAction.java

/**
 * ?//w  w  w.java 2  s.  co m
 * 
 * @param mapping org.apache.struts.action.ActionMapping object
 * @param form org.apache.struts.action.ActionForm object
 * @param request request javax.servlet.http.HttpServletRequest object
 * @param response response javax.servlet.http.HttpServletResponse object
 * @param sterm 
 */
@SuppressWarnings("unchecked")
private void printCscodeList(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response, String sterm) throws Exception {

    HttpSession session = request.getSession(false);
    AdminManager am = (AdminManager) getBean(IConstants.ADMIN_MANAGER_BEAN_NAME);
    ServletContext context = request.getSession().getServletContext();
    Example example = Example.create(new Csno());
    List<Order> orders = new LinkedList<Order>();
    orders.add(Order.asc("cscode"));
    List<Csno> csnos = (List<Csno>) am.findSQLWithCriteria(Csno.class, example, null, orders);

    File templateXLS = new File(context.getRealPath("/WEB-INF/reports/Csnos.xls"));
    HSSFWorkbook workbook = Toolket.getHSSFWorkbook(templateXLS);
    HSSFSheet sheet = workbook.getSheetAt(0);

    int index = 2;

    for (Csno csno : csnos) {
        Toolket.setCellValue(sheet, index, 0, csno.getCscode().trim().toUpperCase());
        Toolket.setCellValue(sheet, index, 1, csno.getChiName().trim());
        Toolket.setCellValue(sheet, index++, 2, StringUtils.defaultIfEmpty(csno.getEngName(), "").trim());
    }

    File tempDir = new File(
            context.getRealPath("/WEB-INF/reports/temp/" + getUserCredential(session).getMember().getIdno()
                    + (new SimpleDateFormat("yyyyMMdd").format(new Date()))));
    if (!tempDir.exists())
        tempDir.mkdirs();

    File output = new File(tempDir, "Csnos.xls");
    FileOutputStream fos = new FileOutputStream(output);
    workbook.write(fos);
    fos.close();

    JasperReportUtils.printXlsToFrontEnd(response, output);
    output.delete();
    tempDir.delete();
}

From source file:tw.edu.chit.struts.action.registration.ReportPrintAction.java

/**
 * ?// w  w  w.jav  a2  s.  co  m
 * 
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @param sterm
 * @throws Exception
 */
@SuppressWarnings("unchecked")
private void printStayTimePrint(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response, String sterm) throws Exception {

    HttpSession session = request.getSession(false);
    MemberManager mm = (MemberManager) getBean(IConstants.MEMBER_MANAGER_BEAN_NAME);
    CourseManager cm = (CourseManager) getBean(IConstants.COURSE_MANAGER_BEAN_NAME);
    ScoreManager sm = (ScoreManager) getBean(IConstants.SCORE_MANAGER_BEAN_NAME);
    ServletContext context = request.getSession().getServletContext();
    Integer year = Integer.valueOf(cm.getNowBy("School_year"));
    String term = form.getString("sterm");
    // Short dayCode = Short.valueOf(form.getString("dayCode"));
    // String nodeCode = form.getString("nodeCode");
    List<Clazz> clazzes = sm.findClassBy(new Clazz(processClassInfo(form)),
            getUserCredential(session).getClassInChargeAry(), true);

    if (!clazzes.isEmpty()) {

        File templateXLS = new File(context.getRealPath("/WEB-INF/reports/TeachSchedAll.xls"));
        HSSFWorkbook workbook = Toolket.getHSSFWorkbook(templateXLS);

        HSSFFont fontSize12 = workbook.createFont();
        fontSize12.setFontHeightInPoints((short) 12);
        fontSize12.setFontName("Arial Unicode MS");

        HSSFSheet sheet = null;
        int sheetIndex = 0, colOffset = 1, col = 0;
        boolean isLocationNull = false;
        String departClass = null;
        Dtime dtime = null;
        Empl empl = null;
        Set<String> idnoSet = new HashSet<String>();
        Short colorForStayTime = HSSFColor.AUTOMATIC.index;
        Short colorForLifeCounseling = HSSFColor.LIGHT_GREEN.index;
        List<TeacherStayTime> tsts = null;
        List<LifeCounseling> lcs = null;
        List<Dtime> dtimes = null;
        List<Map> map = null;
        Map content = null;

        for (Clazz clazz : clazzes) {
            departClass = clazz.getClassNo();

            dtime = new Dtime();
            dtime.setDepartClass(departClass);
            dtime.setSterm(sterm);
            dtimes = cm.findDtimeBy(dtime, "cscode");
            if (!dtimes.isEmpty()) {
                for (Dtime d : dtimes) {
                    if (StringUtils.isNotBlank(d.getTechid()))
                        idnoSet.add(d.getTechid());
                }
            }
        }

        for (String idno : idnoSet) {
            empl = mm.findEmplByIdno(idno);
            if (empl != null && "1".equalsIgnoreCase(empl.getCategory())) {

                sheet = workbook.getSheetAt(sheetIndex);
                workbook.setSheetName(sheetIndex++, empl.getCname());
                isLocationNull = empl.getLocation() == null;

                Toolket.setCellValue(sheet, 0, 1, year + "" + term + "" + empl.getCname()
                        + "?" + " (:"
                        + (isLocationNull ? ""
                                : StringUtils.defaultIfEmpty(empl.getLocation().getExtension(), ""))
                        + " ?:"
                        + (isLocationNull ? "" : StringUtils.defaultIfEmpty(empl.getLocation().getRoomId(), ""))
                        + ")");
                map = cm.findCourseByTeacherTermWeekdaySched(empl.getIdno(), term.toString());

                for (int i = 0; i < 14; i++) {
                    for (int j = 0; j < 7; j++) {
                        content = map.get(j * 15 + i);
                        if (!CollectionUtils.isEmpty(content)) {
                            Toolket.setCellValue(sheet, i + 2, j + 2, (String) content.get("ClassName") + "\n"
                                    + (String) content.get("chi_name") + "\n" + (String) content.get("place"));
                        }
                    }
                }

                tsts = empl.getStayTime();
                for (TeacherStayTime tst : tsts) {
                    col = tst.getWeek() + colOffset;
                    if (tst.getNode1() != null && tst.getNode1() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 2, col)))
                            Toolket.setCellValue(workbook, sheet, 2, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode2() != null && tst.getNode2() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 3, col)))
                            Toolket.setCellValue(workbook, sheet, 3, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode3() != null && tst.getNode3() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 4, col)))
                            Toolket.setCellValue(workbook, sheet, 4, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode4() != null && tst.getNode4() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 5, col)))
                            Toolket.setCellValue(workbook, sheet, 5, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode5() != null && tst.getNode5() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 6, col)))
                            Toolket.setCellValue(workbook, sheet, 6, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode6() != null && tst.getNode6() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 7, col)))
                            Toolket.setCellValue(workbook, sheet, 7, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode7() != null && tst.getNode7() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 8, col)))
                            Toolket.setCellValue(workbook, sheet, 8, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode8() != null && tst.getNode8() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 9, col)))
                            Toolket.setCellValue(workbook, sheet, 9, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode9() != null && tst.getNode9() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 10, col)))
                            Toolket.setCellValue(workbook, sheet, 10, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode10() != null && tst.getNode10() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 11, col)))
                            Toolket.setCellValue(workbook, sheet, 11, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode11() != null && tst.getNode11() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 12, col)))
                            Toolket.setCellValue(workbook, sheet, 12, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode12() != null && tst.getNode12() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 13, col)))
                            Toolket.setCellValue(workbook, sheet, 13, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode13() != null && tst.getNode13() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 14, col)))
                            Toolket.setCellValue(workbook, sheet, 14, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    if (tst.getNode14() != null && tst.getNode14() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 15, col)))
                            Toolket.setCellValue(workbook, sheet, 15, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                }

                lcs = empl.getLifeCounseling();
                colOffset = 1;
                col = 0;
                for (LifeCounseling lc : lcs) {
                    col = lc.getWeek() + colOffset;
                    if (lc.getNode1() != null && lc.getNode1() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 2, col)))
                            Toolket.setCellValue(workbook, sheet, 2, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode2() != null && lc.getNode2() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 3, col)))
                            Toolket.setCellValue(workbook, sheet, 3, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode3() != null && lc.getNode3() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 4, col)))
                            Toolket.setCellValue(workbook, sheet, 4, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode4() != null && lc.getNode4() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 5, col)))
                            Toolket.setCellValue(workbook, sheet, 5, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode5() != null && lc.getNode5() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 6, col)))
                            Toolket.setCellValue(workbook, sheet, 6, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode6() != null && lc.getNode6() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 7, col)))
                            Toolket.setCellValue(workbook, sheet, 7, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode7() != null && lc.getNode7() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 8, col)))
                            Toolket.setCellValue(workbook, sheet, 8, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode8() != null && lc.getNode8() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 9, col)))
                            Toolket.setCellValue(workbook, sheet, 9, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode9() != null && lc.getNode9() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 10, col)))
                            Toolket.setCellValue(workbook, sheet, 10, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode10() != null && lc.getNode10() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 11, col)))
                            Toolket.setCellValue(workbook, sheet, 11, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode11() != null && lc.getNode11() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 12, col)))
                            Toolket.setCellValue(workbook, sheet, 12, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode12() != null && lc.getNode12() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 13, col)))
                            Toolket.setCellValue(workbook, sheet, 13, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode13() != null && lc.getNode13() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 14, col)))
                            Toolket.setCellValue(workbook, sheet, 14, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    if (lc.getNode14() != null && lc.getNode14() == 1) {
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 15, col)))
                            Toolket.setCellValue(workbook, sheet, 15, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                }

            }
        }

        File tempDir = new File(
                context.getRealPath("/WEB-INF/reports/temp/" + getUserCredential(session).getMember().getIdno()
                        + (new SimpleDateFormat("yyyyMMdd").format(new Date()))));
        if (!tempDir.exists())
            tempDir.mkdirs();

        File output = new File(tempDir, "StayTimeList.xls");
        FileOutputStream fos = new FileOutputStream(output);
        workbook.write(fos);
        fos.close();

        JasperReportUtils.printXlsToFrontEnd(response, output);
        output.delete();
        tempDir.delete();

    }
}

From source file:tw.edu.chit.struts.action.registration.ReportPrintAction.java

/**
 * ?/*from   w w w  .j  a v a 2s  .  c o  m*/
 * 
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @param sterm
 * @throws Exception
 */
private void printStmdUnSeld(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response, String sterm) throws Exception {

    HttpSession session = request.getSession(false);
    AdminManager am = (AdminManager) getBean(IConstants.ADMIN_MANAGER_BEAN_NAME);
    CourseManager cm = (CourseManager) getBean(IConstants.COURSE_MANAGER_BEAN_NAME);
    MemberManager mm = (MemberManager) getBean(IConstants.MEMBER_MANAGER_BEAN_NAME);
    ScoreManager sm = (ScoreManager) getBean(IConstants.SCORE_MANAGER_BEAN_NAME);
    ServletContext context = request.getSession().getServletContext();
    List<Clazz> clazzes = sm.findClassBy(new Clazz(processClassInfo(form)),
            getUserCredential(session).getClassInChargeAry(), false);
    String thisYear = cm.getNowBy("School_year");
    String thisTerm = am.findTermBy(PARAMETER_SCHOOL_TERM);

    if (!clazzes.isEmpty()) {

        List<Student> students = null;
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook
                .createSheet(thisYear + "" + thisTerm + "?");
        sheet.setColumnWidth(0, 3000);
        sheet.setColumnWidth(1, 3000);
        sheet.setColumnWidth(2, 5000);
        sheet.setColumnWidth(3, 3500);
        sheet.setColumnWidth(4, 3500);
        sheet.setColumnWidth(5, 3500);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));

        HSSFFont fontSize16 = workbook.createFont();
        fontSize16.setFontHeightInPoints((short) 16);
        fontSize16.setFontName("Arial Unicode MS");

        HSSFFont fontSize12 = workbook.createFont();
        fontSize12.setFontHeightInPoints((short) 12);
        fontSize12.setFontName("Arial Unicode MS");

        // Header
        Toolket.setCellValue(workbook, sheet, 0, 0, "?", fontSize16,
                HSSFCellStyle.ALIGN_CENTER, false, 35.0F, null);

        // Column Header
        Toolket.setCellValue(workbook, sheet, 1, 0, "", fontSize12, HSSFCellStyle.ALIGN_CENTER, true,
                null);
        Toolket.setCellValue(workbook, sheet, 1, 1, "??", fontSize12, HSSFCellStyle.ALIGN_CENTER, true,
                null);
        Toolket.setCellValue(workbook, sheet, 1, 2, "?", fontSize12, HSSFCellStyle.ALIGN_CENTER, true,
                null);
        Toolket.setCellValue(workbook, sheet, 1, 3, "", fontSize12, HSSFCellStyle.ALIGN_CENTER,
                true, null);
        Toolket.setCellValue(workbook, sheet, 1, 4, "", fontSize12, HSSFCellStyle.ALIGN_CENTER,
                true, null);
        Toolket.setCellValue(workbook, sheet, 1, 5, "Email", fontSize12, HSSFCellStyle.ALIGN_CENTER, true,
                null);
        int index = 2;
        List<Seld> selds = null;

        for (Clazz clazz : clazzes) {

            students = mm.findStudentsByClassNo(clazz.getClassNo());
            if (!students.isEmpty()) {
                for (Student student : students) {
                    selds = cm.findSeldByStudentNoAndTerm(student.getStudentNo(), thisTerm);

                    if (selds.isEmpty()) {

                        Toolket.setCellValue(workbook, sheet, index, 0, student.getStudentNo(), fontSize12,
                                HSSFCellStyle.ALIGN_CENTER, true, null);
                        Toolket.setCellValue(workbook, sheet, index, 1, student.getStudentName(), fontSize12,
                                HSSFCellStyle.ALIGN_CENTER, true, null);
                        Toolket.setCellValue(workbook, sheet, index, 2,
                                Toolket.getClassFullName(student.getDepartClass()), fontSize12,
                                HSSFCellStyle.ALIGN_CENTER, true, null);
                        Toolket.setCellValue(workbook, sheet, index, 3,
                                StringUtils.defaultIfEmpty(student.getTelephone(), ""), fontSize12,
                                HSSFCellStyle.ALIGN_CENTER, true, null);
                        Toolket.setCellValue(workbook, sheet, index, 4,
                                StringUtils.defaultIfEmpty(student.getCellPhone(), ""), fontSize12,
                                HSSFCellStyle.ALIGN_CENTER, true, null);
                        Toolket.setCellValue(workbook, sheet, index++, 5,
                                StringUtils.defaultIfEmpty(student.getEmail(), ""), fontSize12,
                                HSSFCellStyle.ALIGN_CENTER, true, null);
                    }
                }

            }
        }

        File tempDir = new File(
                context.getRealPath("/WEB-INF/reports/temp/" + getUserCredential(session).getMember().getIdno()
                        + (new SimpleDateFormat("yyyyMMdd").format(new Date()))));
        if (!tempDir.exists())
            tempDir.mkdirs();

        File output = new File(tempDir, "StmdUnSeld.xls");
        FileOutputStream fos = new FileOutputStream(output);
        workbook.write(fos);
        fos.close();

        JasperReportUtils.printXlsToFrontEnd(response, output);
        output.delete();
        tempDir.delete();
    }
}

From source file:tw.edu.chit.struts.action.secretary.ReportPrintAction.java

/**
 * ?/*w w  w  .ja  v a 2 s  . co m*/
 * 
 * @param mapping org.apache.struts.action.ActionMapping object
 * @param form org.apache.struts.action.ActionForm object
 * @param request request javax.servlet.http.HttpServletRequest object
 * @param response response javax.servlet.http.HttpServletResponse object
 * @param sterm 
 */
@SuppressWarnings("unchecked")
private void printStayTimeList(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response, String sterm) throws Exception {

    HttpSession session = request.getSession(false);
    MemberManager mm = (MemberManager) getBean(IConstants.MEMBER_MANAGER_BEAN_NAME);
    CourseManager cm = (CourseManager) getBean(IConstants.COURSE_MANAGER_BEAN_NAME);
    ServletContext context = request.getSession().getServletContext();
    String year = cm.getNowBy("School_year");
    String term = form.getString("sterm");

    List<Empl> empls = mm.findAllTeacher(term);
    if (!empls.isEmpty()) {

        File templateXLS = new File(context.getRealPath("/WEB-INF/reports/TeachSchedAll.xls"));
        HSSFWorkbook workbook = Toolket.getHSSFWorkbook(templateXLS);

        HSSFFont fontSize12 = workbook.createFont();
        fontSize12.setFontHeightInPoints((short) 12);
        fontSize12.setFontName("");

        int sheetIndex = 0;
        int colOffset = 1, col = 0;
        boolean isLocationNull = false;
        HSSFSheet sheet = null;
        List<Map> map = null;
        List<TeacherStayTime> tsts = null;
        List<LifeCounseling> lcs = null;
        Map content = null;
        Short colorForStayTime = HSSFColor.AUTOMATIC.index;
        Short colorForLifeCounseling = HSSFColor.LIGHT_GREEN.index;

        for (Empl empl : empls) {

            if ("1".equalsIgnoreCase(empl.getCategory())) {

                sheet = workbook.getSheetAt(sheetIndex);
                workbook.setSheetName(sheetIndex++, empl.getCname());
                isLocationNull = empl.getLocation() == null;

                Toolket.setCellValue(sheet, 0, 1, year + "" + term + "" + empl.getCname()
                        + "?" + " (:"
                        + (isLocationNull ? ""
                                : StringUtils.defaultIfEmpty(empl.getLocation().getExtension(), ""))
                        + " ?:"
                        + (isLocationNull ? "" : StringUtils.defaultIfEmpty(empl.getLocation().getRoomId(), ""))
                        + ")");
                map = cm.findCourseByTeacherTermWeekdaySched(empl.getIdno(), term.toString());

                for (int i = 0; i < 14; i++) {
                    for (int j = 0; j < 7; j++) {
                        content = map.get(j * 15 + i);
                        if (!CollectionUtils.isEmpty(content)) {
                            Toolket.setCellValue(sheet, i + 2, j + 2, (String) content.get("ClassName") + "\n"
                                    + (String) content.get("chi_name") + "\n" + (String) content.get("place"));
                        }
                    }
                }

                //tsts = empl.getStayTime();
                List<TeacherStayTime> myTsts = cm.ezGetBy(
                        " Select Week, Node1, Node2, Node3, Node4, Node5, Node6, Node7, Node8, Node9, Node10, "
                                + "        Node11, Node12, Node13, Node14 " + " From TeacherStayTime "
                                + " Where SchoolYear='" + year + "'" + "   And SchoolTerm='" + term + "' "
                                + "   And parentOid='" + empl.getOid() + "'");

                List myTsts2 = new ArrayList();

                for (int i = 0; i < myTsts.size(); i++) {
                    //for (TeacherStayTime tst : tsts) {                                    
                    myTsts2.add(myTsts.get(i));
                    String s = myTsts2.get(i).toString();
                    col = Integer.parseInt(s.substring(6, 7)) + colOffset; //Week   
                    //col = tst.getWeek() + colOffset;
                    //if (tst.getNode1() != null && tst.getNode1() == 1) {                  
                    if (Integer.parseInt(s.substring(15, 16)) == 1) { //Node1
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 2, col)))
                            Toolket.setCellValue(workbook, sheet, 2, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode2() != null && tst.getNode2() == 1) {
                    if (Integer.parseInt(s.substring(24, 25)) == 1) { //Node2
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 3, col)))
                            Toolket.setCellValue(workbook, sheet, 3, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode3() != null && tst.getNode3() == 1) {
                    if (Integer.parseInt(s.substring(33, 34)) == 1) { //Node3
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 4, col)))
                            Toolket.setCellValue(workbook, sheet, 4, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode4() != null && tst.getNode4() == 1) {
                    if (Integer.parseInt(s.substring(42, 43)) == 1) { //Node4
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 5, col)))
                            Toolket.setCellValue(workbook, sheet, 5, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode5() != null && tst.getNode5() == 1) {
                    if (Integer.parseInt(s.substring(51, 52)) == 1) { //Node5
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 6, col)))
                            Toolket.setCellValue(workbook, sheet, 6, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode6() != null && tst.getNode6() == 1) {
                    if (Integer.parseInt(s.substring(60, 61)) == 1) { //Node6
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 7, col)))
                            Toolket.setCellValue(workbook, sheet, 7, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode7() != null && tst.getNode7() == 1) {
                    if (Integer.parseInt(s.substring(69, 70)) == 1) { //Node7
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 8, col)))
                            Toolket.setCellValue(workbook, sheet, 8, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode8() != null && tst.getNode8() == 1) {
                    if (Integer.parseInt(s.substring(78, 79)) == 1) { //Node8
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 9, col)))
                            Toolket.setCellValue(workbook, sheet, 9, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode9() != null && tst.getNode9() == 1) {
                    if (Integer.parseInt(s.substring(87, 88)) == 1) { //Node9
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 10, col)))
                            Toolket.setCellValue(workbook, sheet, 10, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode10() != null && tst.getNode10() == 1) {
                    if (Integer.parseInt(s.substring(97, 98)) == 1) { //Node10
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 11, col)))
                            Toolket.setCellValue(workbook, sheet, 11, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode11() != null && tst.getNode11() == 1) {
                    if (Integer.parseInt(s.substring(107, 108)) == 1) { //Node11
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 12, col)))
                            Toolket.setCellValue(workbook, sheet, 12, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode12() != null && tst.getNode12() == 1) {
                    if (Integer.parseInt(s.substring(117, 118)) == 1) { //Node12
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 13, col)))
                            Toolket.setCellValue(workbook, sheet, 13, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode13() != null && tst.getNode13() == 1) {
                    if (Integer.parseInt(s.substring(127, 128)) == 1) { //Node13
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 14, col)))
                            Toolket.setCellValue(workbook, sheet, 14, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                    //if (tst.getNode14() != null && tst.getNode14() == 1) {
                    if (Integer.parseInt(s.substring(137, 138)) == 1) { //Node14
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 15, col)))
                            Toolket.setCellValue(workbook, sheet, 15, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForStayTime);
                    }
                }

                //lcs = empl.getLifeCounseling();
                List<LifeCounseling> myLcs = cm.ezGetBy(
                        " Select Week, Node1, Node2, Node3, Node4, Node5, Node6, Node7, Node8, Node9, Node10, "
                                + "        Node11, Node12, Node13, Node14 "
                                + " From LifeCounseling Where ParentOid='" + empl.getOid() + "'");
                List myLcs2 = new ArrayList();

                colOffset = 1;
                col = 0;
                //for (LifeCounseling lc : lcs) {
                for (int y = 0; y < myLcs.size(); y++) {
                    myLcs2.add(myLcs.get(y));
                    String st = myLcs2.get(y).toString();
                    col = Integer.parseInt(st.substring(6, 7)) + colOffset;
                    //col = lc.getWeek() + colOffset;                                 
                    //if (lc.getNode1() != null && lc.getNode1() == 1) {
                    if (Integer.parseInt(st.substring(15, 16)) == 1) { //Node1
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 2, col)))
                            Toolket.setCellValue(workbook, sheet, 2, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode2() != null && lc.getNode2() == 1) {
                    if (Integer.parseInt(st.substring(24, 25)) == 1) { //Node2
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 3, col)))
                            Toolket.setCellValue(workbook, sheet, 3, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode3() != null && lc.getNode3() == 1) {
                    if (Integer.parseInt(st.substring(33, 34)) == 1) { //Node3
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 4, col)))
                            Toolket.setCellValue(workbook, sheet, 4, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode4() != null && lc.getNode4() == 1) {
                    if (Integer.parseInt(st.substring(42, 43)) == 1) { //Node4
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 5, col)))
                            Toolket.setCellValue(workbook, sheet, 5, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode5() != null && lc.getNode5() == 1) {
                    if (Integer.parseInt(st.substring(51, 52)) == 1) { //Node5
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 6, col)))
                            Toolket.setCellValue(workbook, sheet, 6, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode6() != null && lc.getNode6() == 1) {
                    if (Integer.parseInt(st.substring(60, 61)) == 1) { //Node6
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 7, col)))
                            Toolket.setCellValue(workbook, sheet, 7, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode7() != null && lc.getNode7() == 1) {
                    if (Integer.parseInt(st.substring(69, 70)) == 1) { //Node7
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 8, col)))
                            Toolket.setCellValue(workbook, sheet, 8, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode8() != null && lc.getNode8() == 1) {
                    if (Integer.parseInt(st.substring(78, 79)) == 1) { //Node8
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 9, col)))
                            Toolket.setCellValue(workbook, sheet, 9, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode9() != null && lc.getNode9() == 1) {
                    if (Integer.parseInt(st.substring(87, 88)) == 1) { //Node9
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 10, col)))
                            Toolket.setCellValue(workbook, sheet, 10, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode10() != null && lc.getNode10() == 1) {
                    if (Integer.parseInt(st.substring(97, 98)) == 1) { //Node10
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 11, col)))
                            Toolket.setCellValue(workbook, sheet, 11, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode11() != null && lc.getNode11() == 1) {
                    if (Integer.parseInt(st.substring(107, 108)) == 1) { //Node11
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 12, col)))
                            Toolket.setCellValue(workbook, sheet, 12, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode12() != null && lc.getNode12() == 1) {
                    if (Integer.parseInt(st.substring(117, 118)) == 1) { //Node12
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 13, col)))
                            Toolket.setCellValue(workbook, sheet, 13, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode13() != null && lc.getNode13() == 1) {
                    if (Integer.parseInt(st.substring(127, 128)) == 1) { //Node13
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 14, col)))
                            Toolket.setCellValue(workbook, sheet, 14, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                    //if (lc.getNode14() != null && lc.getNode14() == 1) {
                    if (Integer.parseInt(st.substring(137, 138)) == 1) { //Node14
                        if (StringUtils.isEmpty(Toolket.getCellValue(sheet, 15, col)))
                            Toolket.setCellValue(workbook, sheet, 15, col, "", fontSize12,
                                    HSSFCellStyle.ALIGN_CENTER, true, colorForLifeCounseling);
                    }
                }

            }
        }

        File tempDir = new File(
                context.getRealPath("/WEB-INF/reports/temp/" + getUserCredential(session).getMember().getIdno()
                        + (new SimpleDateFormat("yyyyMMdd").format(new Date()))));
        if (!tempDir.exists())
            tempDir.mkdirs();

        File output = new File(tempDir, "StayTimeList.xls");
        FileOutputStream fos = new FileOutputStream(output);
        workbook.write(fos);
        fos.close();

        JasperReportUtils.printXlsToFrontEnd(response, output);
        output.delete();
        tempDir.delete();
    }
}

From source file:tw.edu.chit.struts.action.secretary.ReportPrintAction.java

/**
 * ?//  w  w  w  .  j a  v a2  s .  c om
 * 
 * @param mapping org.apache.struts.action.ActionMapping object
 * @param form org.apache.struts.action.ActionForm object
 * @param request request javax.servlet.http.HttpServletRequest object
 * @param response response javax.servlet.http.HttpServletResponse object
 * @param sterm 
 */
private void printCalendarList(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response, String sterm) throws Exception {

    HttpSession session = request.getSession(false);
    AdminManager am = (AdminManager) getBean(IConstants.ADMIN_MANAGER_BEAN_NAME);
    DynaActionForm aForm = (DynaActionForm) form;
    ServletContext context = request.getSession().getServletContext();
    ActionMessages messages = new ActionMessages();

    Date from = null, to = null;
    if (StringUtils.isNotBlank(aForm.getString("licenseValidDateStart"))
            || StringUtils.isNotBlank(aForm.getString("licenseValidDateEnd"))) {
        from = StringUtils.isBlank(aForm.getString("licenseValidDateStart")) ? null
                : Toolket.parseNativeDate(aForm.getString("licenseValidDateStart"));
        // ???
        to = StringUtils.isBlank(aForm.getString("licenseValidDateEnd")) ? Calendar.getInstance().getTime()
                : Toolket.parseNativeDate(aForm.getString("licenseValidDateEnd"));
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime(to);
    cal.add(Calendar.DAY_OF_MONTH, 1);

    try {
        IConstants.GOOGLE_SERVICES.setUserCredentials(IConstants.GOOGLE_EMAIL_USERNAME,
                IConstants.GOOGLE_EMAIL_PASSWORD);
    } catch (AuthenticationException ae) {
        log.error(ae.getMessage(), ae);
        messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("Course.errorN1",
                "??,??,,??!"));
        saveMessages(request, messages);
    }

    DateTime rangeFrom = Toolket.parseDateToGoogleDateTime(from);
    DateTime rangeTo = Toolket.parseDateToGoogleDateTime(cal.getTime());

    CalendarEventEntry[] entries = am.findCalendarEventBy(IConstants.GOOGLE_SERVICES, rangeFrom, rangeTo);

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("");
    sheet.setColumnWidth(0, 5000);
    sheet.setColumnWidth(1, 3000);
    sheet.setColumnWidth(2, 3000);
    sheet.setColumnWidth(3, 8000);
    sheet.setColumnWidth(4, 8000);
    sheet.setColumnWidth(5, 8000);
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));

    HSSFFont fontSize16 = workbook.createFont();
    fontSize16.setFontHeightInPoints((short) 16);
    fontSize16.setFontName("Arial Unicode MS");

    HSSFFont fontSize10 = workbook.createFont();
    fontSize10.setFontHeightInPoints((short) 10);
    fontSize10.setFontName("Arial Unicode MS");

    // Header
    Toolket.setCellValue(workbook, sheet, 0, 0, "", fontSize16, HSSFCellStyle.ALIGN_CENTER,
            false, 35.0F, null);

    // Column Header
    Toolket.setCellValue(workbook, sheet, 1, 0, "", fontSize10, HSSFCellStyle.ALIGN_CENTER, true,
            null);
    Toolket.setCellValue(workbook, sheet, 1, 1, "", fontSize10, HSSFCellStyle.ALIGN_CENTER, true,
            null);
    Toolket.setCellValue(workbook, sheet, 1, 2, "?", fontSize10, HSSFCellStyle.ALIGN_CENTER, true,
            null);
    Toolket.setCellValue(workbook, sheet, 1, 3, "", fontSize10, HSSFCellStyle.ALIGN_CENTER, true, null);
    Toolket.setCellValue(workbook, sheet, 1, 4, "", fontSize10, HSSFCellStyle.ALIGN_CENTER, true, null);
    Toolket.setCellValue(workbook, sheet, 1, 5, "?", fontSize10, HSSFCellStyle.ALIGN_CENTER, true,
            null);
    int index = 2;

    List<ExtendedProperty> props = null;
    boolean flag = false;
    DateFormat dt = new SimpleDateFormat("kk:mm");
    DateTime d = null;
    Calendar c = null;

    if (entries.length != 0) {
        for (CalendarEventEntry entry : entries) {

            props = entry.getExtendedProperty();
            flag = false;
            for (ExtendedProperty prop : props) {
                if ("host".equalsIgnoreCase(prop.getName())) {
                    Toolket.setCellValue(workbook, sheet, index, 2, StringUtils.trimToEmpty(prop.getValue()),
                            fontSize10, HSSFCellStyle.ALIGN_CENTER, true, null);
                    flag = true;
                }
            }

            if (!flag)
                Toolket.setCellValue(workbook, sheet, index, 2, "", fontSize10, HSSFCellStyle.ALIGN_CENTER,
                        true, null);

            d = entry.getTimes().get(0).getStartTime();
            c = Calendar.getInstance();
            c.setTimeInMillis(d.getValue());
            Toolket.setCellValue(workbook, sheet, index, 0, Toolket.printNativeDate(c.getTime()), fontSize10,
                    HSSFCellStyle.ALIGN_CENTER, true, null);
            Toolket.setCellValue(workbook, sheet, index, 1, dt.format(c.getTime()), fontSize10,
                    HSSFCellStyle.ALIGN_CENTER, true, null);
            Toolket.setCellValue(workbook, sheet, index, 3, entry.getTitle().getPlainText(), fontSize10,
                    HSSFCellStyle.ALIGN_LEFT, true, null);
            Toolket.setCellValue(workbook, sheet, index, 4,
                    StringUtils.defaultIfEmpty(entry.getLocations().get(0).getValueString(), ""), fontSize10,
                    HSSFCellStyle.ALIGN_LEFT, true, null);
            if (StringUtils.contains(entry.getPlainTextContent(), "?"))
                Toolket.setCellValue(workbook, sheet, index++, 5,
                        StringUtils.substringAfter(entry.getPlainTextContent().trim(), "\n"), fontSize10,
                        HSSFCellStyle.ALIGN_LEFT, true, null);
            else
                Toolket.setCellValue(workbook, sheet, index++, 5, entry.getPlainTextContent().trim(),
                        fontSize10, HSSFCellStyle.ALIGN_LEFT, true, null);

        }
    }

    File tempDir = new File(
            context.getRealPath("/WEB-INF/reports/temp/" + getUserCredential(session).getMember().getIdno()
                    + (new SimpleDateFormat("yyyyMMdd").format(new Date()))));
    if (!tempDir.exists())
        tempDir.mkdirs();

    File output = new File(tempDir, "CalendarList.xls");
    FileOutputStream fos = new FileOutputStream(output);
    workbook.write(fos);
    fos.close();

    JasperReportUtils.printXlsToFrontEnd(response, output);
    output.delete();
    tempDir.delete();
}