1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Generate reports based on calendar and empid using text file as database

Discussion in 'Java' started by sameer_havakajoka, Nov 16, 2009.

  1. sameer_havakajoka

    sameer_havakajoka New Member

    Joined:
    Sep 14, 2009
    Messages:
    271
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Sleeping
    Location:
    Hava Ke Paro Me

    Introduction



    The solution is mainly used in Error website wherein you have to generate reports which gives the number of solutions uploaded in a specific period(Reports by calendar) or the number of solutions posted by particular employee (reports by employee id).

    Background



    My solution of generating reports follows MVC architecture. I'm getting input , for insatnce from date and to date or the employee id from the user. Inputs here will hit the single servlet which calls the corresponding AO class and this AO class uses Utility class to do the process and the result will go to AO class in turn hits the servlet and the reports will be got at th front end

    The code



    Code:
    package org.worldbank.induction.AccessObject;
    import org.worldbank.induction.Bean.IssueBean;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.FileInputStream;
    import java.io.BufferedReader;
    import java.io.EOFException;
    import java.io.ObjectInputStream;
    import java.util.Calendar;
    import java.util.Collection;
    import java.util.Hashtable;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeMap;
    
    
    public class ReportsUtilClass 
    {
    	private Hashtable Exception = new Hashtable();
    
    	private Hashtable resFormData = new Hashtable();
    
    
    
    
    
    	public Hashtable reportsName(Hashtable reqFormData) throws IOException,ClassNotFoundException
    	{
    		System.out.println("Entering ReportsNAme method");
    		String empid=(String) reqFormData.get("employee_id");
    		String filename = "project.txt";
    		File inFile = new File(filename);
    		FileInputStream fis = new FileInputStream(inFile);
    		BufferedReader in = new BufferedReader(new InputStreamReader(fis));
    		TreeMap<String, Integer> modulecount = new TreeMap<String, Integer>();
    		Hashtable resFormData = new Hashtable();
    		String thisLine = null;
    
    		try
    		{
    			System.out.println("Checking Project.txt");
    
    			if(inFile.exists())
    			{
    				System.out.println("Project.txt checked");
    				while ((thisLine = in.readLine()) != null) 
    				{
    					String projectfilename = thisLine+".txt";
    					System.out.println(projectfilename);
    
    					File inProjectFile = new File(projectfilename);
    					FileInputStream fisproject = new FileInputStream(inProjectFile);
    					BufferedReader inproject = new BufferedReader(new InputStreamReader(fisproject));
    					String thisProjectLine=null;
    
    					System.out.println("Checking project filename");
    					modulecount= new TreeMap();
    					if(inProjectFile.exists())
    					{
    						while ((thisProjectLine = inproject.readLine()) !=null)
    						{
    							int count = 0;
    							String modulefilename =thisLine+"_"+thisProjectLine+".txt";
    							File inModuleFile = new File(modulefilename);
    							FileInputStream fismodule = new FileInputStream(inModuleFile);
    							BufferedReader inmodule = new BufferedReader(new InputStreamReader(fismodule));
    							String thisModuleLine=null;
    
    							System.out.println("checking module filename");
    							if(inModuleFile.exists())
    							{
    								while ((thisModuleLine = inmodule.readLine()) !=null)
    								{
    									String issuetypefilename = thisLine+"_"+thisProjectLine+"_"+thisModuleLine+".txt";
    
    									System.out.println(issuetypefilename);
    									File inIssueTypeFile = new File(issuetypefilename);
    									FileInputStream inissuetype = new FileInputStream(inIssueTypeFile); 
    									System.out.println("creating obj i/p stream");
    									ObjectInputStream ois = new ObjectInputStream(inissuetype); 
    									System.out.println("created obj i/p stream");
    									IssueBean issuebean = new IssueBean();
    									Object obj = null;
    
    									System.out.println("checking issuetype name");
    
    									if(inIssueTypeFile.exists())
    									{
    										try{
    											while ((obj = ois.readObject()) != null)
    
    											{
    												if (obj instanceof IssueBean) 
    												{
    													System.out.println(((IssueBean) obj).toString());
    													IssueBean issue = (IssueBean) obj;
    													System.out.println(issue.getEmp_id());
    													System.out.println("Checking employee id");
    													if (issue.getEmp_id().equals(empid))
    													{
    														count++;
    														System.out.println("Count" + count);
    
    													}
    												}
    											}
    										}catch(EOFException eof) {
    											System.out.println("End----------");
    										}
    										ois.close(); 
    									}
    									else
    									{
    
    										System.out.println("Exception Added");
    									}
    								}
    							}
    							else
    							{
    								resFormData.put("Exception","No module File");
    								System.out.println("Exception Added");
    							}
    							try{
    								if(thisProjectLine!=null)
    									System.out.println(thisProjectLine);
    								modulecount.put(thisProjectLine,new Integer(count));
    							}catch (NullPointerException e){
    								System.out.println("NullPointerException");
    							}
    						}
    					} 
    					else
    					{
    						resFormData.put("Exception","No Project File"); 
    						System.out.println("Exception Added");
    					}
    
    					resFormData.put(thisLine, modulecount);
    				}
    			}
    			else
    			{
    				resFormData.put("Exception","No Project.txt File"); 
    				System.out.println("Exception Added");
    			}
    		}
    
    		catch(FileNotFoundException fnf)
    		{
    			Exception.put("FileNotFoundException","No Project File Found");
    			resFormData.put("Exception",Exception);
    		}
    
    		finally
    		{
    			if(in!=null)
    				in.close();
    
    		}
    		System.out.println("Returning resFormData in the end");
    		Set set1=resFormData.keySet();
    		Iterator it1=set1.iterator();
    		Collection col1=resFormData.values();
    		Iterator it2=col1.iterator();
    		while(it1.hasNext())
    		{
    			System.out.println(it1.next());
    
    			while(it2.hasNext()){
    				System.out.println(it2.next());
    			}
    		}
    		return resFormData;
    	}
    
    	public Hashtable reportsCalendar(Hashtable reqFormData) throws IOException,ClassNotFoundException
    	{
    		System.out.println("Entering ReportsNAme method");
    		String fromDate=(String) reqFormData.get("fromdate");
    		String toDate=(String) reqFormData.get("todate");
    		System.out.println("From Date:" + fromDate);
    		System.out.println("To Date:" + toDate);
    
    		Calendar cal1 = Calendar.getInstance();
    		Calendar cal2 = Calendar.getInstance();
    		Calendar cal3 = Calendar.getInstance();
    		String[] tempfrom_date=fromDate.split("-");
    		int year1 = Integer.parseInt(tempfrom_date[2]); 
    		int month1 = Integer.parseInt(tempfrom_date[1]); 
    		int day1 = Integer.parseInt(tempfrom_date[0]);
    
    		String[] tempto_date=toDate.split("-");
    		int year2 = Integer.parseInt(tempto_date[2]); 
    		int month2 = Integer.parseInt(tempto_date[1]); 
    		int day2 = Integer.parseInt(tempto_date[0]);
    
    		cal1.set(year1,month1,day1); 
    		cal2.set(year2,month2,day2);
    
    		String filename = "project.txt";
    		File inFile = new File(filename);
    		FileInputStream fis = new FileInputStream(inFile);
    		BufferedReader in = new BufferedReader(new InputStreamReader(fis));
    		TreeMap<String, Integer> modulecount = new TreeMap<String, Integer>();
    		Hashtable resFormData = new Hashtable();
    		String thisLine = null;
    
    		try
    		{
    			System.out.println("Checking Project.txt");
    
    			if(inFile.exists())
    			{
    				System.out.println("Project.txt checked");
    				while ((thisLine = in.readLine()) != null) 
    				{
    					String projectfilename = thisLine+".txt";
    					System.out.println(projectfilename);
    
    					File inProjectFile = new File(projectfilename);
    					FileInputStream fisproject = new FileInputStream(inProjectFile);
    					BufferedReader inproject = new BufferedReader(new InputStreamReader(fisproject));
    					String thisProjectLine=null;
    
    					System.out.println("Checking project filename");
    					modulecount= new TreeMap();
    					if(inProjectFile.exists())
    					{
    						while ((thisProjectLine = inproject.readLine()) !=null)
    						{
    							int count = 0;
    							String modulefilename =thisLine+"_"+thisProjectLine+".txt";
    							File inModuleFile = new File(modulefilename);
    							FileInputStream fismodule = new FileInputStream(inModuleFile);
    							BufferedReader inmodule = new BufferedReader(new InputStreamReader(fismodule));
    							String thisModuleLine=null;
    
    							System.out.println("checking module filename");
    							if(inModuleFile.exists())
    							{
    								while ((thisModuleLine = inmodule.readLine()) !=null)
    								{
    									String issuetypefilename = thisLine+"_"+thisProjectLine+"_"+thisModuleLine+".txt";
    
    									System.out.println(issuetypefilename);
    									File inIssueTypeFile = new File(issuetypefilename);
    									FileInputStream inissuetype = new FileInputStream(inIssueTypeFile); 
    									System.out.println("creating obj i/p stream");
    									ObjectInputStream ois = new ObjectInputStream(inissuetype); 
    									System.out.println("created obj i/p stream");
    									IssueBean issuebean = new IssueBean();
    									Object obj = null;
    
    									System.out.println("checking issuetype name");
    
    									if(inIssueTypeFile.exists())
    									{
    										try{
    											while ((obj = ois.readObject()) != null)
    
    											{
    												if (obj instanceof IssueBean) 
    												{
    
    													System.out.println(((IssueBean) obj).toString());
    													IssueBean issue = (IssueBean) obj;
    													System.out.println(issue.getIssuedate());
    
    													String issuedate=(String) issue.getIssuedate(); 
    													String[] tempissue_date=issuedate.split("/");
    													int year3 = Integer.parseInt(tempissue_date[2]);
    													int day3 = Integer.parseInt(tempissue_date[1]);
    													int month3 = Integer.parseInt(tempissue_date[0]);
    
    													cal3.set(year3,month3,day3);
    													System.out.println("Checking date");
    
    													if (cal3.equals(cal1)) { 
    														count++;
    													}
    													else if(cal3.equals(cal2)){
    														count++;
    													}
    													else if( cal3.after(cal1) && cal3.before(cal2)) {
    														count++;
    													}
    
    												}
    											}
    										}catch(EOFException eof) {
    											System.out.println("End----------");
    										}
    										ois.close(); 
    									}
    									else
    									{
    
    										System.out.println("Exception Added");
    									}
    								}
    							}
    							else
    							{
    								resFormData.put("Exception","No module File");
    								System.out.println("Exception Added");
    							}
    							try{
    								if(thisProjectLine!=null)
    									System.out.println(thisProjectLine);
    								modulecount.put(thisProjectLine,new Integer(count));
    							}catch (NullPointerException e){
    								System.out.println("NullPointerException");
    							}
    						}
    					} 
    					else
    					{
    						resFormData.put("Exception","No Project File"); 
    						System.out.println("Exception Added");
    					}
    
    					resFormData.put(thisLine, modulecount);
    				}
    			}
    			else
    			{
    				resFormData.put("Exception","No Project.txt File"); 
    				System.out.println("Exception Added");
    			}
    		}
    
    		catch(FileNotFoundException fnf)
    		{
    			Exception.put("FileNotFoundException","No Project File Found");
    			resFormData.put("Exception",Exception);
    		}
    
    		finally
    		{
    			if(in!=null)
    				in.close();
    
    		}
    		System.out.println("Returning resFormData in the end");
    		Set set1=resFormData.keySet();
    		Iterator it1=set1.iterator();
    		Collection col1=resFormData.values();
    		Iterator it2=col1.iterator();
    		while(it1.hasNext())
    		{
    			System.out.println(it1.next());
    
    			while(it2.hasNext()){
    				System.out.println(it2.next());
    			}
    		}
    		return resFormData;
    	}
    }
     
  2. sameer_havakajoka

    sameer_havakajoka New Member

    Joined:
    Sep 14, 2009
    Messages:
    271
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Sleeping
    Location:
    Hava Ke Paro Me
    yeyeye...my first article got published...yeyeye...thank you dear admin
     
  3. sameer_havakajoka

    sameer_havakajoka New Member

    Joined:
    Sep 14, 2009
    Messages:
    271
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Sleeping
    Location:
    Hava Ke Paro Me
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Stats shows that
     
  5. sameer_havakajoka

    sameer_havakajoka New Member

    Joined:
    Sep 14, 2009
    Messages:
    271
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Sleeping
    Location:
    Hava Ke Paro Me
    144 views...thanks dear admin
     
  6. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Valuable information.
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
  8. technica

    technica New Member

    Joined:
    Dec 15, 2007
    Messages:
    107
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    The article is good but little more elaborated information on the given code would help people more.
     
  9. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    HaHA! I have already seen the great thing!!!
     
  10. sameer_havakajoka

    sameer_havakajoka New Member

    Joined:
    Sep 14, 2009
    Messages:
    271
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Sleeping
    Location:
    Hava Ke Paro Me
    thanks to both of you buddies
     
  11. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for this site very helpful.
     

Share This Page