log4j implemented for the producer subsystem
[mir.git] / source / mircoders / producer / PDFPreFormattingProducerNode.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.producer;
33
34 import java.util.*;
35 import java.io.*;
36
37 import mir.util.*;
38 import mir.producer.*;
39 import mir.entity.*;
40 import mir.entity.adapter.*;
41 import mir.log.*;
42
43 import mircoders.entity.*;
44 import mircoders.storage.*;
45
46 //because images are returned as a template model!(maybe not needed after all!)
47 //import freemarker.template.*;
48
49
50 public class PDFPreFormattingProducerNode implements ProducerNode {
51   private String contentKey;
52   private int numLinesBetweenImages;
53   private float contentAreaWidthCM;
54   private float characterWidthCM;
55   private float pixelWidthCM;
56   private float lineHeightCM;
57
58   public PDFPreFormattingProducerNode(String aContentKey,String someNumLinesBetweenImages,String aContentAreaWidthCM,String aCharacterWidthCM,String aPixelWidthCM,String aLineHeightCM) {
59     contentKey = aContentKey;
60     numLinesBetweenImages=(new Integer(someNumLinesBetweenImages)).intValue();
61     contentAreaWidthCM=(new Float(aContentAreaWidthCM)).floatValue();
62     characterWidthCM=(new Float(aCharacterWidthCM)).floatValue();
63     pixelWidthCM=(new Float(aPixelWidthCM)).floatValue();
64     lineHeightCM=(new Float(aLineHeightCM)).floatValue();
65
66     //    float characterWidthCM = 0.17F;
67     //float contentAreaWidthCM = 16;
68     //float pixelWidthCM = .03F;
69     //float lineHeightCM = .5F;
70   }
71
72   public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
73     Object data;
74     Entity entity;
75
76     try {
77       data = ParameterExpander.findValueForKey( aValueMap, contentKey );
78
79       if (! (data instanceof EntityAdapter)) {
80         throw new ProducerFailure("ContentMarkingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
81       }
82
83       entity = ((EntityAdapter) data).getEntity();
84       if (! (entity instanceof EntityContent)) {
85         throw new ProducerFailure("ContentMarkingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
86       }
87
88       int currentPosition = 0;
89
90       //int numLinesBetweenImages=3;
91
92
93
94
95       int numCharsInAnImagelessRow = (new Float(numLinesBetweenImages * (contentAreaWidthCM/characterWidthCM))).intValue();
96
97       boolean outOfText = false;
98
99       ArrayList brokenUpContent = new ArrayList();
100
101
102       EntityList images=DatabaseContentToMedia.getInstance().getImages((EntityContent)entity);
103       String theContent = ((EntityContent) entity).getValue("content_data");
104       if (images == null){
105           HashMap row = new HashMap();
106           row.put("text",theContent);
107           row.put("hasImage","0");
108           brokenUpContent.add(row);
109       }
110       if (images != null){
111           //need to add checks for out of content!
112           HashMap row0 = new HashMap();
113           if (numCharsInAnImagelessRow>(theContent).length()){
114               row0.put("text",theContent);
115               outOfText = true;
116           }
117           else {
118               //break on words so we don't split html entities
119               int lastSpaceAt = theContent.lastIndexOf(" ",numCharsInAnImagelessRow);
120               row0.put("text",theContent.substring(0,lastSpaceAt));
121               currentPosition=lastSpaceAt;
122           }
123           row0.put("hasImage","0");
124           brokenUpContent.add(row0);
125           aLogger.debug("CP1 is "+ currentPosition);
126           while(images.hasNext()){
127               HashMap row1 = new HashMap();
128               HashMap row2 = new HashMap();
129               EntityImages currentImage=(EntityImages) images.next();
130               float img_width=(new Float(currentImage.getValue("img_width"))).floatValue();
131               float img_height=(new Float(currentImage.getValue("img_height"))).floatValue();
132
133               //oversize images must be shrunk
134               if (img_width>400){
135                   img_height=(new Float((new Float(img_height*(400.0F/img_width))).intValue())).floatValue();
136                   img_width=400.0F;
137               }
138
139
140               //calculate how much text goes in the column(use 8 pixels to pad the column)
141               float text_widthCM = contentAreaWidthCM-((img_width+8)*pixelWidthCM);
142               float number_of_lines = img_height*pixelWidthCM/lineHeightCM; //don't worry we will make it an int
143               //add one line for image description
144               int text_amount= (new Float((text_widthCM/characterWidthCM)*(number_of_lines+1))).intValue();
145
146               row1.put("text_widthCM",Float.toString(text_widthCM));
147
148               row1.put("img_title",currentImage.getValue("title"));
149
150               row1.put("img_width",Float.toString(img_width));
151               row1.put("img_height",Float.toString(img_height));
152
153               aLogger.debug("img_width " +Float.toString(img_width));
154               aLogger.debug("img_height "+Float.toString(img_height));
155
156               row1.put("img_src",currentImage.getValue("publish_path"));
157               row1.put("hasImage","1");
158               if (! outOfText){
159                   try {
160                       int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);
161                       row1.put("text",theContent.substring(currentPosition,lastSpaceAt));
162                       currentPosition=lastSpaceAt;
163                   }
164                   catch (IndexOutOfBoundsException e){
165                       row1.put("text",theContent.substring(currentPosition));
166                       outOfText = true;
167                           }
168               }
169               aLogger.debug("CP2 is "+ currentPosition);
170               brokenUpContent.add(row1);
171
172               if (! outOfText){
173                   try {
174                       int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);
175                       row2.put("text",theContent.substring(currentPosition,lastSpaceAt));
176                       currentPosition=lastSpaceAt;
177                   }
178                   catch (IndexOutOfBoundsException e){
179                       row2.put("text",theContent.substring(currentPosition));
180                       outOfText = true;
181                           }
182               }
183               row2.put("hasImage","0");
184               brokenUpContent.add(row2);
185
186               aLogger.debug("CP3 is "+ currentPosition);
187           }
188           HashMap row3 = new HashMap();
189           if (! outOfText){
190               row3.put("text",theContent.substring(currentPosition));
191               row3.put("hasImage","0");
192               brokenUpContent.add(row3);
193           }
194
195       }
196
197
198
199
200
201       ParameterExpander.setValueForKey(
202                                        aValueMap,
203                                        "data.formatted_content",
204                                        new CachingRewindableIterator(brokenUpContent.iterator())
205                                        );
206
207
208     }
209     catch (Throwable t) {
210       aLogger.error("Error while formatting content for PDF: " + t.getMessage());
211       t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));
212     }
213   }
214 }
215
216
217
218