KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bluecubs > xinco > archive > XincoArchiver


1 /**
2  *Copyright 2005 blueCubs.com
3  *
4  *Licensed under the Apache License, Version 2.0 (the "License");
5  *you may not use this file except in compliance with the License.
6  *You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *Unless required by applicable law or agreed to in writing, software
11  *distributed under the License is distributed on an "AS IS" BASIS,
12  *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *See the License for the specific language governing permissions and
14  *limitations under the License.
15  *
16  *************************************************************
17  * This project supports the blueCubs vision of giving back
18  * to the community in exchange for free software!
19  * More information on: http://www.bluecubs.org
20  *************************************************************
21  *
22  * Name: XincoArchiver
23  *
24  * Description: handle document archiving
25  *
26  * Original Author: Alexander Manes
27  * Date: 2005/01/16
28  *
29  * Modifications:
30  *
31  * Who? When? What?
32  * - - -
33  *
34  *************************************************************
35  */

36
37 package com.bluecubs.xinco.archive;
38
39 import java.sql.*;
40 import java.io.*;
41 import java.util.Vector JavaDoc;
42 import java.util.Calendar JavaDoc;
43 import java.util.GregorianCalendar JavaDoc;
44
45 import com.bluecubs.xinco.add.*;
46 import com.bluecubs.xinco.core.server.*;
47
48 /**
49  * This class handles document archiving for xinco.
50  * Edit configuration values in context.xml
51  */

