Skip to content

Commit

Permalink
SAK-41379: Hot Spot questions don't appear when printing PDF (sakaipr…
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusmmp authored and ern committed Apr 4, 2019
1 parent 067abbd commit 41b2801
Showing 1 changed file with 101 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ public String prepDocumentPDF() {
if (!(TypeIfc.FILL_IN_BLANK.equals(item.getItemData().getTypeId()) || TypeIfc.FILL_IN_NUMERIC.equals(item.getItemData().getTypeId())
|| TypeIfc.CALCULATED_QUESTION.equals(item.getItemData().getTypeId()))) {
contentBuffer.append("<br />");
contentBuffer.append(convertFormattedText(item.getItemData().getText()));
if (StringUtils.isNotEmpty(item.getItemData().getText())) {
contentBuffer.append(convertFormattedText(item.getItemData().getText()));
}
contentBuffer.append("<br />");
}
if (item.getItemData().getItemAttachmentList() != null && item.getItemData().getItemAttachmentList().size() > 0) {
Expand Down Expand Up @@ -478,6 +480,21 @@ public String prepDocumentPDF() {
contentBuffer.append("</table>");
}
}

if (item.getItemData().getTypeId().equals(TypeIfc.IMAGEMAP_QUESTION)) {
List<ItemTextIfc> question = item.getItemData().getItemTextArraySorted();
contentBuffer.append("<ul>");
for (int k=0; k<question.size(); k++) {
PublishedItemText itemtext = (PublishedItemText)question.get(k);
contentBuffer.append("<li>");
contentBuffer.append(k+1 + "." + itemtext.getText());
contentBuffer.append("</li>");
}
contentBuffer.append("</ul>");
contentBuffer.append("<br />");
contentBuffer.append(getContentQuestionImageMap(item, printSetting, false));
}

if (item.getItemData().getTypeId().equals(TypeIfc.MATCHING)) {
contentBuffer.append("<table cols='20' width='100%'>");
List question = item.getMatchingArray();
Expand Down Expand Up @@ -682,58 +699,8 @@ else if (item.getItemData().getTypeId().equals(TypeIfc.ESSAY_QUESTION)) {
h = img_in.getHeight(null);
} catch (IOException e) {
log.error(e.getMessage(), e);
}

Color c = new Color(27, 148, 224, 80);
java.awt.Font font = new java.awt.Font("Serif", java.awt.Font.BOLD, 10);

//print areas over the image
BufferedImage img_out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
try {
Graphics g = img_out.getGraphics();
g.setFont(font);
g.drawImage(img_in, 0, 0, null);

List question = item.getItemData().getItemTextArraySorted();
for (int k=0; k<question.size(); k++) {
PublishedItemText itemtext = (PublishedItemText)question.get(k);
List answers=itemtext.getAnswerArray();
PublishedAnswer answer = (PublishedAnswer)answers.get(0);

String area = answer.getText();
Integer areax1=Integer.valueOf(area.substring(area.indexOf("\"x1\":")+5,area.indexOf(",", area.indexOf("\"x1\":"))));
Integer areay1=Integer.valueOf(area.substring(area.indexOf("\"y1\":")+5,area.indexOf(",", area.indexOf("\"y1\":"))));
Integer areax2=Integer.valueOf(area.substring(area.indexOf("\"x2\":")+5,area.indexOf(",", area.indexOf("\"x2\":"))));
Integer areay2=Integer.valueOf(area.substring(area.indexOf("\"y2\":")+5,area.indexOf("}", area.indexOf("\"y2\":"))));

g.setColor(c);
g.fillRect(areax1, areay1, (areax2-areax1), (areay2-areay1));

g.setColor(Color.WHITE);
g.drawRect(areax1, areay1, (areax2-areax1), (areay2-areay1));

g.setColor(Color.BLACK);
g.drawString(""+(k+1), areax2-13, areay1+13);
}
g.dispose();
} catch (Exception e) {
log.error(e.getMessage(), e);
}


String destSrc = "temp://";
try {
File temp = File.createTempFile("imgtemp", "."+ext);
ImageIO.write(img_out, ext, temp);
destSrc+=temp.getCanonicalPath();
} catch (IOException e) {
log.error(e.getMessage(), e);
}

//print it
contentBuffer.append(" <img src=\"");
contentBuffer.append(destSrc);
contentBuffer.append("\" />");
contentBuffer.append(getContentQuestionImageMap(item, printSetting, true));
}
else if(TypeIfc.CALCULATED_QUESTION.equals( item.getItemData().getTypeId() )){
contentBuffer.append(item.getAnswerKeyCalcQuestion());
Expand Down Expand Up @@ -786,6 +753,88 @@ else if(TypeIfc.CALCULATED_QUESTION.equals( item.getItemData().getTypeId() )){
return contentBuffer.toString();
}

public String getContentQuestionImageMap(ItemContentsBean item, PrintSettingsBean printSetting, boolean renderMap) {
StringBuffer contentBufferMap = new StringBuffer("");
//look for path on metadata
String imsrc=item.getItemData().getItemMetaDataByLabel("IMAGE_MAP_SRC");
if (imsrc==null)
imsrc="";

imsrc=imsrc.replaceAll(" ", "%20");

String ext = "";
if (imsrc.lastIndexOf('.') > 0)
ext = imsrc.substring(imsrc.lastIndexOf('.')+1);

BufferedImage img_in=null;
int w=-1;
int h=-1;
try {
URL url = new URL(ServerConfigurationService.getServerUrl()+imsrc);
img_in = ImageIO.read(url);
w = img_in.getWidth(null);
h = img_in.getHeight(null);
} catch (IOException e) {
log.error(e.getMessage(), e);
}

if (renderMap) {

Color c = new Color(27, 148, 224, 80);
java.awt.Font font = new java.awt.Font("Serif", java.awt.Font.BOLD, 10);

//print areas over the image
BufferedImage img_out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
try {
Graphics g = img_out.getGraphics();
g.setFont(font);
g.drawImage(img_in, 0, 0, null);

List<ItemTextIfc> question = item.getItemData().getItemTextArraySorted();
for (int k=0; k<question.size(); k++) {
PublishedItemText itemtext = (PublishedItemText)question.get(k);
List<PublishedAnswer> answers=itemtext.getAnswerArray();
PublishedAnswer answer = (PublishedAnswer)answers.get(0);

String area = answer.getText();
Integer areax1=Integer.valueOf(area.substring(area.indexOf("\"x1\":")+5,area.indexOf(",", area.indexOf("\"x1\":"))));
Integer areay1=Integer.valueOf(area.substring(area.indexOf("\"y1\":")+5,area.indexOf(",", area.indexOf("\"y1\":"))));
Integer areax2=Integer.valueOf(area.substring(area.indexOf("\"x2\":")+5,area.indexOf(",", area.indexOf("\"x2\":"))));
Integer areay2=Integer.valueOf(area.substring(area.indexOf("\"y2\":")+5,area.indexOf("}", area.indexOf("\"y2\":"))));

g.setColor(c);
g.fillRect(areax1, areay1, (areax2-areax1), (areay2-areay1));

g.setColor(Color.WHITE);
g.drawRect(areax1, areay1, (areax2-areax1), (areay2-areay1));

g.setColor(Color.BLACK);
g.drawString(""+(k+1), areax2-13, areay1+13);
}
g.dispose();
img_in = img_out;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}

try {
File temp = File.createTempFile("imgtemp", "." + ext);
temp.deleteOnExit();
ImageIO.write(img_in, ext, temp);
contentBufferMap.append("<img src=\"temp://").append(temp.getCanonicalPath());
contentBufferMap.append("\" alt=\"");
if (StringUtils.isNotBlank(item.getImageAltText())) {
contentBufferMap.append(item.getImageAltText());
}
contentBufferMap.append("\" />");
} catch (IOException e) {
log.warn("Could not create a temporary file for pdf image {}", e.getMessage(), e);
}

return contentBufferMap.toString();
}

public String convertFormattedText(String text) {
//To preserve old behavior, set this to true
//Possibly consider using jsoup with a whitelist so some formatted text gets though, I'm not sure why this was here in the first place
Expand Down

0 comments on commit 41b2801

Please sign in to comment.