1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package graphical;
25 
26 import java.util.ArrayList;
27 
28 /**
29  *
30  * @author ll93751
31  */
32 public class Office implements IOffice
33 {
34     private ParameterHelper m_aParameterHelper;
35     private String m_sDocumentName;
36     private String m_sResult;
37     private IOffice m_aOffice = null;
38 
Office(ParameterHelper _aParam, String _sResult)39     public Office(ParameterHelper _aParam, String _sResult)
40     {
41         m_aParameterHelper = _aParam;
42         m_sResult = _sResult;
43 
44         if (_aParam.getReferenceType().toLowerCase().equals("ooo") ||
45             _aParam.getReferenceType().toLowerCase().equals("o3") ||
46             _aParam.getReferenceType().toLowerCase().equals("ps") ||
47             _aParam.getReferenceType().toLowerCase().equals("pdf"))
48         {
49             m_aOffice = new OpenOfficePostscriptCreator(_aParam, m_sResult);
50         }
51         else if (_aParam.getReferenceType().toLowerCase().equals("msoffice"))
52         {
53             m_aOffice = new MSOfficePostscriptCreator(_aParam, m_sResult);
54         }
55     }
56 
57 
58     /**
59      * Load a document with an already started Office.
60      * @param _sDocumentName
61      * @throws graphical.OfficeException
62      */
load(String _sDocumentName)63     public void load(String _sDocumentName) throws OfficeException
64     {
65         m_sDocumentName = _sDocumentName;
66         // check if given file is a picture, then do nothing
67         String sDocumentSuffix = FileHelper.getSuffix(m_sDocumentName);
68         if (sDocumentSuffix.toLowerCase().endsWith(".png") ||
69             sDocumentSuffix.toLowerCase().endsWith(".gif") ||
70             sDocumentSuffix.toLowerCase().endsWith(".jpg") ||
71             sDocumentSuffix.toLowerCase().endsWith(".bmp"))
72         {
73             throw new OfficeException("The given document is not a document type.");
74         }
75 
76         // TODO: we should start the office after we know if we really need an Office.
77         if (m_aOffice != null)
78         {
79             if (sDocumentSuffix.toLowerCase().endsWith(".odb"))
80             {
81                 if (m_aParameterHelper.getReferenceType().toLowerCase().equals("msoffice"))
82                 {
83                     // we can't handle .odb with msoffice
84                     return;
85                 }
86                 // TODO: run through all documents which exists as reports in odb files
87                 OpenOfficeDatabaseReportExtractor aExtractor = new OpenOfficeDatabaseReportExtractor(m_aParameterHelper);
88                 ArrayList aList = aExtractor.load(m_sDocumentName);
89                 if (aList != null)
90                 {
91                     // remove the whole section about the 'name'.odb there are no information we need
92                     // we will create a new one.
93                     String sIniFile = FileHelper.appendPath(m_sResult, "index.ini");
94                     IniFile aIniFile2 = new IniFile(sIniFile);
95                     String sSection = FileHelper.getBasename(_sDocumentName); // name of the odb file
96                     aIniFile2.removeSection(sSection);
97                     aIniFile2.close();
98 
99                     for (int i=0; i<aList.size();i++)
100                     {
101                         String sDocumentName = (String)aList.get(i);
102                         m_aOffice.load(sDocumentName);
103                         m_aOffice.storeAsPostscript();
104 
105 
106                         // foreach Report found in the .odb file, create an entry 'report'<number> in the original <name>.odb Section
107                         // so it is possible to run through all reports by the given .odb name
108                         IniFile aIniFile = new IniFile(sIniFile);
109                         // String sSection = FileHelper.getBasename(_sDocumentName); // name of the odb file
110                         int nFileCount = aIniFile.getIntValue(sSection, "reportcount", 0);
111                         String sValue = FileHelper.getBasename(sDocumentName);    // name of the corresponding report
112                         aIniFile.insertValue(sSection, "report" + nFileCount, sValue);
113                         aIniFile.insertValue(sSection, "reportcount", nFileCount + 1);
114                         aIniFile.close();
115                     }
116                 }
117                 else
118                 {
119                     throw new OfficeException("Can't open the document " + m_sDocumentName);
120                 }
121             }
122             else
123             {
124                 m_aOffice.load(_sDocumentName);
125             }
126         }
127     }
128 
storeAsPostscript()129     public void storeAsPostscript() throws OfficeException
130     {
131         if (m_aOffice != null)
132         {
133             if (m_sDocumentName.endsWith(".odb"))
134             {
135                 // this has already be done by load() for odb files.
136             }
137             else
138             {
139                 m_aOffice.storeAsPostscript();
140             }
141 
142 //          FileHelper.addBasenameToIndex(sOutputFilename);
143         }
144     }
145 
start()146     public void start() throws OfficeException
147     {
148         if (m_aOffice != null)
149         {
150             m_aOffice.start();
151         }
152     }
153 
close()154     public void close() throws OfficeException
155     {
156         if (m_aOffice != null)
157         {
158             m_aOffice.close();
159         }
160     }
161 
162 
163 
164 
165 }
166