52 public class XincoArchiver {
53     
54     public static synchronized boolean archiveData(XincoDBManager dbm) {
55         
56         int i = 0;
57         int j = 0;
58         int k = 0;
59         int len = 0;
60         FileInputStream fcis = null;
61         FileOutputStream fcos = null;
62         byte[] fcbuf = null;
63         
64         try {
65             XincoCoreDataServer xdata_temp = null;
66             XincoCoreLogServer xlog_temp = null;
67             Vector JavaDoc xnode_temp_vector = null;
68             Calendar JavaDoc ngc = new GregorianCalendar JavaDoc();
69             String JavaDoc ArchiveName = ngc.get(Calendar.YEAR) + "-" + (ngc.get(Calendar.MONTH) + 1) + "-" + ngc.get(Calendar.DAY_OF_MONTH);
70             String JavaDoc ArchiveBaseDir = dbm.config.FileRepositoryPath + ArchiveName;
71             String JavaDoc ArchiveFileDir = null;
72             Vector JavaDoc OrgFileNames = new Vector JavaDoc();
73             String JavaDoc FileName = null;
74             int querycount = 0;
75             String JavaDoc[] query = new String JavaDoc[2];
76             
77             query[querycount] = new String JavaDoc("SELECT DISTINCT xcd.id FROM xinco_core_data xcd, xinco_add_attribute xaa1, xinco_add_attribute xaa2 " +
78                                 "WHERE xcd.xinco_core_data_type_id = 1 " +
79                                 "AND xcd.status_number <> 3 " +
80                                 "AND xcd.id = xaa1.xinco_core_data_id " +
81                                 "AND xcd.id = xaa2.xinco_core_data_id " +
82                                 "AND xaa1.attribute_id = 5 " +
83                                 "AND xaa1.attrib_unsignedint = 1 " +
84                                 "AND xaa2.attribute_id = 6 " +
85                                 "AND xaa2.attrib_datetime < now() " +
86                                 "ORDER BY xcd.id");
87             querycount++;
88
89             query[querycount] = new String JavaDoc("SELECT DISTINCT xcd.id FROM xinco_core_data xcd, xinco_add_attribute xaa1, xinco_add_attribute xaa2, xinco_core_log xcl " +
90                                 "WHERE xcd.xinco_core_data_type_id = 1 " +
91                                 "AND xcd.status_number <> 3 " +
92                                 "AND xcd.id = xaa1.xinco_core_data_id " +
93                                 "AND xcd.id = xaa2.xinco_core_data_id " +
94                                 "AND xcd.id = xcl.xinco_core_data_id " +
95                                 "AND xaa1.attribute_id = 5 " +
96                                 "AND xaa1.attrib_unsignedint = 2 " +
97                                 "AND xaa2.attribute_id = 7 " +
98                                 "AND xcl.op_datetime < (now()-(xaa2.attrib_unsignedint*3600*24)) " +
99                                 "ORDER BY xcd.id");
100             querycount++;
101
102             for (j=0;j<querycount;j++) {
103                 
104                 Statement stmt = dbm.con.createStatement();
105                 //select data with expired archiving date
106
ResultSet rs = stmt.executeQuery(query[j]);
107                 while (rs.next()) {
108                     xdata_temp = new XincoCoreDataServer(rs.getInt("xcd.id"), dbm);
109                     xnode_temp_vector = XincoCoreNodeServer.getXincoCoreNodeParents(xdata_temp.getXinco_core_node_id(), dbm);
110                     //archive data
111
ArchiveFileDir = "";
112                     for (i=xnode_temp_vector.size()-1;i>=0;i--) {
113                         ArchiveFileDir = ArchiveFileDir + System.getProperty("file.separator") + ((XincoCoreNodeServer)xnode_temp_vector.elementAt(i)).getDesignation();
114                     }
115                     (new File(ArchiveBaseDir + ArchiveFileDir)).mkdirs();
116                     //copy file + revisions
117
//build file list
118
OrgFileNames.add(new String JavaDoc("" + xdata_temp.getId()));
119                     for (i=0;i<xdata_temp.getXinco_core_logs().size();i++) {
120                         xlog_temp = ((XincoCoreLogServer)xdata_temp.getXinco_core_logs().elementAt(i));
121                         if ((xlog_temp.getOp_code() == 1) || (xlog_temp.getOp_code() == 5)) {
122                             OrgFileNames.add(new String JavaDoc("" + xdata_temp.getId() + "-" + xlog_temp.getId()));
123                         }
124                     }
125                     //copy + delete files
126
for (k=0;k<OrgFileNames.size();k++) {
127                         FileName = ((String JavaDoc)OrgFileNames.elementAt(k)) + "_" + ((XincoAddAttribute)xdata_temp.getXinco_add_attributes().elementAt(0)).getAttrib_varchar();
128                         if ((new File(dbm.config.FileRepositoryPath + ((String JavaDoc)OrgFileNames.elementAt(k)))).exists()) {
129                             fcis = new FileInputStream(new File(dbm.config.FileRepositoryPath + ((String JavaDoc)OrgFileNames.elementAt(k))));
130                             fcos = new FileOutputStream(new File(ArchiveBaseDir + ArchiveFileDir + System.getProperty("file.separator") + FileName));
131                             fcbuf = new byte[4096];
132                             len = 0;
133                             while((len=fcis.read(fcbuf))!=-1) {
134                                 fcos.write(fcbuf, 0, len);
135                                 }
136                             fcis.close();
137                             fcos.close();
138                             //delete
139
(new File(dbm.config.FileRepositoryPath + ((String JavaDoc)OrgFileNames.elementAt(k)))).delete();
140                         }
141                     }
142                     //update data + log
143
xdata_temp.setStatus_number(3);
144                     ((XincoAddAttribute)xdata_temp.getXinco_add_attributes().elementAt(7)).setAttrib_text("[" + ArchiveName + "]" + ArchiveFileDir.replace('\\', '/') + "/" + FileName);
145                     xdata_temp.write2DB(dbm);
146                     if (xdata_temp.getXinco_core_logs().size() > 0) {
147                         xlog_temp = ((XincoCoreLogServer)xdata_temp.getXinco_core_logs().elementAt(xdata_temp.getXinco_core_logs().size()-1));
148                         if (xlog_temp != null) {
149                             xlog_temp.setId(0);
150                             xlog_temp.setOp_code(8);
151                             xlog_temp.setOp_description("Archived!");
152                             xlog_temp.setXinco_core_user_id(1);
153                             xlog_temp.write2DB(dbm);
154                         }
155                     }
156                 }
157                 stmt.close();
158
159             }
160             return true;
161         } catch (Exception JavaDoc e) {
162             try {
163                 if (fcis != null) {
164                     fcis.close();
165                 }
166                 if (fcos != null) {
167                     fcos.close();
168                 }
169             } catch (Exception JavaDoc fe) {}
170             return false;
171         }
172         
173     }
174     
175     private XincoArchiver() {
176     }
177
178 }
179
Popular Tags