Related
Below is the code which converts the .sql file for insert statements into .tbl file. As the input file is .sql file it contains varchar(string) values inside single quotes. How can I prevent these quotes from getting in my tbl file?
Input:
INSERT INTO
post_tran(post_tran_id,tran_nr,datetime_tran_local,system_trace_audit_nr,settle_
amount_rsp,settle_tran_fee_rsp,post_tran_cust_id,prev_post_tran_id,next_post_tra
n_id,message_type,tran_postilion_originated,sink_node_name,settle_entity_id,batc
h_nr,tran_completed,tran_type,rsp_code_rsp,auth_type,auth_reason,retrieval_refer
ence_nr,settle_amount_req,settle_tran_fee_req,settle_currency_code,datetime_req,
recon_business_date,realtime_business_date,acquiring_inst_id_code) VALUES(
1,2,'2002-04-02 19:02:28','008497',4120,10, 2, 0,0,'0200', 0, 'LinkSink',
1,1,0,'01','01','00',0,'000000000102',6000,0,'840', '', '2004-02-11
12:00:00', '2004-02-11 12:00:00', '2200017000')
For example:
While processing sink_node_name, its data value should be LinkSink instead of 'LinkSink'.
public class SqlToTblCoverter {
private File source_folder = null;
private File destination_folder = null;
private String source_absolute_path = null;
private String destination_absolute_path = null;
private String absolute_file_name = null;
private String absolute_new_file_name = null;
private List<String> column_name_list = null;
private List<String> column_values_list = null;
public SqlToTblCoverter(String source_folder_name,
String destination_folder_name) {
source_folder = new File(source_folder_name);
destination_folder = new File(destination_folder_name);
source_absolute_path = source_folder.getAbsolutePath();
destination_absolute_path = destination_folder.getAbsolutePath();
column_name_list = new ArrayList<String>();
column_values_list = new ArrayList<String>();
}
public void run() throws IOException {
validateInputs();
migrateFiles();
}
private void validateInputs() {
if (source_folder.isDirectory() == false) {
System.out.println("Source must be a FOLDER");
} else if (destination_folder.isDirectory() == false) {
System.out.println("Destination must be a FOLDER");
}
}
private void migrateFiles() throws IOException {
String[] file_list = source_folder.list();
String file_name1 = file_list[0];
// System.out.println(file_name1);
String file_name2 = file_list[1];
// System.out.println(file_name2);
String f1 = migrateFileContains(file_name1);
String f2 = migrateFileContains(file_name2);
Migrator mg = new Migrator();
mg.migrate(f1, f2);
}
private String migrateFileContains(String file_name) throws IOException {
absolute_file_name = source_absolute_path + File.separator + file_name;
absolute_new_file_name = destination_absolute_path + File.separator
+ getNewFileName(file_name);
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(new File(absolute_file_name))));
String line_info = br.readLine();
StringBuffer new_query = new StringBuffer("");
FileWriter fw = new FileWriter(new File(absolute_new_file_name));
while (line_info != null) {
String convertQuery = convertQuery(line_info);
if (convertQuery.isEmpty()) {
line_info = br.readLine();
continue;
}
new_query.append(convertQuery);
new_query.append(System.getProperty("line.separator"));
fw.write(new_query.toString());
new_query.setLength(0);
line_info = br.readLine();
}
br.close();
fw.close();
return absolute_new_file_name;
}
private String convertQuery(String query) {
String new_query = "";
if (query.startsWith("INSERT")) {
int round_bracket_start = query.indexOf('(');
int round_bracket_end = query.indexOf(')');
int round_bracket_start_after_values = query.indexOf('(',
round_bracket_end);
String query_column_name = query.substring(round_bracket_start + 1,
round_bracket_end);
String query_column_values = query.substring(
round_bracket_start_after_values + 1, query.length() - 1);
covertColumnNameList(query_column_name);
covertColumnValueList(',' + query_column_values + ',');
new_query = createNewQuery() + "\n";
}
column_name_list.clear();
column_values_list.clear();
return new_query;
}
private void covertColumnNameList(String query_column_name) {
String[] column_list = query_column_name.split(",");
for (String column_name : column_list) {
column_name_list.add(column_name);
}
}
private void covertColumnValueList(String query_column_values) {
if (query_column_values.equals(",")) {
return;
}
String column_value = null;
int comma_index = query_column_values.indexOf(',');
int next_comma_index = 0;
if (query_column_values.charAt(comma_index + 1) == '\'') {
int quote_index = query_column_values.indexOf('\'', comma_index);
int next_quote_index = query_column_values.indexOf('\'',
quote_index + 1);
next_comma_index = query_column_values.indexOf(',',
next_quote_index);
column_value = query_column_values.substring(comma_index + 2,
next_comma_index - 1);
} else {
next_comma_index = query_column_values
.indexOf(',', comma_index + 1);
column_value = query_column_values.substring(comma_index + 1,
next_comma_index);
}
column_values_list.add(column_value);
covertColumnValueList(query_column_values.substring(next_comma_index));
}
private String createNewQuery() {
StringBuffer buffer = new StringBuffer("");
if (column_name_list.size() != column_values_list.size()) {
System.out.println("Error : " + absolute_file_name);
} else {
for (int index = 0; index < column_name_list.size(); index++) {
buffer.append(createNewColumn(column_name_list.get(index),
column_values_list.get(index)));
}
}
return buffer.toString();
}
private String createNewColumn(String column_name, String column_value) {
StringBuffer buffer = new StringBuffer("");
buffer.append("[name]".trim());
buffer.append(column_name.trim());
buffer.append("[/name]=[data]".trim());
buffer.append(column_value.trim());
buffer.append("[/data]".trim());
buffer.append("\r\n");
return buffer.toString();
}
private String getNewFileName(String file_name) {
String new_file_name = "";
int dot_index = file_name.indexOf('.');
new_file_name = file_name.subSequence(0, dot_index) + ".tbl";
return new_file_name;
}
}
You can replace covertColumnValueList with below code:
private void covertColumnValueList(String query_column_values) {
String temp_val = null;
for (String col_val : query_column_values.split(",")) {
if (col_val.contains("\'")) {
temp_val = col_val.replaceAll("\'", "");
column_values_list.add(temp_val.trim());
} else
column_values_list.add(col_val.trim());
}
}
I assume you have query_column_values something like below:
String query_column_values = "1,2,'2002-04-02 19:02:28','008497',4120,10, 2, 0,0,'0200', 0, 'LinkSink', 1,1,0,'01','01','00',0,'000000000102',6000,0,'840', '', '2004-02-11 12:00:00', '2004-02-11 12:00:00', '2200017000'";
replace '' by current date: Once you populate this list, replace list value at specified index.
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
column_values_list.set( 23, dateFormat.format(cal.getTime()));
The code to remove the delimiting apostrophes is there in covertColumnValueList, but it doesn't handle the case where a space precedes the apostroph. See the simple fix below.
//....
int next_comma_index = 0;
// skip space(s)
while( query_column_values.charAt(comma_index + 1) == ' ' ){
comma_index++;
}
if (query_column_values.charAt(comma_index + 1) == '\'') {
//...
private void covertColumnValueList(String query_column_values) {
if (query_column_values.equals(",")) {
return;
}
String column_value = null;
int comma_index = query_column_values.indexOf(',');
int next_comma_index = 0;
if (query_column_values.charAt(comma_index + 1) == '\'') {
int quote_index = query_column_values.indexOf('\'', comma_index);
int next_quote_index = query_column_values.indexOf('\'',
quote_index + 1);
next_comma_index = query_column_values.indexOf(',',
next_quote_index);
column_value = query_column_values.substring(comma_index + 2,
next_comma_index - 1);
column_value=column_value.replace("\'","") ;
} else {
next_comma_index = query_column_values
.indexOf(',', comma_index + 1);
column_value = query_column_values.substring(comma_index + 1,
next_comma_index);
column_value=column_value.replace("\'","") ;
}
column_values_list.add(column_value);
covertColumnValueList(query_column_values.substring(next_comma_index));
}
I am using Ghost4j to convert multipage PDFs to multipage TIFF images. I haven't found an example of how this is done.
Using below code I'm able to convert the multipage PDF to images but how do I create a single multipage TIFF image out of it?
PDFDocument lDocument = new PDFDocument();
lDocument.load(filePath);
// create renderer
SimpleRenderer lRenderer = new SimpleRenderer();
// set resolution (in DPI)
lRenderer.setResolution(300);
// render as images
List<Image> lImages = lRenderer.render(lDocument);
Iterator<Image> lImageIterator = lImages.iterator();
//how to convert the images to a single TIFF image?
While this is not exactly what you are doing I did something similar. I combined multiple images in one directory into a tiff file with separate pages. Take a look at this code and see if you can pick out what you need. Also you will need external jars/libraries for this to work they can be downloaded just google them. I hope this helps you.
public class CombineImages {
private static String directory = null;
private static String combinedImageLocation = null;
private static DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
private static int folderCount = 0;
private static int totalFolderCount = 0;
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the directory path that contains the images: ");
directory = scanner.nextLine(); //initialize directory
System.out.println("Please enter a location for the new combined images to be placed: ");
combinedImageLocation = scanner.nextLine(); //initialize directory
if(!(combinedImageLocation.charAt((combinedImageLocation.length() - 1)) == '\\'))
{
combinedImageLocation += "\\";
}
if(!(directory.charAt((directory.length() - 1)) == '\\'))
{
directory += "\\";
}
scanner.close();
//directory = "C:\\Users\\sorensenb\\Desktop\\CombinePhotos\\";
//combinedImageLocation = "C:\\Users\\sorensenb\\Desktop\\NewImages\\";
File filesDirectory;
filesDirectory = new File(directory);
File[] files; //holds files in given directory
if(filesDirectory.exists())
{
files = filesDirectory.listFiles();
totalFolderCount = files.length;
folderCount = 0;
if(files != null && (files.length > 0))
{
System.out.println("Start Combining Files...");
System.out.println("Start Time: " + dateFormat.format(new Date()));
combineImages(files);
System.out.println("Finished Combining Files.");
System.out.println("Finish Time: " + dateFormat.format(new Date()));
}
}
else
{
System.out.println("Directory does not exist!");
System.exit(1);
}
}
public static void combineImages(File[] files)
{
int fileCounter = 1;
ArrayList<String> allFiles = new ArrayList<String>();
String folderBase = "";
String parentFolder = "";
String currentFileName = "";
String previousFileName = "";
File previousFile = null;
int counter = 0;
for(File file:files)
{
if(file.isDirectory())
{
folderCount++;
System.out.println("Folder " + folderCount + " out of " + totalFolderCount + " folders.");
combineImages(file.listFiles());
}
else
{
try {
folderBase = file.getParentFile().getCanonicalPath();
parentFolder = file.getParentFile().getName();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(counter == 0)
{
allFiles.add(file.getName().substring(0, file.getName().indexOf('.')));
}
else
{
currentFileName = file.getName();
previousFileName = previousFile.getName();
currentFileName = currentFileName.substring(0, currentFileName.indexOf('.'));
previousFileName = previousFileName.substring(0, previousFileName.indexOf('.'));
if(!currentFileName.equalsIgnoreCase(previousFileName))
{
allFiles.add(currentFileName);
}
}
}
previousFile = file;
counter++;
}
System.out.println("Total Files to be Combined: " + allFiles.size());
for(String currentFile:allFiles)
{
System.out.println("File " + fileCounter + " out of " + allFiles.size() + " CurrentFile: " + currentFile);
try {
createNewImage(currentFile, files, folderBase, parentFolder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fileCounter++;
}
}
***public static void createNewImage(String currentFile, File[] files, String folderBase, String parentFolder) throws IOException
{
//BufferedImage image;
int totalHeight = 0;
int maxWidth = 0;
int currentHeight = 0;
ArrayList<String> allFiles = new ArrayList<String>();
for(File file:files)
{
if((file.getName().substring(0, file.getName().indexOf('.'))).equalsIgnoreCase(currentFile))
{
allFiles.add(file.getName());
}
}
allFiles = sortFiles(allFiles);
BufferedImage image[] = new BufferedImage[allFiles.size()];
SeekableStream ss = null;
PlanarImage op = null;
for (int i = 0; i < allFiles.size(); i++) {
ss = new FileSeekableStream(folderBase + "\\" + allFiles.get(i));
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", ss, null);
op = new NullOpImage(decoder.decodeAsRenderedImage(0),
null, null, OpImage.OP_IO_BOUND);
image[i] = op.getAsBufferedImage();
}
op.dispose();
ss.close();
/*BufferedImage convertImage;
for(int i = 0; i < image.length; i++)
{
convertImage = new BufferedImage(image[i].getWidth(),image[i].getHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics g = convertImage.getGraphics();
g.drawImage(convertImage, 0, 0, null);
g.dispose();
image[i] = convertImage;
}*/
File folderExists = new File(combinedImageLocation);
if(!folderExists.exists())
{
folderExists.mkdirs();
}
TIFFEncodeParam params = new TIFFEncodeParam();
params.setCompression(TIFFEncodeParam.COMPRESSION_GROUP3_1D);
int changeName = 1;
String tempCurrentFile = currentFile;
while((new File(combinedImageLocation + tempCurrentFile + "Combined.tif").exists()))
{
tempCurrentFile = currentFile;
tempCurrentFile += ("(" + changeName + ")");
changeName++;
}
currentFile = tempCurrentFile;
OutputStream out = new FileOutputStream(combinedImageLocation + currentFile + "Combined" + ".tif");
ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, params);
Vector vector = new Vector();
for (int i = 1; i < allFiles.size(); i++) {
vector.add(image[i]);
}
params.setExtraImages(vector.iterator());
encoder.encode(image[0]);
for(int i = 0; i < image.length; i++)
{
image[i].flush();
}
out.close();
System.gc();
/*for(String file:allFiles)
{
image = ImageIO.read(new File(folderBase, file));
int w = image.getWidth();
int h = image.getHeight();
totalHeight += h;
if(w > maxWidth)
{
maxWidth = w;
}
image.flush();
}
BufferedImage combined = new BufferedImage((maxWidth), (totalHeight), BufferedImage.TYPE_BYTE_GRAY);
for(String file:allFiles)
{
Graphics g = combined.getGraphics();
image = ImageIO.read(new File(folderBase, file));
int h = image.getHeight();
g.drawImage(image, 0, currentHeight, null);
currentHeight += h;
image.flush();
g.dispose();
}
File folderExists = new File(combinedImageLocation + parentFolder + "\\");
if(!folderExists.exists())
{
folderExists.mkdirs();
}
ImageIO.write(combined, "TIFF", new File(combinedImageLocation, (parentFolder + "\\" + currentFile + "Combined.tif")));
combined.flush();
System.gc();*/
}***
public static ArrayList<String> sortFiles(ArrayList<String> allFiles)
{
ArrayList<String> sortedFiles = new ArrayList<String>();
ArrayList<String> numbers = new ArrayList<String>();
ArrayList<String> letters = new ArrayList<String>();
for(String currentFile:allFiles)
{
try
{
Integer.parseInt(currentFile.substring((currentFile.indexOf('.') + 1)));
numbers.add(currentFile);
}catch(Exception e)
{
letters.add(currentFile);
}
}
//String[] numbersArray = new String[numbers.size()];
//String[] lettersArray = new String[letters.size()];
for(int i = 0; i < numbers.size(); i++)
{
sortedFiles.add(numbers.get(i));
}
for(int i = 0; i < letters.size(); i++)
{
sortedFiles.add(letters.get(i));
}
return sortedFiles;
}
}
I want to read and fetch my folder paths from the excel sheet under the column "FolderPath" and create a list of folder structures under a parent folder called "Documentum". I have attached sample excel data as below. I am able to create a folder from the below code by hard coding the folder path to be created. For Eg in this case it is : /documentum.
FolderPath
/documentum/folder1
/documentum/folder2
/documentum/folder3
/documentum/folder8/folder9
**
Kindly suggest , how can I get this feature added in my below code. I tried with excel POI but no luck as I havent worked much in POI, any sample modifications or suggestions to the below code can be of help.
package com.documentum;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.IDfId;
import com.documentum.fc.common.IDfLoginInfo;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfFolder;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;
import java.util.StringTokenizer;
import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
public class AutoFolderStructure
{
/**
* #param args
*/
public static void main(String[] args)
{
String username = "user";
String password = "docu";
String repoName = "documentum";
String folderPath = "/documentum"; //folder path to be created
IDfSessionManager sessMgr = null;
IDfSession sess = null;
try
{
sessMgr = createSessionManager();
addIdentity(sessMgr,username,password,repoName);
sess = sessMgr.getSession(repoName);
IDfId newId = createFolder(sess,folderPath,null,null,null);
System.out.println("Created folder: " + newId);
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
if((sessMgr != null) && (sess != null))
{
sessMgr.release(sess);
}
}
}
public static IDfId createFolder(
IDfSession sess,
String path,
String pathSep,
String type,
String basePath)
throws DfException
{
boolean cabinetStillToBeProcessed = true;
if (pathSep == null || (pathSep.length() == 0))
{
pathSep = "/";
}
if ((type == null) || (type.length() == 0))
{
type = "dm_folder";
}
IDfId currentId = null;
StringBuffer bufFldrPath = new StringBuffer(48);
if ((basePath != null) && (basePath.length() > 1))
{
currentId = getIdByPath(sess, basePath);
if (!currentId.isNull())
{
//base path actually exists.
bufFldrPath.append(basePath);
cabinetStillToBeProcessed = false; //cabinet already processed due to base path.
int basePathLen = basePath.length();
if(basePathLen < path.length())
{
path = path.substring(basePath.length());
}
}
}
StringTokenizer tokFldrNames = new StringTokenizer(path, pathSep);
if (cabinetStillToBeProcessed)
{
//Execution will come here only if basePath was not specified or
//if specified basePath was not valid.
String cabinetName = tokFldrNames.nextToken();
StringBuffer cabQual = new StringBuffer(32);
cabQual.append("dm_cabinet where object_name='").append(
cabinetName).append(
"'");
currentId = sess.getIdByQualification(cabQual.toString());
if (currentId.isNull())
{
//need to create cabinet.
IDfFolder cab = (IDfFolder) sess.newObject("dm_cabinet");
cab.setObjectName(cabinetName);
cab.save();
currentId = cab.getObjectId();
}
bufFldrPath.append(pathSep).append(cabinetName);
}
//By this point the bufFldrPath will either have the cabinet path
//or it will have the basePath in it.
//now create all folders beyond the cabinet or basePath.
while(tokFldrNames.hasMoreTokens())
{
String parentPath = bufFldrPath.toString();
String fldrName = tokFldrNames.nextToken();
bufFldrPath.append(pathSep).append(fldrName);
//by this point the buffer should contain the new expected path
currentId = getIdByPath(sess,bufFldrPath.toString());
if(currentId.isNull())
{
//looks like the new folder in the path does not exist.
IDfFolder newFldr = (IDfFolder) sess.newObject(type);
newFldr.setObjectName(fldrName);
newFldr.link(parentPath);
newFldr.save();
currentId = newFldr.getObjectId();
}
//by this point currentId should point to next folder in path
}//while(all folder names)
return currentId;
}
public static IDfId getIdByPath(IDfSession sess, String path)
throws DfException
{
int pathSepIndex = path.lastIndexOf('/');
if (pathSepIndex == -1)
{
return new DfId("000");
}
StringBuffer bufQual = new StringBuffer(32);
if (pathSepIndex == 0)
{
//its a cabinet path
bufQual.append(" dm_cabinet where object_name='");
bufQual.append(path.substring(1));
bufQual.append("'");
}
else
{
bufQual.append(" dm_sysobject where FOLDER('");
bufQual.append(path.substring(0, pathSepIndex));
bufQual.append("') ");
bufQual.append(" and object_name='");
bufQual.append(path.substring(pathSepIndex + 1));
bufQual.append("'");
}
String strQual = bufQual.toString();
IDfId id = sess.getIdByQualification(strQual);
return id;
}
private static IDfSessionManager createSessionManager() throws DfException
{
IDfClientX clientX = new DfClientX();
IDfClient localClient = clientX.getLocalClient();
IDfSessionManager sessMgr = localClient.newSessionManager();
return sessMgr;
}
private static void addIdentity(IDfSessionManager sm, String username,
String password, String repoName) throws DfException
{
IDfClientX clientX = new DfClientX();
IDfLoginInfo li = clientX.getLoginInfo();
li.setUser(username);
li.setPassword(password);
// check if session manager already has an identity.
// if yes, remove it.
if (sm.hasIdentity(repoName))
{
sm.clearIdentity(repoName);
}
sm.setIdentity(repoName, li);
}
}
Thanks and regards
Deb
You can use Apache POI library. You need to add it to your classpath if you are running your own script, and to the java_methods directory if you are doing a documentum job (executed by documentum JMS)
I'll Post here my code of working with the POI:
first of all:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
You can extract from my example how I worked with this:
I used:
ColumName 1 | ColumName 2 | ColumName 3 | ColumName 4
Data Here
Code:
rowStart = 1; Means that I start processing from the data line row by row.
lastColumn = 4; Means that I have 4 Columns.
fix the code how ever you find likly.
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook;
try
{
workbook = new XSSFWorkbook(file);
// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
int rowStart = 1;
int rowEnd = sheet.getLastRowNum();
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++)
{
Row r = sheet.getRow(rowNum);
int lastColumn = 4;
int inputtype = 0;
table_no = "";
dql = "";
description = "";
table_code = "";
code = "";
for (int cn = 0; cn < lastColumn; cn++)
{
Cell cell = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (cell == null)
{
switch (inputtype)
{
case 0:
inputtype++;
code = "";
break;
case 1:
inputtype++;
table_no = "";
break;
case 2:
inputtype++;
table_code = "";
break;
case 3:
inputtype++;
description = "";
break;
default:
inputtype = 0;
break;
}
}
else
{
switch (inputtype)
{
case 0:
inputtype++;
if (cell.getCellType() == Cell.CELL_TYPE_STRING)
{
code = cell.getStringCellValue();
}
else
{
tmp = cell.getNumericCellValue();
code = tmp.intValue() + "";
}
break;
case 1:
inputtype++;
if (cell.getCellType() == Cell.CELL_TYPE_STRING)
{
table_no = cell.getStringCellValue();
}
else
{
tmp = cell.getNumericCellValue();
table_no = tmp.intValue() + "";
}
break;
case 2:
inputtype++;
if (cell.getCellType() == Cell.CELL_TYPE_STRING)
{
table_code = cell.getStringCellValue().trim();
}
else
{
tmp = cell.getNumericCellValue();
table_code = tmp.intValue() + "";
}
break;
case 3:
inputtype++;
if (cell.getCellType() == Cell.CELL_TYPE_STRING)
{
description = cell.getStringCellValue();
}
else
{
tmp = cell.getNumericCellValue();
description = tmp.intValue() + "";
}
break;
default:
inputtype = 0;
break;
}
}
}
I have created a Batch Reporting using Apache POI. I need to know how to add Headers to both sheets. I used getHeader() but that adds same Headers for both sheets and I need to add different headers to both sheets. My code is following.
Excel Writer:
public class ExcelWriter {
Logger log = Logger.getLogger(ExcelWriter.class.getName());
private HSSFWorkbook excel;
public ExcelWriter() {
excel = new HSSFWorkbook();
}
public HSSFWorkbook getWorkbook() {
return excel;
}
public void writeExcelFile(String filename, String[] columns, Object[][] data, HSSFCellStyle[] styles,
HSSFCellStyle columnsStyle, String[] header, String[] footer) throws IOException {
FileOutputStream out = new FileOutputStream(filename);
HSSFSheet sheet = excel.createSheet("Daily Screening");
HSSFSheet sheet1 = excel.createSheet("Parcel Return");
int numHeaderRows = header.length;
createHeader(sheet,header,columns.length, 0);
createColumnHeaderRow(sheet,columns,numHeaderRows,columnsStyle);
int rowCtr1 = numHeaderRows;
for( int i = 0; i < data.length; i++) {
if (i > data.length -2)
++rowCtr1;
else
rowCtr1 = rowCtr1 + 2;
createRow(sheet, data[i], rowCtr1, styles);
}
int totalRows = rowCtr1 + 1;
createHeader(sheet1,footer,columns.length, totalRows);
excel.write(out);
out.close();
}
private void createHeader(HSSFSheet sheet1, String[] header, int columns, int rowNum) {
for( int i = 0; i < header.length ; i++ ) {
HSSFRow row = sheet1.createRow(i + rowNum);
HSSFCell cell = row.createCell((short) 0);
String text = header[i];
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(text);
HSSFCellStyle style = excel.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont arialBoldFont = excel.createFont();
arialBoldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
arialBoldFont.setFontName("Arial");
arialBoldFont.setFontHeightInPoints((short) 12);
style.setFont(arialBoldFont);
if (!isEmpty(header[i]) && rowNum < 1) {
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
}
cell.setCellStyle(style);
sheet1.addMergedRegion( new Region(i+rowNum,(short)0,i+rowNum,(short)(columns-1)) );
}
}
private HSSFRow createColumnHeaderRow(HSSFSheet sheet, Object[] values, int rowNum, HSSFCellStyle style) {
HSSFCellStyle[] styles = new HSSFCellStyle[values.length];
for( int i = 0; i < values.length; i++ ) {
styles[i] = style;
}
return createRow(sheet,values,rowNum,styles);
}
private HSSFRow createRow(HSSFSheet sheet1, Object[] values, int rowNum, HSSFCellStyle[] styles) {
HSSFRow row = sheet1.createRow(rowNum);
for( int i = 0; i < values.length; i++ ) {
HSSFCell cell = row.createCell((short) i);
cell.setCellStyle(styles[i]);
try{
Object o = values[i];
if( o instanceof String ) {
String text = String.valueOf(o);
cell.setCellValue(text);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
}
else if (o instanceof Double) {
Double d = (Double) o;
cell.setCellValue(d.doubleValue());
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
}
else if (o instanceof Integer) {
Integer in = (Integer)o;
cell.setCellValue(in.intValue());
}
else if (o instanceof Long) {
Long l = (Long)o;
cell.setCellValue(l.longValue());
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
}
else if( o != null ) {
String text = String.valueOf(o);
cell.setCellValue(text);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
}
}
catch(Exception e) {
log.error(e.getMessage());
}
}
return row;
}
public boolean isEmpty(String str) {
if(str.equals(null) || str.equals(""))
return true;
else
return false;
}
}
Report Generator which includes Headers, footer, and columns:
public class SummaryReportGenerator extends ReportGenerator {
Logger log = Logger.getLogger(SummaryReportGenerator.class.getName());
public SummaryReportGenerator(String reportDir, String filename) {
super( reportDir + filename + ".xls", reportDir + filename + ".pdf");
}
public String[] getColumnNames() {
String[] columnNames = {"ISC\nCode", "Total\nParcels", "Total\nParcel Hit\n Count",
"Filter Hit\n%", "Unanalyzed\nCount", "Unanalyzed\n%",
"Name\nMatch\nCount", "Name\nMatch\n%", "Pended\nCount",
"Pended\n%", "E 1 Sanction\nCountries\nCount", "E 1 Sanction\nCountries\n%", "Greater\nthat\n$2500\nCount", "Greater\nthat\n$2500\n%",
"YTD\nTotal Hit\nCount", "YTD\nLong Term\nPending", "YTD\nLong Term\n%"};
return columnNames;
}
public HSSFCellStyle getColumnsStyle(HSSFWorkbook wrkbk) {
HSSFCellStyle style = wrkbk.createCellStyle();
style.setWrapText(true);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont timesBoldFont = wrkbk.createFont();
timesBoldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
timesBoldFont.setFontName("Arial");
style.setFont(timesBoldFont);
return style;
}
public Object[][] getData(Map map) {
int rows = map.size();// + 1 + // 1 blank row 1; // 1 row for the grand total;
int cols = getColumnNames().length;
Object[][] data = new Object[rows][cols];
int row = 0;
for (int i=0; i < map.size(); i++ ){
try{
SummaryBean bean = (SummaryBean)map.get(new Integer(i));
data[row][0] = bean.getIscCode();
data[row][1] = new Long(bean.getTotalParcelCtr());
data[row][2] = new Integer(bean.getTotalFilterHitCtr());
data[row][3] = bean.getFilterHitPrctg();
data[row][4] = new Integer(bean.getPendedHitCtr());
data[row][5] = bean.getPendedHitPrctg();
data[row][6] = new Integer(bean.getTrueHitCtr());
data[row][7] = new Integer(bean.getRetiredHitCtr());
data[row][8] = new Integer(bean.getSanctCntryCtr());
data[row][9] = new Integer(bean.getC25Ctr());
data[row][10] = new Integer(bean.getCnmCtr());
data[row][11] = new Integer(bean.getCndCtr());
data[row][12] = new Integer(bean.getCnlCtr());
data[row][13] = new Integer(bean.getCneCtr());
data[row][14] = new Integer(bean.getVndCtr());
data[row][15] = new Integer(bean.getCilCtr());
data[row][16] = new Integer(bean.getHndCtr());
data[row][17] = new Integer(bean.getCnrCtr());
++row;
}
catch(Exception e) {
log.error(e.getMessage());
}
}
return data;
}
public String[] getHeader(String startDate, String endDate) {
Date today = new Date();
String reportDateFormat = Utils.formatDateTime(today, "MM/dd/yyyyHH.mm.ss");
String nowStr = Utils.now(reportDateFormat);
String[] header = {"","EXCS Daily Screening Summary Report ","",
"for transactions processed for the calendar date range",
"from " + startDate + " to " + endDate,
"Report created on " + nowStr.substring(0,10)+ " at "
+ nowStr.substring(10)};
return header;
}
public HSSFCellStyle[] getStyles(HSSFWorkbook wrkbk) {
int columnSize = getColumnNames().length;
HSSFCellStyle[] styles = new HSSFCellStyle[columnSize];
HSSFDataFormat format = wrkbk.createDataFormat();
for (int i=0; i < columnSize; i++){
styles[i] = wrkbk.createCellStyle();
if (i == 0){
styles[i].setAlignment(HSSFCellStyle.ALIGN_LEFT);
}else{
styles[i].setAlignment(HSSFCellStyle.ALIGN_RIGHT);
}
if (i == 1 || i == 2){
styles[i].setDataFormat(format.getFormat("#,###,##0"));
}
HSSFFont timesFont = wrkbk.createFont();
timesFont.setFontName("Arial");
styles[i].setFont(timesFont);
}
return styles;
}
public String[] getFooter() {
String[] header = {"","Parcel Return Reason Code Reference","",
"DPM = Sender and/or recipient matches denied party",
"HND = Humanitarian exception not declared",
"CNM = Content not mailable under export laws",
"VND = Value of content not declared",
"CNR = Customer non-response",
"C25 = Content Value greater than $2500",
"CIL = Invalid license",
"C30 = More than one parcel in a calendar month",
"CNL = Content description not legible",
"CNE = Address on mailpiece not in English",
"RFN = Requires full sender and addressee names",
"DGS = Dangerous goods",
"R29 = RE-used 2976 or 2976A",
"ANE = PS Form 2976 or 2976A not in English",
"ICF = Incorrect Customs Declaration Form used",
"DPR = Declaration of purpose required",
"ITN = Internal Transaction Number (ITN), Export Exception/Exclusion Legend (ELL), or Proof of Filing Citation (PFC) is required",
"OTH = Other","",};
return header;
}
}
Report Generator Abstract class
public void generateExcelReport(Map map, String startDate, String endDate) throws IOException {
ExcelWriter writer = new ExcelWriter();
writer.writeExcelFile( getXlsFilename(), getColumnNames(), getData(map),
getStyles(writer.getWorkbook()), getColumnsStyle(writer.getWorkbook()),
getHeader(startDate, endDate), getFooter());
Report Driver:
public class ReportDriver {
static Logger log = Logger.getLogger(ReportDriver.class.getName());
public static void main (String args[]){
if (args.length == 0) {
log.error("Usage - ReportDriver [Day|Week|Month|Year]");
System.exit(1);
}
String reportPeriod = args[0];
log.info("Begin Prior " + reportPeriod + " Report");
String dir = "c:\\excsbatch\\report\\";
Dates dates = new Dates();
dates.setDates(reportPeriod);
String startDate = dates.getStartDate();
String endDate = dates.getEndDate();
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
Calendar cal = Calendar.getInstance();
String timeStamp = dateFormat.format(cal.getTime());
String fileName = "prior" + reportPeriod + "Report" + timeStamp;
SummaryDAO dao = new SummaryDAO();
Map map = dao.extractData(startDate, endDate);
SummaryReportGenerator report = new SummaryReportGenerator(dir, fileName);
try {
report.generateExcelReport(map, startDate, endDate);
}
catch(Exception e) {
log.error(e.getMessage());
System.exit(2);
}
log.info("End Prior " + reportPeriod + " Report");
}
}
I have a problem with my code. I need to do several operations on a log file with this structure:
190.12.1.100 2011-03-02 12:12 test.html
190.12.1.100 2011-03-03 13:18 data.html
128.33.100.1 2011-03-03 15:25 test.html
128.33.100.1 2011-03-04 18:30 info.html
I need to get the number of visits per month, number of visits per page and number of unique visitors based on the IP. That is not the question, I managed to get all three operations working. The problem is, only the first choice runs correctly while the other choices just return values of 0 afterwards, as if the file is empty, so i am guessing i made a mistake with the I/O somewhere. Here's the code:
import java.io.*;
import java.util.*;
public class WebServerAnalyzer {
private Map<String, Integer> hm1;
private Map<String, Integer> hm2;
private int[] months;
private Scanner input;
public WebServerAnalyzer() throws IOException {
hm1 = new HashMap<String, Integer>();
hm2 = new HashMap<String, Integer>();
months = new int[12];
for (int i = 0; i < 12; i++) {
months[i] = 0;
}
File file = new File("webserver.log");
try {
input = new Scanner(file);
} catch (FileNotFoundException fne) {
input = null;
}
}
public String nextLine() {
String line = null;
if (input != null && input.hasNextLine()) {
line = input.nextLine();
}
return line;
}
public int getMonth(String line) {
StringTokenizer tok = new StringTokenizer(line);
if (tok.countTokens() == 4) {
String ip = tok.nextToken();
String date = tok.nextToken();
String hour = tok.nextToken();
String page = tok.nextToken();
StringTokenizer dtok = new StringTokenizer(date, "-");
if (dtok.countTokens() == 3) {
String year = dtok.nextToken();
String month = dtok.nextToken();
String day = dtok.nextToken();
int m = Integer.parseInt(month);
return m;
}
}
return -1;
}
public String getIP(String line) {
StringTokenizer tok = new StringTokenizer(line);
if (tok.countTokens() == 4) {
String ip = tok.nextToken();
String date = tok.nextToken();
String hour = tok.nextToken();
String page = tok.nextToken();
StringTokenizer dtok = new StringTokenizer(date, "-");
return ip;
}
return null;
}
public String getPage(String line) {
StringTokenizer tok = new StringTokenizer(line);
if (tok.countTokens() == 4) {
String ip = tok.nextToken();
String date = tok.nextToken();
String hour = tok.nextToken();
String page = tok.nextToken();
StringTokenizer dtok = new StringTokenizer(date, "-");
return page;
}
return null;
}
public void visitsPerMonth() {
String line = null;
do {
line = nextLine();
if (line != null) {
int m = getMonth(line);
if (m != -1) {
months[m - 1]++;
}
}
} while (line != null);
// Print the result
String[] monthName = {"JAN ", "FEB ", "MAR ",
"APR ", "MAY ", "JUN ", "JUL ", "AUG ", "SEP ",
"OCT ", "NOV ", "DEC "};
for (int i = 0; i < 12; i++) {
System.out.println(monthName[i] + months[i]);
}
}
public int count() throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream("webserver.log"));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
while ((readChars = is.read(c)) != -1) {
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n')
++count;
}
}
return count;
} finally {
is.close();
}
}
public void UniqueIP() throws IOException{
String line = null;
for (int x = 0; x <count(); x++){
line = nextLine();
if (line != null) {
if(hm1.containsKey(getIP(line)) == false) {
hm1.put(getIP(line), 1);
} else {
hm1.put(getIP(line), hm1.get(getIP(line)) +1 );
}
}
}
Set set = hm1.entrySet();
Iterator i = set.iterator();
System.out.println("\nNumber of unique visitors: " + hm1.size());
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + " - ");
System.out.println(me.getValue() + " visits");
}
}
public void pageVisits() throws IOException{
String line = null;
for (int x = 0; x <count(); x++){
line = nextLine();
if (line != null) {
if(hm2.containsKey(getPage(line)) == false)
hm2.put(getPage(line), 1);
else
hm2.put(getPage(line), hm2.get(getPage(line)) +1 );
}
}
Set set = hm2.entrySet();
Iterator i = set.iterator();
System.out.println("\nNumber of pages visited: " + hm2.size());
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + " - ");
System.out.println(me.getValue() + " visits");
}
}
Any help figuring out the problem would be much appreciated as I am quite stuck.
I didn't read the code thoroughly yet, but I guess you're not setting the read position back to the beginning of the file when you start a new operation. Thus nextLine() would return null.
You should create a new Scanner for each operation and close it afterwards. AFAIK scanner doesn't provide a method to go back to the first byte.
Currently I could also think of 3 alternatives:
Use a BufferedReader and call reset() for each new operation. This should cause the reader to go back to byte 0 provided you didn't call mark() somewhere.
Read the file contents once and iterate over the lines in memory, i.e. put all lines into a List<String> and then start at each line.
Read the file once, parse each line and construct an apropriate data structure that contains the data you need. For example, you could use a TreeMap<Date, Map<Page, Map<IPAdress, List<Visit>>>>, i.e. you'd store the visits per ip address per page for each date. You could then select the appropriate submaps by date, page and ip address.
The reset method of BufferedReader that Thomas recommended would only work if the file size is smaller than the buffer size or if you called mark with a large enough read ahead limit.
I would recommend reading throught the file once and to update your maps and month array for each line. BTW, you don't need a Scanner just to read lines, BufferedReader has a readLine method itself.
BufferedReader br = ...;
String line;
while (null != (line = br.readLine())) {
String ip = getIP(line);
String page = getPage(line);
int month = getMonth(line);
// update hashmaps and arrays
}