package pt.estgp.estgweb.services.assessments;
import com.itextpdf.text.*;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import jomm.utils.FilesUtils;
import org.apache.log4j.Logger;
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.domain.*;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.domain.views.AssessmentView;
import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum;
import pt.estgp.estgweb.services.data.RepositoryService;
import pt.estgp.estgweb.web.utils.DatesUtils;
import pt.utl.ist.berserk.logic.serviceManager.IService;
import java.io.*;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Filipe Matos
* Date: 6/Ago/2013
* Time: 2:20:23
* To change this template use File | Settings | File Templates.
*/
public class GeneratePdfService
implements IService
{
private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger =
1.5.0/docs/api/java/util/logging/Logger.html">Logger.
getLogger(GeneratePdfService.
class);
private RepositoryService repositoryService =
new RepositoryService
();
public void generateAssessmentPdf
(long assessmentId,UserSession userSession
) throws 1.5.0/docs/api/java/io/IOException.html">IOException, DocumentException
{
Assessment assessment = DaoFactory.
getAssessmentDaoImpl().
get(assessmentId
);
////////////////////////////////////////////////
5+0%2Fdocs%2Fapi+Document">Document doc =
null;
5+0%2Fdocs%2Fapi+OutputStream">OutputStream os =
null;
try {
//cria o documento tamanho A4, margens de 2,54cm
doc =
new 5+0%2Fdocs%2Fapi+Document">Document(PageSize.
A4, 72, 72, 72, 72
);
//cria a stream de sa�da
os =
new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(Globals.
TMP_DIR+
"Enunciado"+assessment.
getTitle()+
".pdf");
//associa a stream de sa�da ao
PdfWriter.
getInstance(doc, os
);
//abre o documento
doc.
open();
//imagem do cabe�alho
1.5.0/docs/api/java/awt/Image.html">Image img =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/logoestg.gif");
img.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_LEFT);
doc.
add(img
);
//paragrafo em branco
doc.
add(new Phrase
(" "));
//fonts
//primeira linha para o caracteres especiais
// BaseFont specialCharacter = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
1.5.0/docs/api/java/awt/Font.html">Font fontHeader =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,14,
1.5.0/docs/api/java/awt/Font.html">Font.
BOLD);
1.5.0/docs/api/java/awt/Font.html">Font fontBold =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,10,
1.5.0/docs/api/java/awt/Font.html">Font.
BOLD);
1.5.0/docs/api/java/awt/Font.html">Font fontNormal =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,10,
1.5.0/docs/api/java/awt/Font.html">Font.
NORMAL);
1.5.0/docs/api/java/awt/Font.html">Font fontTextBold =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,12,
1.5.0/docs/api/java/awt/Font.html">Font.
BOLD);
1.5.0/docs/api/java/awt/Font.html">Font fontTextNormal =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,12,
1.5.0/docs/api/java/awt/Font.html">Font.
NORMAL);
//cabe�alho
//unidade curricular
Paragraph courseUnit =
new Paragraph
(assessment.
getCourseUnit().
getName(),fontHeader
);
courseUnit.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_CENTER);
doc.
add(courseUnit
);
//primeira linha cabe�alho
1.5.0/docs/api/java/lang/String.html">String evaluationTime =
"";
if(assessment.
getEvaluationTime().
equals(Globals.
ASSESSMENT_EVALUATION_TIME_CONTINUOUS))
evaluationTime=
"Avalia��o Cont�nua";
if(assessment.
getEvaluationTime().
equals(Globals.
ASSESSMENT_EVALUATION_TIME_FIRSTEXAM))
evaluationTime=
"Exame Normal";
if(assessment.
getEvaluationTime().
equals(Globals.
ASSESSMENT_EVALUATION_TIME_SECONDEXAM))
evaluationTime=
"Exame Recurso";
if(assessment.
getEvaluationTime().
equals(Globals.
ASSESSMENT_EVALUATION_TIME_THIRDEXAM))
evaluationTime=
"Exame Especial";
1.5.0/docs/api/java/lang/String.html">String yearText =
" - Ano lectivo";
1.5.0/docs/api/java/lang/String.html">String year =
" - "+assessment.
getYear();
1.5.0/docs/api/java/lang/String.html">String course =
" - "+assessment.
getCourseUnit().
getCourseName();
Paragraph firstLine =
new Paragraph
();
firstLine.
add(new Chunk
(evaluationTime,fontBold
));
firstLine.
add(new Chunk
(yearText,fontNormal
));
firstLine.
add(new Chunk
(year,fontNormal
));
firstLine.
add(new Chunk
(course,fontNormal
));
firstLine.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_CENTER);
doc.
add(firstLine
);
//segunda linha cabe�alho
1.5.0/docs/api/java/lang/String.html">String dateText =
"Data:";
1.5.0/docs/api/java/lang/String.html">String date =
" "+ DatesUtils.
getStringFromDate(assessment.
getStartDate());
1.5.0/docs/api/java/lang/String.html">String timeText =
" - Dura��o:";
1.5.0/docs/api/java/lang/String.html">String time =
" "+
1.5.0/docs/api/java/lang/String.html">String.
valueOf(assessment.
getTime().
getHours())+
":"+
1.5.0/docs/api/java/lang/String.html">String.
valueOf(assessment.
getTime().
getMinutes());
1.5.0/docs/api/java/lang/String.html">String ownerText =
" - Docente:";
1.5.0/docs/api/java/lang/String.html">String owner =
" "+assessment.
getOwner().
getName();
Paragraph secondLine =
new Paragraph
();
secondLine.
add(new Chunk
(dateText,fontBold
));
secondLine.
add(new Chunk
(date,fontNormal
));
secondLine.
add(new Chunk
(timeText,fontBold
));
secondLine.
add(new Chunk
(time,fontNormal
));
secondLine.
add(new Chunk
(ownerText,fontBold
));
secondLine.
add(new Chunk
(owner,fontNormal
));
secondLine.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_CENTER);
doc.
add(secondLine
);
//paragrafo em branco
//doc.add(new Paragraph("Barra pah",fontNormal));
doc.
add(Chunk.
NEWLINE);
//Barra horizontal
LineSeparator objectName =
new LineSeparator
();
doc.
add(objectName
);
doc.
add(Chunk.
NEWLINE);
//quest�es
int numberQ=
0;
for(AssessmentQuestion question : assessment.
getQuestions())
{
numberQ++
;
1.5.0/docs/api/java/lang/String.html">String numberQuotation =
1.5.0/docs/api/java/lang/String.html">String.
valueOf(numberQ
) +
". ("+
1.5.0/docs/api/java/lang/String.html">String.
valueOf(question.
getQuotation())+
") ";
1.5.0/docs/api/java/lang/String.html">String questionText = question.
getTextpt();
Paragraph questionP =
new Paragraph
();
questionP.
add(new Chunk
(numberQuotation,fontTextBold
));
questionP.
add(new Chunk
(questionText,fontTextNormal
));
questionP.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_JUSTIFIED);
doc.
add(questionP
);
if(question.
getImage()!=
null)
{
1.5.0/docs/api/java/awt/Image.html">Image image =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imageStream/"+question.
getImage().
getId());
image.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_LEFT);
doc.
add(image
);
}
if(question.
getType().
equals(Globals.
ASSESSMENT_QUESTION_TYPE_RADIO) || question.
getType().
equals(Globals.
ASSESSMENT_QUESTION_TYPE_CHECK))
{
for(AssessmentQuestionItem item : question.
getQuestionItems())
{
for(AssessmentQuestionPossibleAnswer pA : item.
getPossibleAnswers())
{
1.5.0/docs/api/java/awt/Image.html">Image imagePa=
null;
if(question.
getType().
equals(Globals.
ASSESSMENT_QUESTION_TYPE_RADIO))
imagePa =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/radiobox.png");
else
imagePa =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/checkbox.png");
imagePa.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
PARAGRAPH);
Paragraph pAP =
new Paragraph
();
pAP.
add(new Chunk
(imagePa, -1.00f, -2.00f
));
pAP.
add(new Chunk
(" "+pA.
getTextpt(),fontTextNormal
));
pAP.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_JUSTIFIED);
doc.
add(pAP
);
}
}
}
if(question.
getType().
equals(Globals.
ASSESSMENT_QUESTION_TYPE_MATRIX))
{
PdfPTable table =
null;
int numPossibles=
0;
//construir tabela
//para saber o numero de respostas possiveis
for(AssessmentQuestionItem itemTam : question.
getQuestionItems())
{
numPossibles = itemTam.
getPossibleAnswers().
size()+
1;
table =
new PdfPTable
(numPossibles
);
PdfPCell colunaVazia =
new PdfPCell
(new Paragraph
());
colunaVazia.
setBorder(0
);
table.
addCell(colunaVazia
);
for(AssessmentQuestionPossibleAnswer possibleTam : itemTam.
getPossibleAnswers())
{
PdfPCell possible =
new PdfPCell
(new Paragraph
(possibleTam.
getTextpt(),fontTextNormal
));
possible.
setBorder(0
);
table.
addCell(possible
);
}
break;
}
for(AssessmentQuestionItem item : question.
getQuestionItems())
{
PdfPCell item1 =
new PdfPCell
(new Paragraph
(item.
getTextpt(),fontTextNormal
));
item1.
setBorder(0
);
table.
addCell(item1
);
for(AssessmentQuestionPossibleAnswer pA : item.
getPossibleAnswers())
{
1.5.0/docs/api/java/awt/Image.html">Image imagePa=
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/radiobox.png");
PdfPCell imageCell =
new PdfPCell
(imagePa
);
imageCell.
setBorder(0
);
table.
addCell(imageCell
);
}
}
float splitWidth = 1.0f /
(float)numPossibles
;
float[] tams =
new float[numPossibles
];
for(int i=
0;i
<numPossibles
;i++
)
{
tams
[i
] = splitWidth
;
}
table.
setWidths(tams
);
table.
setWidthPercentage(100.0f
);
doc.
add(table
);
}
doc.
add(Chunk.
NEWLINE);
}
} finally {
if (doc
!=
null) {
//fechamento do documento
doc.
close();
}
if (os
!=
null) {
//fechamento da stream de sa�da
os.
close();
}
}
//repositoryStream
1.5.0/docs/api/java/lang/String.html">String fileName = Globals.
TMP_DIR+
"Enunciado"+assessment.
getTitle()+
".pdf";
1.5.0/docs/api/java/io/File.html">File file =
new 1.5.0/docs/api/java/io/File.html">File(fileName
);
1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.
getExtension(fileName
);
1.5.0/docs/api/java/lang/String.html">String description =
"assessment.file "+assessment.
getCourseUnit().
getName();
1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream inputStream =
new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(file
);
1.5.0/docs/api/java/lang/String.html">String identifier = assessment.
getIdentifier();
if(identifier==
null)
identifier = repositoryService.
storeRepositoryFile(inputStream,
"application/pdf",extension,
(int)file.
length(),file.
getName(),description, ResourceAccessControlEnum.
publicDomain,userSession
);
else
repositoryService.
updateRepositoryFile(identifier,inputStream,
"application/pdf",extension,
(int)file.
length(),file.
getName(),description, ResourceAccessControlEnum.
publicDomain);
assessment.
setIdentifier(identifier
);
// return new AssessmentView(assessment);
}
public void generateAssessmentResponsePdf
(long assessmentResponseId,
long assessmentId,UserSession userSession
) throws 1.5.0/docs/api/java/io/IOException.html">IOException, DocumentException
{
AssessmentResponse assessmentResponse = DaoFactory.
getAssessmentResponseDaoImpl().
get(assessmentResponseId
);
Assessment assessment = DaoFactory.
getAssessmentDaoImpl().
get(assessmentId
);
////////////////////////////////////////////////
5+0%2Fdocs%2Fapi+Document">Document doc =
null;
5+0%2Fdocs%2Fapi+OutputStream">OutputStream os =
null;
try {
//cria o documento tamanho A4, margens de 2,54cm
doc =
new 5+0%2Fdocs%2Fapi+Document">Document(PageSize.
A4, 72, 72, 72, 72
);
//cria a stream de sa�da
os =
new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(Globals.
TMP_DIR+
"Resposta"+assessment.
getTitle()+
1.5.0/docs/api/java/lang/String.html">String.
valueOf(assessmentResponse.
getId())+
".pdf");
//associa a stream de sa�da ao
PdfWriter.
getInstance(doc, os
);
//abre o documento
doc.
open();
//imagem do cabe�alho
1.5.0/docs/api/java/awt/Image.html">Image img =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/logoestg.gif");
img.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_LEFT);
doc.
add(img
);
//paragrafo em branco
doc.
add(new Phrase
(" "));
//fonts
//primeira linha para o caracteres especiais
// BaseFont specialCharacter = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
1.5.0/docs/api/java/awt/Font.html">Font fontHeader =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,14,
1.5.0/docs/api/java/awt/Font.html">Font.
BOLD);
1.5.0/docs/api/java/awt/Font.html">Font fontBold =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,10,
1.5.0/docs/api/java/awt/Font.html">Font.
BOLD);
1.5.0/docs/api/java/awt/Font.html">Font fontNormal =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,10,
1.5.0/docs/api/java/awt/Font.html">Font.
NORMAL);
1.5.0/docs/api/java/awt/Font.html">Font fontTextBold =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,12,
1.5.0/docs/api/java/awt/Font.html">Font.
BOLD);
1.5.0/docs/api/java/awt/Font.html">Font fontTextNormal =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,12,
1.5.0/docs/api/java/awt/Font.html">Font.
NORMAL);
1.5.0/docs/api/java/awt/Font.html">Font fontTextEvaluate =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,12,
1.5.0/docs/api/java/awt/Font.html">Font.
NORMAL,BaseColor.
RED);
//cabe�alho
//unidade curricular
Paragraph courseUnit =
new Paragraph
(assessment.
getCourseUnit().
getName(),fontHeader
);
courseUnit.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_CENTER);
doc.
add(courseUnit
);
//primeira linha cabe�alho
1.5.0/docs/api/java/lang/String.html">String evaluationTime =
"";
if(assessment.
getEvaluationTime().
equals(Globals.
ASSESSMENT_EVALUATION_TIME_CONTINUOUS))
evaluationTime=
"Avalia��o Cont�nua";
if(assessment.
getEvaluationTime().
equals(Globals.
ASSESSMENT_EVALUATION_TIME_FIRSTEXAM))
evaluationTime=
"Exame Normal";
if(assessment.
getEvaluationTime().
equals(Globals.
ASSESSMENT_EVALUATION_TIME_SECONDEXAM))
evaluationTime=
"Exame Recurso";
if(assessment.
getEvaluationTime().
equals(Globals.
ASSESSMENT_EVALUATION_TIME_THIRDEXAM))
evaluationTime=
"Exame Especial";
1.5.0/docs/api/java/lang/String.html">String yearText =
" - Ano lectivo";
1.5.0/docs/api/java/lang/String.html">String year =
" - "+assessment.
getYear();
1.5.0/docs/api/java/lang/String.html">String course =
" - "+assessment.
getCourseUnit().
getCourseName();
Paragraph firstLine =
new Paragraph
();
firstLine.
add(new Chunk
(evaluationTime,fontBold
));
firstLine.
add(new Chunk
(yearText,fontNormal
));
firstLine.
add(new Chunk
(year,fontNormal
));
firstLine.
add(new Chunk
(course,fontNormal
));
firstLine.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_CENTER);
doc.
add(firstLine
);
//segunda linha cabe�alho
1.5.0/docs/api/java/lang/String.html">String dateText =
"Data:";
1.5.0/docs/api/java/lang/String.html">String date =
" "+ DatesUtils.
getStringFromDate(assessment.
getStartDate());
1.5.0/docs/api/java/lang/String.html">String timeText =
" - Dura��o:";
1.5.0/docs/api/java/lang/String.html">String time =
" "+
1.5.0/docs/api/java/lang/String.html">String.
valueOf(assessment.
getTime().
getHours())+
":"+
1.5.0/docs/api/java/lang/String.html">String.
valueOf(assessment.
getTime().
getMinutes());
1.5.0/docs/api/java/lang/String.html">String ownerText =
" - Docente:";
1.5.0/docs/api/java/lang/String.html">String owner =
" "+assessment.
getOwner().
getName();
1.5.0/docs/api/java/lang/String.html">String quotationText =
" - Nota:";
1.5.0/docs/api/java/lang/String.html">String quotation =
" "+
1.5.0/docs/api/java/lang/String.html">String.
valueOf(assessmentResponse.
getQuotation());
Paragraph secondLine =
new Paragraph
();
secondLine.
add(new Chunk
(dateText,fontBold
));
secondLine.
add(new Chunk
(date,fontNormal
));
secondLine.
add(new Chunk
(timeText,fontBold
));
secondLine.
add(new Chunk
(time,fontNormal
));
secondLine.
add(new Chunk
(ownerText,fontBold
));
secondLine.
add(new Chunk
(owner,fontNormal
));
if(assessmentResponse.
isEvaluated())
{
secondLine.
add(new Chunk
(quotationText,fontBold
));
secondLine.
add(new Chunk
(quotation,fontNormal
));
}
secondLine.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_CENTER);
doc.
add(secondLine
);
//paragrafo em branco
//doc.add(new Paragraph("Barra pah",fontNormal));
doc.
add(Chunk.
NEWLINE);
//Barra horizontal
LineSeparator objectName =
new LineSeparator
();
doc.
add(objectName
);
doc.
add(Chunk.
NEWLINE);
//quest�es
int numberQ=
0;
for(AssessmentQuestion question : assessment.
getQuestions())
{
numberQ++
;
1.5.0/docs/api/java/lang/String.html">String numberQuotation =
1.5.0/docs/api/java/lang/String.html">String.
valueOf(numberQ
) +
". ("+
1.5.0/docs/api/java/lang/String.html">String.
valueOf(question.
getQuotation())+
") ";
1.5.0/docs/api/java/lang/String.html">String questionText = question.
getTextpt();
Paragraph questionP =
new Paragraph
();
questionP.
add(new Chunk
(numberQuotation,fontTextBold
));
questionP.
add(new Chunk
(questionText,fontTextNormal
));
questionP.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_JUSTIFIED);
doc.
add(questionP
);
if(question.
getImage()!=
null)
{
1.5.0/docs/api/java/awt/Image.html">Image image =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imageStream/"+question.
getImage().
getId());
image.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_LEFT);
doc.
add(image
);
}
if(question.
getType().
equals(Globals.
ASSESSMENT_QUESTION_TYPE_TEXT))
{
for(AssessmentQuestionAnswer answer : assessmentResponse.
getAnswers())
{
if(answer.
getPossibleAnswer().
getQuestionItem().
getQuestion()== question
)
{
Paragraph answerP =
new Paragraph
();
answerP.
add(new Chunk
("Resposta: ",fontTextBold
));
answerP.
add(new Chunk
(answer.
getTextAnswer(), fontTextNormal
));
if(assessmentResponse.
isEvaluated())
{ //verifica se est� correcta
if(question.
getQuotation()==answer.
getQuotation())
{
answerP.
add(new Chunk
(" Correcta",fontTextEvaluate
));
if(answer.
getNote() !=
null && answer.
getNote().
trim().
length() !=0
)
answerP.
add(new Chunk
(" Obs:"+answer.
getNote(),fontTextEvaluate
));
}
else if(answer.
getQuotation()==0
)
{
answerP.
add(new Chunk
(" Errada",fontTextEvaluate
));
if(answer.
getNote() !=
null && answer.
getNote().
trim().
length() !=0
)
answerP.
add(new Chunk
(" Obs:"+answer.
getNote(),fontTextEvaluate
));
}
else
{
// double quotationA = (answer.getQuotation()*100)/question.getQuotation();
double quotationA = answer.
getQuotation();
answerP.
add(new Chunk
(" Incompleta("+quotationA+
")",fontTextEvaluate
));
if(answer.
getNote() !=
null && answer.
getNote().
trim().
length() !=0
)
answerP.
add(new Chunk
(" Obs:"+answer.
getNote(),fontTextEvaluate
));
}
}
doc.
add(answerP
);
break;
}
}
}
if(question.
getType().
equals(Globals.
ASSESSMENT_QUESTION_TYPE_PRACTICAL))
{
for(AssessmentQuestionAnswer answer : assessmentResponse.
getAnswers())
{
if(answer.
getPossibleAnswer().
getQuestionItem().
getQuestion()== question
)
{
if(answer.
getIdentifier()!=
null && answer.
getIdentifier().
trim().
length() !=0
)
{
Paragraph answerP =
new Paragraph
("Foi entregue um ficheiro", fontTextBold
);
if(assessmentResponse.
isEvaluated())
{ //verifica se est� correcta
if(question.
getQuotation()==answer.
getQuotation())
{
answerP.
add(new Chunk
(" Correcta",fontTextEvaluate
));
if(answer.
getNote() !=
null && answer.
getNote().
trim().
length() !=0
)
answerP.
add(new Chunk
(" Obs:"+answer.
getNote(),fontTextEvaluate
));
}
else if(answer.
getQuotation()==0
)
{
answerP.
add(new Chunk
(" Errada",fontTextEvaluate
));
if(answer.
getNote() !=
null && answer.
getNote().
trim().
length() !=0
)
answerP.
add(new Chunk
(" Obs:"+answer.
getNote(),fontTextEvaluate
));
}
else
{
// double quotationA = (answer.getQuotation()*100)/question.getQuotation();
double quotationA = answer.
getQuotation();
answerP.
add(new Chunk
(" Incompleta("+quotationA+
")",fontTextEvaluate
));
if(answer.
getNote() !=
null && answer.
getNote().
trim().
length() !=0
)
answerP.
add(new Chunk
(" Obs:"+answer.
getNote(),fontTextEvaluate
));
}
}
doc.
add(answerP
);
break;
}
}
}
}
if(question.
getType().
equals(Globals.
ASSESSMENT_QUESTION_TYPE_RADIO) || question.
getType().
equals(Globals.
ASSESSMENT_QUESTION_TYPE_CHECK))
{
for(AssessmentQuestionItem item : question.
getQuestionItems())
{
for(AssessmentQuestionPossibleAnswer pA : item.
getPossibleAnswers())
{
1.5.0/docs/api/java/lang/String.html">String evaluated=
null;
1.5.0/docs/api/java/awt/Image.html">Image imagePa=
null;
if(question.
getType().
equals(Globals.
ASSESSMENT_QUESTION_TYPE_RADIO))
{
boolean isRadioAnswered =
false;
for(AssessmentQuestionAnswer answer : assessmentResponse.
getAnswers())
{
if(answer.
getPossibleAnswer() == pA
)
{
imagePa =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/radioboxselected.png");
isRadioAnswered =
true;
if(assessmentResponse.
isEvaluated())
{ //verifica se est� correcta
if(pA.
getQuotation()>0)
{
evaluated =
" Correcta";
}
else if(pA.
getQuotation()==
0)
{
evaluated =
" Errada";
}
}
break;
}
}
if(!isRadioAnswered
)
imagePa =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/radiobox.png");
}
else
{
boolean isCheckAnswered =
false;
for(AssessmentQuestionAnswer answer : assessmentResponse.
getAnswers())
{
if(answer.
getPossibleAnswer() == pA
)
{
imagePa =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/checkboxselected.jpg");
isCheckAnswered =
true;
if(assessmentResponse.
isEvaluated())
{ //verifica se est� correcta
if(pA.
getQuotation()>0)
{
evaluated =
" Correcta";
}
else if(pA.
getQuotation()==
0)
{
evaluated =
" Errada";
}
}
break;
}
}
if(!isCheckAnswered
)
imagePa =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/checkbox.png");
}
imagePa.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
PARAGRAPH);
Paragraph pAP =
new Paragraph
();
pAP.
add(new Chunk
(imagePa, -1.00f, -2.00f
));
pAP.
add(new Chunk
(" "+pA.
getTextpt(),fontTextNormal
));
if(evaluated
!=
null)
{
pAP.
add(new Chunk
(evaluated,fontTextEvaluate
));
evaluated=
null;
}
pAP.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_JUSTIFIED);
doc.
add(pAP
);
}
}
}
if(question.
getType().
equals(Globals.
ASSESSMENT_QUESTION_TYPE_MATRIX))
{
PdfPTable table =
null;
int numPossibles=
0;
//construir tabela
//para saber o numero de respostas possiveis
for(AssessmentQuestionItem itemTam : question.
getQuestionItems())
{
numPossibles = itemTam.
getPossibleAnswers().
size()+
2; //mais um para items e para correcções
table =
new PdfPTable
(numPossibles
);
PdfPCell colunaVazia =
new PdfPCell
(new Paragraph
());
colunaVazia.
setBorder(0
);
table.
addCell(colunaVazia
);
for(AssessmentQuestionPossibleAnswer possibleTam : itemTam.
getPossibleAnswers())
{
PdfPCell possible =
new PdfPCell
(new Paragraph
(possibleTam.
getTextpt(),fontTextNormal
));
possible.
setBorder(0
);
table.
addCell(possible
);
}
table.
addCell(colunaVazia
); //para fechar a primeira linha da tabela
break;
}
for(AssessmentQuestionItem item : question.
getQuestionItems())
{
1.5.0/docs/api/java/lang/String.html">String evaluated=
null;
PdfPCell item1 =
new PdfPCell
(new Paragraph
(item.
getTextpt(),fontTextNormal
));
item1.
setBorder(0
);
table.
addCell(item1
);
1.5.0/docs/api/java/awt/Image.html">Image imagePa=
null;
for(AssessmentQuestionPossibleAnswer pA : item.
getPossibleAnswers())
{
boolean isRadioAnswered =
false;
for(AssessmentQuestionAnswer answer : assessmentResponse.
getAnswers())
{
if(answer.
getPossibleAnswer() == pA
)
{
imagePa =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/radioboxselected.png");
isRadioAnswered =
true;
if(assessmentResponse.
isEvaluated())
{ //verifica se esta correcta
if(pA.
getQuotation()>0)
{
evaluated =
" Correcta";
}
else if(pA.
getQuotation()==
0)
{
evaluated =
" Errada";
}
}
break;
}
}
if(!isRadioAnswered
)
imagePa=
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/radiobox.png");
PdfPCell imageCell =
new PdfPCell
(imagePa
);
imageCell.
setBorder(0
);
table.
addCell(imageCell
);
}
if(evaluated
!=
null)
{
PdfPCell evaluatedCell =
new PdfPCell
(new Paragraph
(evaluated,fontTextEvaluate
));
evaluatedCell.
setBorder(0
);
table.
addCell(evaluatedCell
);
evaluated=
null;
}
else
{
PdfPCell evaluatedCell =
new PdfPCell
(new Paragraph
());
evaluatedCell.
setBorder(0
);
table.
addCell(evaluatedCell
);
}
}
float splitWidth = 1.0f /
(float)numPossibles
;
float[] tams =
new float[numPossibles
];
for(int i=
0;i
<numPossibles
;i++
)
{
tams
[i
] = splitWidth
;
}
table.
setWidths(tams
);
table.
setWidthPercentage(100.0f
);
doc.
add(table
);
}
doc.
add(Chunk.
NEWLINE);
}
} finally {
if (doc
!=
null) {
//fechamento do documento
doc.
close();
}
if (os
!=
null) {
//fechamento da stream de saida
os.
close();
}
}
//repositoryStream
1.5.0/docs/api/java/lang/String.html">String fileName = Globals.
TMP_DIR+
"Resposta"+assessment.
getTitle()+
1.5.0/docs/api/java/lang/String.html">String.
valueOf(assessmentResponse.
getId())+
".pdf";
1.5.0/docs/api/java/io/File.html">File file =
new 1.5.0/docs/api/java/io/File.html">File(fileName
);
1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.
getExtension(fileName
);
1.5.0/docs/api/java/lang/String.html">String description =
"assessmentResponse.file "+assessment.
getCourseUnit().
getName()+assessmentResponse.
getOwner().
getName();
1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream inputStream =
new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(file
);
1.5.0/docs/api/java/lang/String.html">String identifierResponse = assessmentResponse.
getIdentifier();
if(identifierResponse==
null)
identifierResponse = repositoryService.
storeRepositoryFile(inputStream,
"application/pdf",extension,
(int)file.
length(),file.
getName(),description, ResourceAccessControlEnum.
publicDomain,userSession
);
else
repositoryService.
updateRepositoryFile(identifierResponse,inputStream,
"application/pdf",extension,
(int)file.
length(),file.
getName(),description, ResourceAccessControlEnum.
publicDomain);
assessmentResponse.
setIdentifier(identifierResponse
);
}
public AssessmentView generateEvaluationsPdf
(long assessmentId,UserSession userSession
) throws 1.5.0/docs/api/java/io/IOException.html">IOException, DocumentException
{
AssessmentImpl assessment = DaoFactory.
getAssessmentDaoImpl().
get(assessmentId
);
List
<AssessmentResponse
> responses = DaoFactory.
getAssessmentResponseDaoImpl().
loadResponsesByAssessmentId(assessmentId
);
////////////////////////////////////////////////
5+0%2Fdocs%2Fapi+Document">Document doc =
null;
5+0%2Fdocs%2Fapi+OutputStream">OutputStream os =
null;
try {
//cria o documento tamanho A4, margens de 2,54cm
doc =
new 5+0%2Fdocs%2Fapi+Document">Document(PageSize.
A4, 72, 72, 72, 72
);
//cria a stream de saida
os =
new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(Globals.
TMP_DIR+ assessment.
getTitle()+
".pdf");
//associa a stream de saida ao
PdfWriter.
getInstance(doc, os
);
//abre o documento
doc.
open();
//imagem do cabecalho
1.5.0/docs/api/java/awt/Image.html">Image img =
1.5.0/docs/api/java/awt/Image.html">Image.
getInstance(Globals.
SITE_URL+
"/imgs/logoestg.gif");
img.
setAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_LEFT);
doc.
add(img
);
//Barra horizontal
LineSeparator objectName =
new LineSeparator
();
doc.
add(objectName
);
//adiciona um paragrafo para dar espaco
// Paragraph p = new Paragraph("");
// Paragraph paragrafo = new Paragraph();
// paragrafo.setSpacingBefore(10);
// paragrafo.setIndentationLeft(58);
// paragrafo.setSpacingBefore(20);
// paragrafo.setLeading(20);
// paragrafo.add(new Chunk());
// doc.add(paragrafo);
//doc.add(p);
//doc.add(Chunk.NEWLINE);
//Informacoes sobre a prova
Phrase anoCurso =
new Phrase
("\nAno Lectivo: "+
1.5.0/docs/api/java/lang/String.html">String.
valueOf(assessment.
getYear())+
" / Curso: "+assessment.
getCourseUnit().
getCourseName()+
"\n");
Phrase courseUnit =
new Phrase
("Unidade Curricular: "+assessment.
getCourseUnit().
getName()+
"\nDocente: "+assessment.
getOwner().
getName()+
"\n");
doc.
add(anoCurso
);
doc.
add(courseUnit
);
doc.
add(new Phrase
(" "));
////tabela
PdfPTable table =
new PdfPTable
(assessment.
getQuestions().
size()+3
);
1.5.0/docs/api/java/awt/Font.html">Font fontHeader =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,10,
1.5.0/docs/api/java/awt/Font.html">Font.
BOLD);
//primeira linha para as cotacoes
PdfPCell colunaVazia =
new PdfPCell
(new Paragraph
());
colunaVazia.
setBackgroundColor(new BaseColor
(0Xc0, 0Xc0,0Xc0
));
colunaVazia.
setColspan(2
);
PdfPCell cotacaoMaxima =
new PdfPCell
(new Paragraph
("20",fontHeader
));
cotacaoMaxima.
setBackgroundColor(new BaseColor
(0Xc0, 0Xc0,0Xc0
));
table.
addCell(colunaVazia
);
//table.addCell(colunaVazia);
table.
addCell(cotacaoMaxima
);
for(AssessmentQuestion question : assessment.
getQuestions())
{
PdfPCell coluna =
new PdfPCell
(new Paragraph
(1.5.0/docs/api/java/lang/String.html">String.
valueOf(question.
getQuotation()),fontHeader
));
coluna.
setBackgroundColor(new BaseColor
(0Xc0, 0Xc0,0Xc0
));
table.
addCell(coluna
);
}
//segunda linha
PdfPCell col1 =
new PdfPCell
(new Paragraph
("N�",fontHeader
));
col1.
setBackgroundColor(new BaseColor
(0Xc0, 0Xc0,0Xc0
));
PdfPCell col2 =
new PdfPCell
(new Paragraph
("Nome",fontHeader
));
col2.
setBackgroundColor(new BaseColor
(0Xc0, 0Xc0,0Xc0
));
PdfPCell col3 =
new PdfPCell
(new Paragraph
("Nota",fontHeader
));
col3.
setBackgroundColor(new BaseColor
(0Xc0, 0Xc0,0Xc0
));
table.
addCell(col1
);
table.
addCell(col2
);
table.
addCell(col3
);
int numberQ=
0;
for(AssessmentQuestion question : assessment.
getQuestions())
{
numberQ++
;
PdfPCell coluna =
new PdfPCell
(new Paragraph
("P"+
1.5.0/docs/api/java/lang/String.html">String.
valueOf(numberQ
),fontHeader
));
coluna.
setBackgroundColor(new BaseColor
(0Xc0, 0Xc0,0Xc0
));
table.
addCell(coluna
);
}
1.5.0/docs/api/java/awt/Font.html">Font fontText =
new 1.5.0/docs/api/java/awt/Font.html">Font(1.5.0/docs/api/java/awt/Font.html">Font.
FontFamily.
TIMES_ROMAN,10,
1.5.0/docs/api/java/awt/Font.html">Font.
NORMAL);
//cotacoes das respostas
for(AssessmentResponse response : responses
)
{
1.5.0/docs/api/java/lang/String.html">String code=
"";
if(response.
getOwner() instanceof Student
)
{
code =
1.5.0/docs/api/java/lang/String.html">String.
valueOf(((Student
)response.
getOwner()).
getSigesCode());
}
PdfPCell colunaNumero =
new PdfPCell
(new Paragraph
(code,fontText
));
PdfPCell colunaNome =
new PdfPCell
(new Paragraph
(response.
getOwner().
getName(),fontText
));
PdfPCell colunaNota =
new PdfPCell
(new Paragraph
(1.5.0/docs/api/java/lang/String.html">String.
valueOf(response.
getQuotation()),fontText
));
table.
addCell(colunaNumero
);
table.
addCell(colunaNome
);
table.
addCell(colunaNota
);
//numero nome e nota adicionados
//percorrer as questoes
for(AssessmentQuestion question : assessment.
getQuestions())
{
1.5.0/docs/api/java/lang/String.html">String semResposta=
" ";
double percentagemF=
0;
boolean respondida =
false;
for(AssessmentQuestionAnswer answer : response.
getAnswers())
{
if(answer.
getPossibleAnswer().
getQuestionItem().
getQuestion()==question
)
{
// percentagemF += (answer.getQuotation()*100) / question.getQuotation();
percentagemF += answer.
getQuotation();
respondida=
true;
}
}
if(respondida
){
// int percentagemI = (int)percentagemF;
// PdfPCell colunaPercentagem = new PdfPCell(new Paragraph(String.valueOf(percentagemI)+"%",fontText));
PdfPCell colunaPercentagem =
new PdfPCell
(new Paragraph
(1.5.0/docs/api/java/lang/String.html">String.
valueOf(percentagemF
),fontText
));
table.
addCell(colunaPercentagem
);
}
else{
PdfPCell colunaPercentagem =
new PdfPCell
(new Paragraph
(semResposta,fontText
));
table.
addCell(colunaPercentagem
);
}
}
}
float splitWidth = 0.65f /
(float)(assessment.
getQuestions().
size()+1
);
float[] tams =
new float[assessment.
getQuestions().
size()+3
];
tams
[0
]=0.10f
;
tams
[1
]=0.25f
;
for(int i=
2;i
<assessment.
getQuestions().
size()+
3;i++
)
{
tams
[i
] = splitWidth
;
}
// float[] tams = {0.10f,0.25f,0.10f,0.10f,0.10f,0.03f,0.04f,0.04f};
table.
setWidths(tams
);
table.
setWidthPercentage(100.0f
);
table.
setHorizontalAlignment(5+0%2Fdocs%2Fapi+Element">Element.
ALIGN_LEFT);
doc.
add(table
);
} finally {
if (doc
!=
null) {
//fechamento do documento
doc.
close();
}
if (os
!=
null) {
//fechamento da stream de saida
os.
close();
}
}
//repositoryStream
1.5.0/docs/api/java/lang/String.html">String fileName = Globals.
TMP_DIR+ assessment.
getTitle()+
".pdf";
1.5.0/docs/api/java/io/File.html">File file =
new 1.5.0/docs/api/java/io/File.html">File(fileName
);
1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.
getExtension(fileName
);
1.5.0/docs/api/java/lang/String.html">String description =
"assessment.evaluations.file "+assessment.
getCourseUnit().
getName();
1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream inputStream =
new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(file
);
1.5.0/docs/api/java/lang/String.html">String identifierEvaluations = assessment.
getEvaluationsIdentifier();
if(identifierEvaluations==
null)
identifierEvaluations = repositoryService.
storeRepositoryFile(inputStream,
"application/pdf",extension,
(int)file.
length(),file.
getName(),description, ResourceAccessControlEnum.
publicDomain,userSession
);
else
repositoryService.
updateRepositoryFile(identifierEvaluations,inputStream,
"application/pdf",extension,
(int)file.
length(),file.
getName(),description, ResourceAccessControlEnum.
publicDomain);
assessment.
setEvaluationsIdentifier(identifierEvaluations
);
return new AssessmentView
(assessment
);
// return new AssessmentView();
}
}