/*
Copyright 2012 Christian Prause and Fraunhofer FIT
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package net.sf.collabreview.mediawiki;
import net.sf.collabreview.core.Artifact;
import net.sf.collabreview.core.CollabReview;
import net.sf.collabreview.core.Repository;
import net.sf.collabreview.core.configuration.ConfigurationData;
import net.sf.collabreview.core.users.Author;
import net.sf.collabreview.hooks.ConfigurableHook;
import net.sf.collabreview.hooks.ReviewRejectedHook;
import net.sf.collabreview.hooks.SetReviewHook;
import net.sf.collabreview.repository.Review;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
* Send a mail to the person whose review was rejected.
*
* @author Christian Prause (chris)
* @date 2011-01-21 09:30:03
*/
public class SendMoknowpediaEventMails implements ConfigurableHook {
/**
* Apache commons logging logger for class SendMoknowpediaEventMails.
*/
private static final Log logger = LogFactory.getLog(SendMoknowpediaEventMails.class);
@Override
public int getPriority() {
return PRIORITY_VERY_LOW;
}
@Override
public void configure(final CollabReview collabReview, ConfigurationData configurationData) {
logger.info("Register SendMoknowpediaEventMails hooks");
collabReview.getRepository().registerSetReviewHook(new SetReviewHook() {
@Override
public void reviewUpdated(Repository repository, Review oldReview, Review newReview) {
if (oldReview != null && oldReview.getRejectedBy() == null && newReview.getRejectedBy() != null) {
}
int rating = newReview.getRating();
String comment = newReview.getReviewText();
boolean anonymous = newReview.getIsAnonymous();
Author author = newReview.getAuthor();
Artifact artifact = collabReview.getRepository().getArtifact(newReview.getArtifactIdentifier());
String articleName = newReview.getArtifactIdentifier().getName();
int mailCount = 3;
String ratingName = "";
switch (rating) {
case 0:
ratingName = "mangelhaft";
break;
case 2:
ratingName = "ausreichend";
break;
case 4:
ratingName = "befriedigend";
break;
case 7:
ratingName = "gut";
break;
case 10:
ratingName = "sehr gut";
break;
}
if (comment.equals("Bitte geben Sie Ihren Kommentar hier ein!") || comment.equals("")) {
comment = "- kein -";
}
Map<Author, Float> responsibilities = collabReview.getMeasurementsManager().getArtifactResponsibility().listResponsibilities(artifact);
for (Author aa : responsibilities.keySet()) {
if (mailCount == 0 || responsibilities.get(aa) < 0.1) {
break;
}
mailCount--;
logger.debug("Mail an " + aa + " email: " + aa.getEmail());
String linkArticle = null;
try {
linkArticle = "http://moknowpedia.fit.fraunhofer.de/mediawiki/index.php5/" + java.net.URLEncoder.encode(articleName, "utf-8");
} catch (UnsupportedEncodingException e) {
logger.error("Encoding error when trying to send Moknowpedia Mail", e);
linkArticle = "[ENCODING ERROR]";
}
String reviewerName = anonymous ? "anonym" : ("von " + author.getName());
collabReview.getMailManager().newMail("Ein Artikel von dir wurde bewertet! :-)", aa.getEmail())
.setBody("Hallo " + aa.getName() + ",\n\n dein Artikel \"" + articleName + "\" wurde gerade " + reviewerName + " mit _" + ratingName + "_ bewertet!" +
"\n Optionaler Kommentar der Bewertung: " + comment + "." +
"\n\nBester Gruß,\n Euer MoKnowPedianer" +
"\n\nPS: Diese URL führt dich eventuell direkt zum Artikel: " + linkArticle)
.send();
}
}
@Override
public int getPriority() {
return PRIORITY_MEDIUM;
}
});
collabReview.getRepository().registerReviewRejectedHook(new ReviewRejectedHook() {
@Override
public void reviewRejected(Review review) {
review.getRepository().getCollabReview().getMailManager().newMail("Artikelbewertung abgelehnt", review.getAuthor().getEmail()).setBody(String.format(
"Hallo %s,\n\n deine Bewertung für den Artikel %s wurde gerade von %s zurückgewiesen. Begründung:\n\n %s\n",
review.getAuthorName(),
review.getArtifactIdentifier().getName(),
review.getRejectedByName(),
review.getRejectionReason()))
.send();
}
@Override
public int getPriority() {
return PRIORITY_VERY_LOW;
}
});
}
}