Updater.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
tvraterplugin |
![]() |
![]() |
TV-Browser |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* TV-Browser Copyright (C) 04-2003 Martin Oberhauser (martin_oat@yahoo.de)
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package tvraterplugin;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import util.exc.ErrorHandler;
import util.io.IOUtilities;
import util.io.NetworkUtilities;
import util.io.XMLWriter;
import util.ui.Localizer;
import util.ui.progress.Progress;
import devplugin.Channel;
import devplugin.Date;
import devplugin.Plugin;
import devplugin.Program;
/**
* This class contains the communication with the server
*
* @author bodo tasche
*/
public class Updater implements Progress {
/** Localizer */
private static final Localizer mLocalizer = Localizer.getLocalizerFor(Updater.class);
/** Location of Update-Skript */
// private static String LOCATION =
// "http://localhost/~bodum/wannawork3/tvaddicted/updater.php";
private static String LOCATION = "http://tvaddicted.de/updater.php";
/** The Plugin */
private TVRaterPlugin _tvraterPlugin;
/** Update Successfull ? */
private boolean _wasSuccessfull = false;
private Hashtable<String, Program> _updateList;
/**
* Creates the Updater
*
* @param tvraterPlugin Plugin that uses the Updater
*/
public Updater(TVRaterPlugin tvraterPlugin) {
_tvraterPlugin = tvraterPlugin;
}
/**
* Does the Update
*/
public void run() {
Properties settings = _tvraterPlugin.getSettings();
String name = settings.getProperty("name");
String password = settings.getProperty("password");
if ((name == null)
|| (name.length() == 0)
|| (password == null)
|| (password.length() == 0)) {
JOptionPane.showMessageDialog(_tvraterPlugin.getParentFrameForTVRater(), mLocalizer.msg("noUser",
"Please Enter your Userdata in the\nconfiguration of this Plugin"), mLocalizer.msg("error",
"Error while updating TV Rater"), JOptionPane.ERROR_MESSAGE);
return;
}
try {
if (!NetworkUtilities.checkConnection(new URL("http://www.tvaddicted.de"))) {
JOptionPane.showMessageDialog(null,
mLocalizer.msg("noConnectionMessage", "No Connection!"),
mLocalizer.msg("noConnectionTitle", "No Connection!"),
JOptionPane.ERROR_MESSAGE);
return;
}
_updateList = createUpdateList();
if (_updateList.size() == 0) {
_wasSuccessfull = true;
return;
}
URL url = new URL(LOCATION);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
OutputStream out = connection.getOutputStream();
GZIPOutputStream outZipped = new GZIPOutputStream(out);
writeData(outZipped);
outZipped.close();
Node data = readURLConnection(connection);
if (data.getNodeName().equals("error")) {
String message = getTextFromNode(data);
JOptionPane.showMessageDialog(_tvraterPlugin.getParentFrameForTVRater(), mLocalizer.msg("serverError",
"The Server has send the following error:")
+ "\n" + message, mLocalizer.msg("error", "Error while updating TV Rater"), JOptionPane.ERROR_MESSAGE);
} else {
readData(data);
_wasSuccessfull = true;
_tvraterPlugin.updateCurrentDate();
}
out.close();
} catch (Exception e) {
ErrorHandler.handle(mLocalizer.msg("updateError", "An error occured while updating the TVRater Database"), e);
e.printStackTrace();
}
}
/**
* Was the update successfull?
*
* @return Successfully updated ?
*/
public boolean wasSuccessfull() {
return _wasSuccessfull;
}
/**
* Gets the Text within a Node
*
* @param data Node to rip the Text from
* @return Text in the Node
*/
private String getTextFromNode(Node data) {
Node child = data.getFirstChild();
StringBuffer text = new StringBuffer();
while (child != null) {
if (child.getNodeType() == Node.TEXT_NODE) {
text.append(child.getNodeValue());
}
child = child.getNextSibling();
}
return text.toString();
}
/**
* Reads the String returned by the PHP-Skript and parses the DOM
*
*/
private void readData(Node node) {
Node child = node.getFirstChild();
while (child != null) {
if (child.getNodeName().equals("data")) {
readRatingData(child);
}
child = child.getNextSibling();
}
}
/**
* Reads the Data in this Node
*
* @param node Node to analyse
*/
private void readRatingData(Node node) {
_tvraterPlugin.getDatabase().clearServer();
Node child = node.getFirstChild();
while (child != null) {
if (child.getNodeName().equals("rating")) {
readRating(child);
}
child = child.getNextSibling();
}
}
/**
* Reads a single Rating
*
* @param node Rating as DOM-Node
*/
private void readRating(Node node) {
Rating rating = new Rating();
Node child = node.getFirstChild();
while (child != null) {
String nodename = child.getNodeName();
if (nodename.equals("title")) {
rating.setTitle(getNodeValue(child));
} else if (nodename.equals("overall")) {
int overall = new Double(Double.parseDouble(getNodeValue(child))).intValue();
rating.setOverallRating(overall);
} else if (nodename.equals("action")) {
int action = new Double(Double.parseDouble(getNodeValue(child))).intValue();
rating.setActionRating(action);
} else if (nodename.equals("entitlement")) {
int entitlement = new Double(Double.parseDouble(getNodeValue(child))).intValue();
rating.setEntitlementRating(entitlement);
} else if (nodename.equals("fun")) {
int fun = new Double(Double.parseDouble(getNodeValue(child))).intValue();
rating.setFunRating(fun);
} else if (nodename.equals("tension")) {
int tension = new Double(Double.parseDouble(getNodeValue(child))).intValue();
rating.setTensionRating(tension);
} else if (nodename.equals("erotic")) {
int erotic = new Double(Double.parseDouble(getNodeValue(child))).intValue();
rating.setEroticRating(erotic);
} else if (nodename.equals("count")) {
int userCount = Integer.parseInt(getNodeValue(child));
rating.setUserCount(userCount);
} else if (nodename.equals("genre")) {
int genre = Integer.parseInt(getNodeValue(child));
rating.setGenre(genre);
} else if (nodename.equals("id")) {
int onlineID = Integer.parseInt(getNodeValue(child));
rating.setOnlineID(onlineID);
if (rating.getTitle() != null) {
Rating personal = _tvraterPlugin.getDatabase().getPersonalRating(rating.getTitle());
if (personal != null) {
personal.setOnlineID(onlineID);
}
} else {
System.out.println("No Title");
}
}
child = child.getNextSibling();
}
_tvraterPlugin.getDatabase().setServerRating(rating);
}
/**
* Returns the Text-Value in this Node
*
* @param node get Text from this Node
* @return Text in this Node
*/
private String getNodeValue(Node node) {
StringBuffer value = new StringBuffer();
Node child = node.getFirstChild();
while (child != null) {
if (child.getNodeType() == Node.TEXT_NODE) {
value.append(child.getNodeValue());
}
child = child.getNextSibling();
}
return value.toString();
}
/**
* Writes the Data into the Outputstream
*
* @param output the Outputstream
* @throws ParserConfigurationException
* @throws IOException
*/
private void writeData(OutputStream output) throws ParserConfigurationException, IOException {
Document document;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
Element tvrater = document.createElement("tvrater");
document.appendChild(tvrater);
// User
Element user = document.createElement("user");
Element name = createNodeWithTextValue(document, "name", _tvraterPlugin.getSettings().getProperty("name"));
user.appendChild(name);
Element password = createNodeWithTextValue(document, "password", IOUtilities.xorEncode(_tvraterPlugin.getSettings()
.getProperty("password"), 21));
user.appendChild(password);
tvrater.appendChild(user);
// Command
Element command = createNodeWithTextValue(document, "command", "Update");
tvrater.appendChild(command);
// Data
Element data = document.createElement("data");
tvrater.appendChild(data);
// Setratings
Element setratings = document.createElement("setratings");
data.appendChild(setratings);
ArrayList<Rating> list = _tvraterPlugin.getDatabase().getChangedPersonal();
for (int i = 0; i < list.size(); i++) {
Element ratingElement = document.createElement("rating");
setratings.appendChild(ratingElement);
Rating rating = list.get(i);
ratingElement.appendChild(createNodeWithTextValue(document, "title", rating.getTitle()));
ratingElement.appendChild(createNodeWithTextValue(document, "overall", rating.getOverallRating()));
ratingElement.appendChild(createNodeWithTextValue(document, "action", rating.getActionRating()));
ratingElement
.appendChild(createNodeWithTextValue(document, "entitlement", rating.getEntitlementRating()));
ratingElement.appendChild(createNodeWithTextValue(document, "fun", rating.getFunRating()));
ratingElement.appendChild(createNodeWithTextValue(document, "tension", rating.getTensionRating()));
ratingElement.appendChild(createNodeWithTextValue(document, "erotic", rating.getEroticRating()));
ratingElement.appendChild(createNodeWithTextValue(document, "genre", rating.getGenre()));
}
_tvraterPlugin.getDatabase().clearChangedPersonal();
// GetRatings
Element getratings = document.createElement("getratings");
data.appendChild(getratings);
Enumeration<Program> en = _updateList.elements();
while (en.hasMoreElements()) {
Element program = document.createElement("program");
getratings.appendChild(program);
Program prog = en.nextElement();
program.appendChild(createNodeWithTextValue(document, "title", prog.getTitle()));
}
XMLWriter writer = new XMLWriter();
writer.writeDocumentToOutputStream(document, output, "UTF-8");
}
/**
* Creates a Node with a filled TextNode
*
* @param doc Create Node with this Document
* @param nodename Name of the Node to create
* @param value Text-Value in this Node
* @return Node with a filled TextNode
*/
private Element createNodeWithTextValue(Document doc, String nodename, String value) {
Element el = doc.createElement(nodename);
el.appendChild(doc.createTextNode(value));
return el;
}
/**
* Creates a Node with a filled TextNode
*
* @param doc Create Node with this Document
* @param nodename Name of the Node to create
* @param value Text-Value in this Node
* @return Node with a filled TextNode
*/
private Element createNodeWithTextValue(Document doc, String nodename, int value) {
return createNodeWithTextValue(doc, nodename, Integer.toString(value));
}
/**
* Reads the Data in a URLConnection
*
* @param uc Connection
* @return Data returned from the URL
* @throws Exception IOException etc...
*/
private static Node readURLConnection(URLConnection uc) throws Exception {
Document document;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// System.out.println(new BufferedInputStream(uc.getInputStream()).);
/*
* DataInputStream dis = new DataInputStream (uc.getInputStream()); String
* line; try { do { line = dis.readLine(); System.out.println(line);
* }while (line != null); } catch (IOException e) { line = "0";}
*/
document = builder.parse(new GZIPInputStream(uc.getInputStream()));
} catch (Exception e) {
throw e;
}
return document.getDocumentElement();
}
/**
* Runs thru all Days and Channels, creates a List of Programs that need to
* get a rating
*
* @return Hashtable filled with Programs to rate
*/
private Hashtable<String, Program> createUpdateList() {
Hashtable<String, Program> table = new Hashtable<String, Program>();
Channel[] channels = Plugin.getPluginManager().getSubscribedChannels();
Date date = new Date();
date = date.addDays(-1);
for (int d = 0; d < 32; d++) {
for (int i = 0; i < channels.length; i++) {
Iterator<Program> it = Plugin.getPluginManager().getChannelDayProgram(date, channels[i]);
while ((it != null) && (it.hasNext())) {
Program program = it.next();
if ((program != null) && _tvraterPlugin.isProgramRateable(program)) {
if (!table.containsKey(program.getTitle())) {
table.put(program.getTitle(), program);
}
}
}
}
date = date.addDays(1);
}
return table;
}
}
The table below shows all metrics for Updater.java.




