    public static String encodePassword(String password) {
        try {
            MessageDigest messageDiegest = MessageDigest.getInstance("SHA-1");
            messageDiegest.update(password.getBytes("UTF-8"));
            return Base64.encodeToString(messageDiegest.digest(), false);
        } catch (NoSuchAlgorithmException e) {
            log.error("Exception while encoding password");
            throw new Error(e);
        } catch (UnsupportedEncodingException e) {
            log.error("Exception while encoding password");
            throw new Error(e);
        }
    }


    public static String[] findClassesInPackage(String packageName, List<String> included, List<String> excluded) throws IOException {
        String packageOnly = packageName;
        boolean recursive = false;
        if (packageName.endsWith(".*")) {
            packageOnly = packageName.substring(0, packageName.lastIndexOf(".*"));
            recursive = true;
        }
        List<String> vResult = new ArrayList<String>();
        String packageDirName = packageOnly.replace('.', '/');
        Enumeration<URL> dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
        while (dirs.hasMoreElements()) {
            URL url = dirs.nextElement();
            String protocol = url.getProtocol();
            if (!matchTestClasspath(url, packageDirName, recursive)) {
                continue;
            }
            if ("file".equals(protocol)) {
                findClassesInDirPackage(packageOnly, included, excluded, URLDecoder.decode(url.getFile(), "UTF-8"), recursive, vResult);
            } else if ("jar".equals(protocol)) {
                JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
                Enumeration<JarEntry> entries = jar.entries();
                while (entries.hasMoreElements()) {
                    JarEntry entry = entries.nextElement();
                    String name = entry.getName();
                    if (name.charAt(0) == '/') {
                        name = name.substring(1);
                    }
                    if (name.startsWith(packageDirName)) {
                        int idx = name.lastIndexOf('/');
                        if (idx != -1) {
                            packageName = name.substring(0, idx).replace('/', '.');
                        }
                        Utils.log("PackageUtils", 4, "Package name is " + packageName);
                        if ((idx != -1) || recursive) {
                            if (name.endsWith(".class") && !entry.isDirectory()) {
                                String className = name.substring(packageName.length() + 1, name.length() - 6);
                                Utils.log("PackageUtils", 4, "Found class " + className + ", seeing it if it's included or excluded");
                                includeOrExcludeClass(packageName, className, included, excluded, vResult);
                            }
                        }
                    }
                }
            }
        }
        String[] result = vResult.toArray(new String[vResult.size()]);
        return result;
    }


    public void execute() throws MojoExecutionException, MojoFailureException {
        try {
            this.getLog().info("copy source web.xml - " + this.getWebXml() + " to build dir (source web.xml required if mergewebxml execution is enabled)");
            File destination = new File(this.getBuildDir(), "web.xml");
            if (!destination.exists()) {
                destination.getParentFile().mkdirs();
                destination.createNewFile();
            }
            FileIOUtils.copyFile(this.getWebXml(), destination);
            for (int i = 0; i < this.getCompileTarget().length; i++) {
                File moduleFile = null;
                for (Iterator it = this.getProject().getCompileSourceRoots().iterator(); it.hasNext() && moduleFile == null; ) {
                    File check = new File(it.next().toString() + "/" + this.getCompileTarget()[i].replace('.', '/') + ".gwt.xml");
                    getLog().debug("Looking for file: " + check.getAbsolutePath());
                    if (check.exists()) {
                        moduleFile = check;
                    }
                }
                for (Iterator it = this.getProject().getResources().iterator(); it.hasNext(); ) {
                    Resource r = (Resource) it.next();
                    File check = new File(r.getDirectory() + "/" + this.getCompileTarget()[i].replace('.', '/') + ".gwt.xml");
                    getLog().debug("Looking for file: " + check.getAbsolutePath());
                    if (check.exists()) {
                        moduleFile = check;
                    }
                }
                ClassLoader loader = this.fixThreadClasspath();
                if (moduleFile == null) {
                    try {
                        String classpath = "/" + this.getCompileTarget()[i].replace('.', '/') + ".gwt.xml";
                        InputStream is = loader.getResourceAsStream(classpath);
                        System.out.println("Looking for classpath: " + classpath + "(" + (is != null) + ")");
                        if (is != null) {
                            File temp = new File(this.getBuildDir(), this.getCompileTarget()[i].concat(".gwt.xml"));
                            FileOutputStream fos = new FileOutputStream(temp);
                            FileIOUtils.copyStream(is, fos);
                            moduleFile = temp;
                        }
                    } catch (IOException e) {
                        this.getLog().info(e);
                    }
                }
                GwtWebInfProcessor processor = null;
                try {
                    if (moduleFile != null) {
                        getLog().info("Module file: " + moduleFile.getAbsolutePath());
                        processor = new GwtWebInfProcessor(this.getCompileTarget()[i], moduleFile, destination.getAbsolutePath(), destination.getAbsolutePath(), this.isWebXmlServletPathAsIs());
                        processor.process();
                    } else {
                        throw new MojoExecutionException("module file null");
                    }
                } catch (ExitException e) {
                    this.getLog().info(e.getMessage());
                }
            }
        } catch (Exception e) {
            throw new MojoExecutionException("Unable to merge web.xml", e);
        }
    }


    private static void readAndRewrite(File inFile, File outFile) throws IOException {
        ImageInputStream iis = ImageIO.createImageInputStream(new BufferedInputStream(new FileInputStream(inFile)));
        DcmParser dcmParser = DcmParserFactory.getInstance().newDcmParser(iis);
        Dataset ds = DcmObjectFactory.getInstance().newDataset();
        dcmParser.setDcmHandler(ds.getDcmHandler());
        dcmParser.parseDcmFile(null, Tags.PixelData);
        PixelDataReader pdReader = pdFact.newReader(ds, iis, dcmParser.getDcmDecodeParam().byteOrder, dcmParser.getReadVR());
        System.out.println("reading " + inFile + "...");
        pdReader.readPixelData(false);
        ImageOutputStream out = ImageIO.createImageOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
        DcmEncodeParam dcmEncParam = DcmEncodeParam.IVR_LE;
        ds.writeDataset(out, dcmEncParam);
        ds.writeHeader(out, dcmEncParam, Tags.PixelData, dcmParser.getReadVR(), dcmParser.getReadLength());
        System.out.println("writing " + outFile + "...");
        PixelDataWriter pdWriter = pdFact.newWriter(pdReader.getPixelDataArray(), false, ds, out, dcmParser.getDcmDecodeParam().byteOrder, dcmParser.getReadVR());
        pdWriter.writePixelData();
        out.flush();
        out.close();
        System.out.println("done!");
    }


    public void convert(File src, File dest) throws IOException {
        InputStream in = new BufferedInputStream(new FileInputStream(src));
        DcmParser p = pfact.newDcmParser(in);
        Dataset ds = fact.newDataset();
        p.setDcmHandler(ds.getDcmHandler());
        try {
            FileFormat format = p.detectFileFormat();
            if (format != FileFormat.ACRNEMA_STREAM) {
                System.out.println("\n" + src + ": not an ACRNEMA stream!");
                return;
            }
            p.parseDcmFile(format, Tags.PixelData);
            if (ds.contains(Tags.StudyInstanceUID) || ds.contains(Tags.SeriesInstanceUID) || ds.contains(Tags.SOPInstanceUID) || ds.contains(Tags.SOPClassUID)) {
                System.out.println("\n" + src + ": contains UIDs!" + " => probable already DICOM - do not convert");
                return;
            }
            boolean hasPixelData = p.getReadTag() == Tags.PixelData;
            boolean inflate = hasPixelData && ds.getInt(Tags.BitsAllocated, 0) == 12;
            int pxlen = p.getReadLength();
            if (hasPixelData) {
                if (inflate) {
                    ds.putUS(Tags.BitsAllocated, 16);
                    pxlen = pxlen * 4 / 3;
                }
                if (pxlen != (ds.getInt(Tags.BitsAllocated, 0) >>> 3) * ds.getInt(Tags.Rows, 0) * ds.getInt(Tags.Columns, 0) * ds.getInt(Tags.NumberOfFrames, 1) * ds.getInt(Tags.NumberOfSamples, 1)) {
                    System.out.println("\n" + src + ": mismatch pixel data length!" + " => do not convert");
                    return;
                }
            }
            ds.putUI(Tags.StudyInstanceUID, uid(studyUID));
            ds.putUI(Tags.SeriesInstanceUID, uid(seriesUID));
            ds.putUI(Tags.SOPInstanceUID, uid(instUID));
            ds.putUI(Tags.SOPClassUID, classUID);
            if (!ds.contains(Tags.NumberOfSamples)) {
                ds.putUS(Tags.NumberOfSamples, 1);
            }
            if (!ds.contains(Tags.PhotometricInterpretation)) {
                ds.putCS(Tags.PhotometricInterpretation, "MONOCHROME2");
            }
            if (fmi) {
                ds.setFileMetaInfo(fact.newFileMetaInfo(ds, UIDs.ImplicitVRLittleEndian));
            }
            OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
            try {
            } finally {
                ds.writeFile(out, encodeParam());
                if (hasPixelData) {
                    if (!skipGroupLen) {
                        out.write(PXDATA_GROUPLEN);
                        int grlen = pxlen + 8;
                        out.write((byte) grlen);
                        out.write((byte) (grlen >> 8));
                        out.write((byte) (grlen >> 16));
                        out.write((byte) (grlen >> 24));
                    }
                    out.write(PXDATA_TAG);
                    out.write((byte) pxlen);
                    out.write((byte) (pxlen >> 8));
                    out.write((byte) (pxlen >> 16));
                    out.write((byte) (pxlen >> 24));
                }
                if (inflate) {
                    int b2, b3;
                    for (; pxlen > 0; pxlen -= 3) {
                        out.write(in.read());
                        b2 = in.read();
                        b3 = in.read();
                        out.write(b2 & 0x0f);
                        out.write(b2 >> 4 | ((b3 & 0x0f) << 4));
                        out.write(b3 >> 4);
                    }
                } else {
                    for (; pxlen > 0; --pxlen) {
                        out.write(in.read());
                    }
                }
                out.close();
            }
            System.out.print('.');
        } finally {
            in.close();
        }
    }


    @Override
    protected Void doInBackground(String... urls) {
        Log.d("ParseTask", "Getting URL " + urls[0]);
        try {
            XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
            reader.setContentHandler(mParser);
            reader.parse(new InputSource(new URL(urls[0]).openStream()));
        } catch (Exception e) {
            if (mCallback != null) mCallback.OnFailure(new ApiResponseObject(ApiResponse.RESPONSE_CRITICAL_FAILURE, e.getLocalizedMessage()));
        }
        return null;
    }


        public final int sendMetaData(FileInputStream fis) throws Exception {
            try {
                UUID uuid = UUID.randomUUID();
                HttpClient client = new SSLHttpClient();
                StringBuilder builder = new StringBuilder(mServer).append("?cmd=meta").append("&id=" + uuid);
                HttpPost method = new HttpPost(builder.toString());
                String fileName = uuid + ".metadata";
                FileInputStreamPart part = new FileInputStreamPart("data", fileName, fis);
                MultipartEntity requestContent = new MultipartEntity(new Part[] { part });
                method.setEntity(requestContent);
                HttpResponse response = client.execute(method);
                int code = response.getStatusLine().getStatusCode();
                if (code == HttpStatus.SC_OK) {
                    return 0;
                } else {
                    return -1;
                }
            } catch (Exception e) {
                throw new Exception("send meta data", e);
            }
        }


    public static XMLShowInfo NzbSearch(TVRageShowInfo tvrage, XMLShowInfo xmldata, int latestOrNext) {
        String newzbin_query = "", csvData = "", hellaQueueDir = "", newzbinUsr = "", newzbinPass = "";
        String[] tmp;
        DateFormat tvRageDateFormat = new SimpleDateFormat("MMM/dd/yyyy");
        DateFormat tvRageDateFormatFix = new SimpleDateFormat("yyyy-MM-dd");
        newzbin_query = "?q=" + xmldata.showName + "+";
        if (latestOrNext == 0) {
            if (xmldata.searchBy.equals("ShowName Season x Episode")) newzbin_query += tvrage.latestSeasonNum + "x" + tvrage.latestEpisodeNum; else if (xmldata.searchBy.equals("Showname SeriesNum")) newzbin_query += tvrage.latestSeriesNum; else if (xmldata.searchBy.equals("Showname YYYY-MM-DD")) {
                try {
                    Date airTime = tvRageDateFormat.parse(tvrage.latestAirDate);
                    newzbin_query += tvRageDateFormatFix.format(airTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else if (xmldata.searchBy.equals("Showname EpisodeTitle")) newzbin_query += tvrage.latestTitle;
        } else {
            if (xmldata.searchBy.equals("ShowName Season x Episode")) newzbin_query += tvrage.nextSeasonNum + "x" + tvrage.nextEpisodeNum; else if (xmldata.searchBy.equals("Showname SeriesNum")) newzbin_query += tvrage.nextSeriesNum; else if (xmldata.searchBy.equals("Showname YYYY-MM-DD")) {
                try {
                    Date airTime = tvRageDateFormat.parse(tvrage.nextAirDate);
                    newzbin_query += tvRageDateFormatFix.format(airTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else if (xmldata.searchBy.equals("Showname EpisodeTitle")) newzbin_query += tvrage.nextTitle;
        }
        newzbin_query += "&searchaction=Search";
        newzbin_query += "&fpn=p";
        newzbin_query += "&category=8category=11";
        newzbin_query += "&area=-1";
        newzbin_query += "&u_nfo_posts_only=0";
        newzbin_query += "&u_url_posts_only=0";
        newzbin_query += "&u_comment_posts_only=0";
        newzbin_query += "&u_v3_retention=1209600";
        newzbin_query += "&ps_rb_language=" + xmldata.language;
        newzbin_query += "&sort=ps_edit_date";
        newzbin_query += "&order=desc";
        newzbin_query += "&areadone=-1";
        newzbin_query += "&feed=csv";
        newzbin_query += "&ps_rb_video_format=" + xmldata.format;
        newzbin_query = newzbin_query.replaceAll(" ", "%20");
        System.out.println("http://v3.newzbin.com/search/" + newzbin_query);
        try {
            URL url = new URL("http://v3.newzbin.com/search/" + newzbin_query);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            csvData = in.readLine();
            if (csvData != null) {
                JavaNZB.searchCount++;
                if (searchCount == 6) {
                    searchCount = 0;
                    System.out.println("Sleeping for 60 seconds");
                    try {
                        Thread.sleep(60000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                tmp = csvData.split(",");
                tmp[2] = tmp[2].substring(1, tmp[2].length() - 1);
                tmp[3] = tmp[3].substring(1, tmp[3].length() - 1);
                Pattern p = Pattern.compile("[\\\\</:>?\\[|\\]\"]");
                Matcher matcher = p.matcher(tmp[3]);
                tmp[3] = matcher.replaceAll(" ");
                tmp[3] = tmp[3].replaceAll("&", "and");
                URLConnection urlConn;
                DataOutputStream printout;
                url = new URL("http://v3.newzbin.com/api/dnzb/");
                urlConn = url.openConnection();
                urlConn.setDoInput(true);
                urlConn.setDoOutput(true);
                urlConn.setUseCaches(false);
                urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                printout = new DataOutputStream(urlConn.getOutputStream());
                String content = "username=" + JavaNZB.newzbinUsr + "&password=" + JavaNZB.newzbinPass + "&reportid=" + tmp[2];
                printout.writeBytes(content);
                printout.flush();
                printout.close();
                BufferedReader nzbInput = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
                File f = new File(JavaNZB.hellaQueueDir, tmp[3] + ".nzb");
                BufferedWriter out = new BufferedWriter(new FileWriter(f));
                String str;
                System.out.println("--Downloading " + tmp[3] + ".nzb" + " to queue directory--");
                while (null != ((str = nzbInput.readLine()))) out.write(str);
                nzbInput.close();
                out.close();
                if (latestOrNext == 0) {
                    xmldata.episode = tvrage.latestEpisodeNum;
                    xmldata.season = tvrage.latestSeasonNum;
                } else {
                    xmldata.episode = tvrage.nextEpisodeNum;
                    xmldata.season = tvrage.nextSeasonNum;
                }
            } else System.out.println("No new episode posted");
            System.out.println();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
            System.out.println("IO Exception from NzbSearch");
        }
        return xmldata;
    }


    public TVRageShowInfo(String xmlShowName) {
        String[] tmp, tmp2;
        String line = "";
        this.usrShowName = xmlShowName;
        try {
            URL url = new URL("http://www.tvrage.com/quickinfo.php?show=" + xmlShowName.replaceAll(" ", "%20"));
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            while ((line = in.readLine()) != null) {
                tmp = line.split("@");
                if (tmp[0].equals("Show Name")) showName = tmp[1];
                if (tmp[0].equals("Show URL")) showURL = tmp[1];
                if (tmp[0].equals("Latest Episode")) {
                    StringTokenizer st = new StringTokenizer(tmp[1], "^");
                    for (int i = 0; st.hasMoreTokens(); i++) {
                        if (i == 0) {
                            tmp2 = st.nextToken().split("x");
                            latestSeasonNum = tmp2[0];
                            latestEpisodeNum = tmp2[1];
                            if (latestSeasonNum.charAt(0) == '0') latestSeasonNum = latestSeasonNum.substring(1);
                        } else if (i == 1) latestTitle = st.nextToken().replaceAll("&", "and"); else latestAirDate = st.nextToken();
                    }
                }
                if (tmp[0].equals("Next Episode")) {
                    StringTokenizer st = new StringTokenizer(tmp[1], "^");
                    for (int i = 0; st.hasMoreTokens(); i++) {
                        if (i == 0) {
                            tmp2 = st.nextToken().split("x");
                            nextSeasonNum = tmp2[0];
                            nextEpisodeNum = tmp2[1];
                            if (nextSeasonNum.charAt(0) == '0') nextSeasonNum = nextSeasonNum.substring(1);
                        } else if (i == 1) nextTitle = st.nextToken().replaceAll("&", "and"); else nextAirDate = st.nextToken();
                    }
                }
                if (tmp[0].equals("Status")) status = tmp[1];
                if (tmp[0].equals("Airtime")) airTime = tmp[1];
            }
            in.close();
            url = new URL(showURL);
            in = new BufferedReader(new InputStreamReader(url.openStream()));
            while ((line = in.readLine()) != null) {
                if (line.indexOf("<b>Latest Episode: </b>") > -1) {
                    tmp = line.split("'>");
                    if (tmp[2].indexOf(':') > -1) {
                        tmp = tmp[2].split(":");
                        latestSeriesNum = tmp[0];
                    }
                } else if (line.indexOf("<b>Next Episode: </b>") > -1) {
                    tmp = line.split("'>");
                    if (tmp[2].indexOf(':') > -1) {
                        tmp = tmp[2].split(":");
                        nextSeriesNum = tmp[0];
                    }
                }
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
    }


    public void patch() throws IOException {
        if (mods.isEmpty()) {
            return;
        }
        IOUtils.copy(new FileInputStream(Paths.getMinecraftJarPath()), new FileOutputStream(new File(Paths.getMinecraftBackupPath())));
        JarFile mcjar = new JarFile(Paths.getMinecraftJarPath());
    }


    public ASDGrammarReader(String fileName, boolean includeCoords) throws IOException, MalformedURLException {
        includePixelCoords = includeCoords;
        fileName = fileName.trim();
        urlConnection = null;
        urlStream = null;
        if (fileName.substring(0, 5).equalsIgnoreCase("http:")) {
            URL fileURL = new URL(fileName);
            urlConnection = (HttpURLConnection) fileURL.openConnection();
            urlStream = urlConnection.getInputStream();
            reader = new ASDTokenReader(new BufferedReader(new InputStreamReader(urlStream)));
        } else reader = new ASDTokenReader(new FileReader(fileName));
    }


    public static boolean checkVersion(String vers) throws IOException {
        try {
            String tmp = "";
            URL url = new URL("http://rbmsoft.com.br/apis/ql/index.php?url=null&versao=" + vers);
            BufferedInputStream buf = new BufferedInputStream(url.openStream());
            int dado = 0;
            char letra;
            while ((dado = buf.read()) != -1) {
                letra = (char) dado;
                tmp += letra;
            }
            if (tmp.contains("FALSE")) {
                return false;
            } else if (tmp.contains("TRUE")) {
                new UpdateCheck().updateDialog();
                return true;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return false;
    }


    private static void readServicesFromUrl(Collection<String> list, URL url) throws IOException {
        InputStream in = url.openStream();
        try {
            if (in == null) return;
            BufferedReader r = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            while (true) {
                String line = r.readLine();
                if (line == null) break;
                int idx = line.indexOf('#');
                if (idx != -1) line = line.substring(0, idx);
                line = line.trim();
                if (line.length() == 0) continue;
                list.add(line);
            }
        } finally {
            try {
                if (in != null) in.close();
            } catch (Throwable ignore) {
            }
        }
    }


    static String fetchURLComposeExternPackageList(String urlpath, String pkglisturlpath) {
        String link = pkglisturlpath + "package-list";
        try {
            boolean relative = isRelativePath(urlpath);
            readPackageList((new URL(link)).openStream(), urlpath, relative);
        } catch (MalformedURLException exc) {
            return getText("doclet.MalformedURL", link);
        } catch (IOException exc) {
            return getText("doclet.URL_error", link);
        }
        return null;
    }


    public void connect() throws IOException {
        try {
            URL url = new URL(pluginUrl);
            connection = (HttpURLConnection) url.openConnection();
            sendNotification(DownloadState.CONNECTION_ESTABLISHED);
            contentLength = connection.getContentLength();
            sendNotification(DownloadState.CONTENT_LENGTH_SET);
            downloadedBytes = 0;
        } catch (java.io.IOException e) {
            e.printStackTrace();
            throw e;
        }
    }


    private void copyFileToDir(MyFile file, MyFile to, wlPanel panel) throws IOException {
        Utilities.print("started copying " + file.getAbsolutePath() + "\n");
        FileOutputStream fos = new FileOutputStream(new File(to.getAbsolutePath()));
        FileChannel foc = fos.getChannel();
        FileInputStream fis = new FileInputStream(new File(file.getAbsolutePath()));
        FileChannel fic = fis.getChannel();
        Date d1 = new Date();
        long amount = foc.transferFrom(fic, rest, fic.size() - rest);
        fic.close();
        foc.force(false);
        foc.close();
        Date d2 = new Date();
        long time = d2.getTime() - d1.getTime();
        double secs = time / 1000.0;
        double rate = amount / secs;
        frame.getStatusArea().append(secs + "s " + "amount: " + Utilities.humanReadable(amount) + " rate: " + Utilities.humanReadable(rate) + "/s\n", "black");
        panel.updateView();
    }


    private static String processStr(String srcStr, String sign) throws NoSuchAlgorithmException, NullPointerException {
        if (null == srcStr) {
            throw new java.lang.NullPointerException("��Ҫ���ܵ��ַ�ΪNull");
        }
        MessageDigest digest;
        String algorithm = "MD5";
        String result = "";
        digest = MessageDigest.getInstance(algorithm);
        digest.update(srcStr.getBytes());
        byte[] byteRes = digest.digest();
        int length = byteRes.length;
        for (int i = 0; i < length; i++) {
            result = result + byteHEX(byteRes[i], sign);
        }
        return result;
    }


    private void getRandomGUID(boolean secure) {
        MessageDigest md5 = null;
        StringBuffer sbValueBeforeMD5 = new StringBuffer();
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            System.out.println("Error: " + e);
        }
        try {
            long time = System.currentTimeMillis();
            long rand = 0;
            if (secure) {
                rand = mySecureRand.nextLong();
            } else {
                rand = myRand.nextLong();
            }
            sbValueBeforeMD5.append(s_id);
            sbValueBeforeMD5.append(":");
            sbValueBeforeMD5.append(Long.toString(time));
            sbValueBeforeMD5.append(":");
            sbValueBeforeMD5.append(Long.toString(rand));
            valueBeforeMD5 = sbValueBeforeMD5.toString();
            md5.update(valueBeforeMD5.getBytes());
            byte[] array = md5.digest();
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < array.length; ++j) {
                int b = array[j] & 0xFF;
                if (b < 0x10) sb.append('0');
                sb.append(Integer.toHexString(b));
            }
            valueAfterMD5 = sb.toString();
        } catch (Exception e) {
            System.out.println("Error:" + e);
        }
    }


    private void saveVideo(URL url, DownloadTask task) {
        if (url == null) {
            return;
        }
        FileOutputStream out = null;
        InputStream stream = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File file = new File(task.source.getPreferredVideoLocation("mp4"));
        publish("Starting download to file:" + file.getAbsolutePath());
        try {
            out = new FileOutputStream(file);
            URLConnection uc = url.openConnection();
            String contentType = uc.getContentType();
            int length = uc.getContentLength();
            StatisticsStorage.numberofdownloadedbytes += length;
            publish("Content type: " + contentType + " with a length of " + length);
            stream = uc.getInputStream();
            bis = new BufferedInputStream(stream);
            bos = new BufferedOutputStream(out);
            byte[] bytes = new byte[1024];
            int i = 0;
            long time = System.currentTimeMillis();
            int nextbyte;
            while ((nextbyte = bis.read()) != -1) {
                bos.write(nextbyte);
                i++;
                if (System.currentTimeMillis() - time >= 1000) {
                    publish("downloaded " + (i++) + " bytes of " + length + " bytes.");
                    time = System.currentTimeMillis();
                    int progress = (i - 1) / length;
                    if (progress <= 100 && progress >= 0) setProgress(progress);
                }
            }
            bos.write(bytes);
        } catch (IOException e) {
            System.err.println(url);
            e.printStackTrace();
        } finally {
            try {
                if (out != null) out.close();
                if (stream != null) stream.close();
                if (bis != null) bis.close();
                if (bos != null) bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("done downloading video");
        task.source.setVideo(file.getAbsolutePath());
        if (task.source.hasLyrics()) {
            task.source.getLyrics().setTag("Video", file.getName());
        }
    }


    private final void reOrderFriendsListByOnlineStatus() {
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = 0; i < friendsCount - 1; i++) if (friendsListOnlineStatus[i] < friendsListOnlineStatus[i + 1]) {
                int j = friendsListOnlineStatus[i];
                friendsListOnlineStatus[i] = friendsListOnlineStatus[i + 1];
                friendsListOnlineStatus[i + 1] = j;
                long l = friendsListLongs[i];
                friendsListLongs[i] = friendsListLongs[i + 1];
                friendsListLongs[i + 1] = l;
                flag = true;
            }
        }
    }


    public void zipDocsetFiles(SaxHandler theXmlHandler, int theEventId, Attributes theAtts) throws BpsProcessException {
        ZipOutputStream myZipOut = null;
        BufferedInputStream myDocumentInputStream = null;
        String myFinalFile = null;
        String myTargetPath = null;
        String myTargetFileName = null;
        String myInputFileName = null;
        byte[] myBytesBuffer = null;
        int myLength = 0;
        try {
            myZipOut = new ZipOutputStream(new FileOutputStream(myFinalFile));
            myZipOut.putNextEntry(new ZipEntry(myTargetPath + myTargetFileName));
            myDocumentInputStream = new BufferedInputStream(new FileInputStream(myInputFileName));
            while ((myLength = myDocumentInputStream.read(myBytesBuffer, 0, 4096)) != -1) myZipOut.write(myBytesBuffer, 0, myLength);
            myZipOut.closeEntry();
            myZipOut.close();
        } catch (FileNotFoundException e) {
            throw (new BpsProcessException(BpsProcessException.ERR_OPEN_FILE, "FileNotFoundException while building zip dest file"));
        } catch (IOException e) {
            throw (new BpsProcessException(BpsProcessException.ERR_OPEN_FILE, "IOException while building zip dest file"));
        }
    }


    protected synchronized String encryptThis(String seed, String text) throws EncryptionException {
        String encryptedValue = null;
        String textToEncrypt = text;
        if (seed != null) {
            textToEncrypt = seed.toLowerCase() + text;
        }
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(textToEncrypt.getBytes("UTF-8"));
            encryptedValue = (new BASE64Encoder()).encode(md.digest());
        } catch (Exception e) {
            throw new EncryptionException(e);
        }
        return encryptedValue;
    }


    public void convert(File src, File dest) throws IOException {
        InputStream in = new BufferedInputStream(new FileInputStream(src));
        DcmParser p = pfact.newDcmParser(in);
        Dataset ds = fact.newDataset();
        p.setDcmHandler(ds.getDcmHandler());
        try {
            FileFormat format = p.detectFileFormat();
            if (format != FileFormat.ACRNEMA_STREAM) {
                System.out.println("\n" + src + ": not an ACRNEMA stream!");
                return;
            }
            p.parseDcmFile(format, Tags.PixelData);
            if (ds.contains(Tags.StudyInstanceUID) || ds.contains(Tags.SeriesInstanceUID) || ds.contains(Tags.SOPInstanceUID) || ds.contains(Tags.SOPClassUID)) {
                System.out.println("\n" + src + ": contains UIDs!" + " => probable already DICOM - do not convert");
                return;
            }
            boolean hasPixelData = p.getReadTag() == Tags.PixelData;
            boolean inflate = hasPixelData && ds.getInt(Tags.BitsAllocated, 0) == 12;
            int pxlen = p.getReadLength();
            if (hasPixelData) {
                if (inflate) {
                    ds.putUS(Tags.BitsAllocated, 16);
                    pxlen = pxlen * 4 / 3;
                }
                if (pxlen != (ds.getInt(Tags.BitsAllocated, 0) >>> 3) * ds.getInt(Tags.Rows, 0) * ds.getInt(Tags.Columns, 0) * ds.getInt(Tags.NumberOfFrames, 1) * ds.getInt(Tags.NumberOfSamples, 1)) {
                    System.out.println("\n" + src + ": mismatch pixel data length!" + " => do not convert");
                    return;
                }
            }
            ds.putUI(Tags.StudyInstanceUID, uid(studyUID));
            ds.putUI(Tags.SeriesInstanceUID, uid(seriesUID));
            ds.putUI(Tags.SOPInstanceUID, uid(instUID));
            ds.putUI(Tags.SOPClassUID, classUID);
            if (!ds.contains(Tags.NumberOfSamples)) {
                ds.putUS(Tags.NumberOfSamples, 1);
            }
            if (!ds.contains(Tags.PhotometricInterpretation)) {
                ds.putCS(Tags.PhotometricInterpretation, "MONOCHROME2");
            }
            if (fmi) {
                ds.setFileMetaInfo(fact.newFileMetaInfo(ds, UIDs.ImplicitVRLittleEndian));
            }
            OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
            try {
            } finally {
                ds.writeFile(out, encodeParam());
                if (hasPixelData) {
                    if (!skipGroupLen) {
                        out.write(PXDATA_GROUPLEN);
                        int grlen = pxlen + 8;
                        out.write((byte) grlen);
                        out.write((byte) (grlen >> 8));
                        out.write((byte) (grlen >> 16));
                        out.write((byte) (grlen >> 24));
                    }
                    out.write(PXDATA_TAG);
                    out.write((byte) pxlen);
                    out.write((byte) (pxlen >> 8));
                    out.write((byte) (pxlen >> 16));
                    out.write((byte) (pxlen >> 24));
                }
                if (inflate) {
                    int b2, b3;
                    for (; pxlen > 0; pxlen -= 3) {
                        out.write(in.read());
                        b2 = in.read();
                        b3 = in.read();
                        out.write(b2 & 0x0f);
                        out.write(b2 >> 4 | ((b3 & 0x0f) << 4));
                        out.write(b3 >> 4);
                    }
                } else {
                    for (; pxlen > 0; --pxlen) {
                        out.write(in.read());
                    }
                }
                out.close();
            }
            System.out.print('.');
        } finally {
            in.close();
        }
    }


        private void init(URL url) {
            frame = new JInternalFrame(name);
            frame.addInternalFrameListener(this);
            listModel.add(listModel.size(), this);
            area = new JTextArea();
            area.setMargin(new Insets(5, 5, 5, 5));
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
                String in;
                while ((in = reader.readLine()) != null) {
                    area.append(in);
                    area.append("\n");
                }
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
            th = area.getTransferHandler();
            area.setFont(new Font("monospaced", Font.PLAIN, 12));
            area.setCaretPosition(0);
            area.setDragEnabled(true);
            area.setDropMode(DropMode.INSERT);
            frame.getContentPane().add(new JScrollPane(area));
            dp.add(frame);
            frame.show();
            if (DEMO) {
                frame.setSize(300, 200);
            } else {
                frame.setSize(400, 300);
            }
            frame.setResizable(true);
            frame.setClosable(true);
            frame.setIconifiable(true);
            frame.setMaximizable(true);
            frame.setLocation(left, top);
            incr();
            SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    select();
                }
            });
            nullItem.addActionListener(this);
            setNullTH();
        }


    public static ByteBuffer join(ByteBuffer buffer1, ByteBuffer buffer2) {
        if (buffer2 == null || buffer2.remaining() == 0) return NIOUtils.copy(buffer1);
        if (buffer1 == null || buffer1.remaining() == 0) return NIOUtils.copy(buffer2);
        ByteBuffer buffer = ByteBuffer.allocate(buffer1.remaining() + buffer2.remaining());
        buffer.put(buffer1);
        buffer.put(buffer2);
        buffer.flip();
        return buffer;
    }


    private void loadProperties() {
        if (properties == null) {
            properties = new Properties();
            try {
                URL url = getClass().getResource(propsFile);
                properties.load(url.openStream());
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getRequestURI().split("/")[3];
        if (log.isDebugEnabled()) log.debug("request: " + id + " from: " + request.getRemoteHost());
        Song song = manager.find(id);
        if (song != null) {
            File file = new File(song.getFile());
            if (file.exists()) {
                response.setStatus(HttpServletResponse.SC_OK);
                response.setContentType("audio/" + song.getType());
                response.setContentLength((int) file.length());
                FileInputStream stream = new FileInputStream(file);
                try {
                    IOUtils.copy(stream, response.getOutputStream());
                } finally {
                    IOUtils.closeQuietly(stream);
                }
            } else {
                log.warn("file not found: " + file);
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            }
        } else {
            log.info("song not found: " + id);
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
    }


    public void myCheckCredHandler(View v) {
        Log.d("login_section", "entered handler");
        EditText Username = (EditText) findViewById(R.id.Username);
        EditText Password = (EditText) findViewById(R.id.user_pass);
        String username_S = Username.getText().toString();
        String pass_S = Password.getText().toString();
        TextView ltv = (TextView) findViewById(R.id.LoginPagetv);
        HttpClient httpclient = createHttpClient();
        HttpPost httppost = new HttpPost(authenticationURL);
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("login", username_S));
            nameValuePairs.add(new BasicNameValuePair("password", pass_S));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            int status = response.getStatusLine().getStatusCode();
            String responseBody = EntityUtils.toString(response.getEntity());
            Log.d("login_section", responseBody);
            JSONObject jsonObject = new JSONObject(responseBody);
            if (status == this.statusOK && jsonObject.getBoolean("logged_in")) {
                ltv.setText("You have been logged in. :D");
                etracks.setLogged(true);
                etracks.setUserToken(jsonObject.getString("user_token"));
                Intent it = new Intent(Intent.ACTION_VIEW);
                it.setClassName(this, etracks.class.getName());
                this.finish();
                startActivity(it);
            } else if (status == this.statusUnprocessable && !jsonObject.getBoolean("logged_in")) {
                if (!jsonObject.isNull("errors")) ltv.setText(jsonObject.getString("errors")); else ltv.setText("login unsuccessful");
            } else Log.d("login_section", "what just happened?");
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public FlatFileFrame() {
        super("Specify Your Flat File Data");
        try {
            Class transferAgentClass = this.getStorageTransferAgentClass();
            if (transferAgentClass == null) {
                throw new RuntimeException("Transfer agent class can not be null.");
            }
            Class[] parameterTypes = new Class[] { RepositoryStorage.class };
            Constructor constr = transferAgentClass.getConstructor(parameterTypes);
            Object[] actualValues = new Object[] { this };
            this.transferAgent = (RepositoryStorageTransferAgent) constr.newInstance(actualValues);
        } catch (Exception err) {
            throw new RuntimeException("Unable to instantiate transfer agent.", err);
        }
        this.fmtlistener = new FormatTableModelListener();
        this.map = new HashMap();
        this.NoCallbackChangeMode = false;
        this.setSize(new Dimension(1000, 400));
        this.setLayout(new GridLayout(1, 1));
        this.Config = new FlatFileToolsConfig();
        this.Config.initialize();
        this.connectionHandler = new RepositoryConnectionHandler(this.Config);
        this.Connection = (FlatFileStorageConnectivity) this.connectionHandler.getConnection("default");
        this.Prefs = new FlatFileToolsPrefs();
        this.Prefs.initialize();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formatted_date = formatter.format(new Date());
        this.createdOnText = new JTextField(formatted_date);
        this.createdByText = new JTextField(this.Prefs.getConfigValue("createdby"));
        this.reposListeners = new Vector();
        this.removeFormatButton = new JButton("Remove");
        this.previewPanel = new DataSetPanel(new DataSet());
        this.previewPanel.setEditable(false);
        this.chooser = new JFileChooser();
        this.chooser.setMultiSelectionEnabled(true);
        this.enabledRadio = new JRadioButton("Enabled:");
        this.enabledRadio.setSelected(true);
        this.editPrefsButton = new JButton("Preferences...");
        this.editPrefsButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                System.out.println("Making visible");
                prefsEditor.setVisible(true);
            }
        });
        this.commentTextArea = new JTextArea(20, 8);
        this.commentTextArea.setText("No comment.");
        this.commentTextArea.setToolTipText("A detailed (possibly formatted) description including guidance to future developers of this set.");
        this.iconServer = new IconServer();
        this.iconServer.setConfigFile(this.Prefs.getConfigValue("default", "iconmapfile"));
        this.nicknameText = new IconifiedDomainNameTextField(new FlatFileFindNameDialog(Config, iconServer), this.iconServer);
        this.nicknameText.setPreferredSize(new Dimension(200, 25));
        this.nicknameText.setText(this.Prefs.getConfigValue("default", "domainname") + ".");
        this.nicknameText.setNameTextToolTipText("Right click to search the database.");
        this.uploadButton = new JButton("Upload");
        this.uploadButton.setToolTipText("Uploads current state to repository.");
        this.uploadButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                System.out.println("Trying to upload flat file spec...");
                try {
                    String expname = getNickname();
                    int split = expname.lastIndexOf('.');
                    String domain = "";
                    String name = "";
                    String usersdomain = Prefs.getConfigValue("default", "domainname");
                    if (split > 0) {
                        domain = expname.substring(0, split);
                        name = expname.substring(split + 1, expname.length());
                    } else {
                        name = expname;
                    }
                    name = name.trim();
                    if (name.equals("")) {
                        JOptionPane.showMessageDialog(null, "Cowardly refusing to upload with an empty flat file name...");
                        return;
                    }
                    if (!domain.equals(usersdomain)) {
                        int s = JOptionPane.showConfirmDialog(null, "If you are not the original author, you may wish to switch the current domain name " + domain + " to \nyour domain name " + usersdomain + ".  Would you like to do this?\n (If you'll be using this domain often, you may want to set it in your preferences.)", "Potential WWW name-space clash!", JOptionPane.YES_NO_CANCEL_OPTION);
                        if (s == JOptionPane.YES_OPTION) {
                            setNickname(usersdomain + "." + name);
                            executeTransfer();
                        }
                        if (s == JOptionPane.NO_OPTION) {
                            executeTransfer();
                        }
                    } else {
                        executeTransfer();
                    }
                } catch (Exception err) {
                    throw new RuntimeException("Problem uploading storage.", err);
                }
            }
        });
        this.repositoryView = new JButton("default");
        this.repositoryView.addActionListener(new ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent e) {
                repositoryEditor.setCurrentRepository(repositoryView.getText());
                repositoryEditor.setVisible(true);
            }
        });
        this.prefsEditor = new PrefsConfigFrame(this.Prefs);
        this.prefsEditor.setVisible(false);
        this.prefsEditor.addCloseListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                prefsEditor.setVisible(false);
            }
        });
        this.prefsEditor.addSelectListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                prefsEditor.setVisible(false);
            }
        });
        this.repositoryEditor = new ReposConfigFrame(this.Config);
        this.repositoryEditor.setVisible(false);
        this.repositoryEditor.addSelectListener(new SelectListener());
        this.repositoryEditor.addCloseListener(new CloseListener());
        this.addSources = new JButton("Source from file...");
        this.preview = new JButton("Preview");
        this.leastcolumn = new JSpinner();
        this.columns2show = new JSpinner();
        this.leastrow = new JSpinner();
        this.rows2show = new JSpinner();
        int rowCount = 10;
        JLabel sourceLabel = new JLabel("File Source");
        this.flatfilesource = new JTextField();
        this.flatfilesource.setPreferredSize(new Dimension(200, 25));
        this.flatfilesource.setMinimumSize(new Dimension(200, 25));
        this.flatfilesource.setMaximumSize(new Dimension(200, 25));
        this.isURLButton = new JRadioButton("URL");
        Box scrollBox = Box.createVerticalBox();
        Box srcBox = Box.createHorizontalBox();
        srcBox.add(this.addSources);
        srcBox.add(sourceLabel);
        srcBox.add(this.flatfilesource);
        srcBox.add(this.isURLButton);
        srcBox.add(this.preview);
        scrollBox.add(srcBox);
        Box detailsPanel = Box.createVerticalBox();
        Box detailsBox = Box.createVerticalBox();
        JLabel label;
        Box jointBox;
        jointBox = Box.createHorizontalBox();
        label = new JLabel("Pre-Header Lines:");
        this.preheaderlines = new JSpinner();
        jointBox.add(label);
        jointBox.add(this.preheaderlines);
        detailsBox.add(jointBox);
        this.preheaderlines.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent ev) {
                int selectedRow = fileselector.getSelectedRow();
                if (selectedRow >= 0) {
                    String fn = (String) sourcemodel.getValueAt(selectedRow, 0);
                    if (fn != null) {
                        updateDetailsFor(fn);
                    }
                }
            }
        });
        jointBox = Box.createHorizontalBox();
        label = new JLabel("Has Header Line:");
        this.hasHeaderLineBox = new JCheckBox();
        jointBox.add(label);
        jointBox.add(this.hasHeaderLineBox);
        detailsBox.add(jointBox);
        this.hasHeaderLineBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                int selectedRow = fileselector.getSelectedRow();
                if (selectedRow >= 0) {
                    String fn = (String) sourcemodel.getValueAt(selectedRow, 0);
                    if (fn != null) {
                        fn = fn.trim();
                        if ((fn != null) && (fn.length() > 0)) {
                            updateDetailsFor(fn);
                        }
                    }
                }
            }
        });
        jointBox = Box.createHorizontalBox();
        label = new JLabel("Post-Header Lines:");
        this.postheaderlines = new JSpinner();
        jointBox.add(label);
        jointBox.add(this.postheaderlines);
        detailsBox.add(jointBox);
        this.postheaderlines.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent ev) {
                int selectedRow = fileselector.getSelectedRow();
                if (selectedRow >= 0) {
                    String fn = (String) sourcemodel.getValueAt(selectedRow, 0);
                    if (fn != null) {
                        fn = fn.trim();
                        if ((fn != null) && (fn.length() > 0)) {
                            updateDetailsFor(fn);
                        }
                    }
                }
            }
        });
        jointBox = Box.createHorizontalBox();
        label = new JLabel("Format:");
        jointBox.add(label);
        this.singleFormatText = new JTextField("%s");
        jointBox.add(this.singleFormatText);
        jointBox.add(new JLabel("Repeat"));
        this.repeatFormatNumber = new JSpinner();
        this.repeatFormatNumber.setValue(new Integer(1));
        jointBox.add(this.repeatFormatNumber);
        this.addFormatButton = new JButton("Add");
        jointBox.add(this.addFormatButton);
        this.removeFormatButton = new JButton("Remove");
        jointBox.add(this.removeFormatButton);
        detailsBox.add(jointBox);
        jointBox = Box.createHorizontalBox();
        label = new JLabel("Column Format:");
        this.formatmodel = new FormatTableModel();
        this.formatTable = new JTable(this.formatmodel);
        this.formatmodel.addTableModelListener(this.fmtlistener);
        JTable hdrTable = this.formatTable.getTableHeader().getTable();
        this.formatTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        JScrollPane fsp = new JScrollPane(this.formatTable);
        fsp.setPreferredSize(new Dimension(200, 100));
        jointBox.add(label);
        jointBox.add(fsp);
        detailsBox.add(jointBox);
        jointBox = Box.createHorizontalBox();
        label = new JLabel("Field Delimiter:");
        this.fieldDelimiter = new JTextField("\\t");
        this.fieldDelimiter.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                int selectedRow = fileselector.getSelectedRow();
                if (selectedRow >= 0) {
                    String fn = (String) sourcemodel.getValueAt(selectedRow, 0);
                    if (fn != null) {
                        fn = fn.trim();
                        if ((fn != null) && (fn.length() > 0)) {
                            updateDetailsFor(fn);
                        }
                    }
                }
            }
        });
        jointBox.add(label);
        jointBox.add(this.fieldDelimiter);
        this.inferButton = new JButton("Infer");
        this.inferButton.setEnabled(false);
        jointBox.add(this.inferButton);
        detailsBox.add(jointBox);
        jointBox = Box.createHorizontalBox();
        label = new JLabel("Record Delimiter:");
        this.recordDelimiter = new JTextField("\\n");
        this.recordDelimiter.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                int selectedRow = fileselector.getSelectedRow();
                if (selectedRow >= 0) {
                    String fn = (String) sourcemodel.getValueAt(selectedRow, 0);
                    if (fn != null) {
                        fn = fn.trim();
                        if ((fn != null) && (fn.length() > 0)) {
                            updateDetailsFor(fn);
                        }
                    }
                }
            }
        });
        jointBox.add(label);
        jointBox.add(this.recordDelimiter);
        detailsBox.add(jointBox);
        detailsBox.add(Box.createVerticalGlue());
        detailsBox.add(Box.createVerticalGlue());
        detailsPanel.add(srcBox);
        detailsPanel.add(detailsBox);
        detailsPanel.add(previewPanel);
        this.addFormatButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                String fmt2rep = singleFormatText.getText();
                Integer rep = (Integer) repeatFormatNumber.getValue();
                Vector fmtparts = formatmodel.getFormatParts();
                int selectedCol = formatTable.getSelectedColumn();
                if (selectedCol < 0) {
                    selectedCol = formatTable.getColumnCount() - 1;
                }
                for (int r = 1; r <= rep.intValue(); r++) {
                    fmtparts.insertElementAt(fmt2rep, selectedCol);
                }
                formatmodel.setFormatParts(fmtparts);
                updateFormatTable();
                int selectedRow = fileselector.getSelectedRow();
                if ((selectedRow < sourcemodel.getRowCount()) && (selectedRow >= 0)) {
                    String fn = (String) sourcemodel.getValueAt(selectedRow, 0);
                    fn = fn.trim();
                    if (fn != null) {
                        fn = fn.trim();
                        if ((fn != null) && (fn.length() > 0)) {
                            updateDetailsFor(fn);
                        }
                    }
                }
            }
        });
        this.removeFormatButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                int selectedCol = formatTable.getSelectedColumn();
                if (selectedCol < 0) {
                    return;
                }
                Vector parts = formatmodel.getFormatParts();
                if (parts.size() == 1) {
                    throw new RuntimeException("At least one format column is required.");
                }
                parts.removeElementAt(selectedCol);
                formatmodel.setFormatParts(parts);
                updateFormatTable();
                int selectedRow = fileselector.getSelectedRow();
                if ((selectedRow < sourcemodel.getRowCount()) && (selectedRow >= 0)) {
                    String fn = (String) sourcemodel.getValueAt(selectedRow, 0);
                    fn = fn.trim();
                    if (fn != null) {
                        fn = fn.trim();
                        if ((fn != null) && (fn.length() > 0)) {
                            updateDetailsFor(fn);
                        }
                    }
                }
                System.out.println("The new Column count after remove is " + formatmodel.getColumnCount());
            }
        });
        this.inferButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                int row = fileselector.getSelectedRow();
                int col = 0;
                String filename = (String) sourcemodel.getValueAt(0, 0);
                Boolean isURL = (Boolean) sourcemodel.getValueAt(0, 1);
                BufferedReader br = null;
                File file = null;
                DataInputStream in = null;
                if (isURL.booleanValue()) {
                    try {
                        URL url2goto = new URL(filename);
                        in = new DataInputStream(url2goto.openStream());
                        System.out.println("READY TO READ FROM URL:" + url2goto);
                    } catch (Exception err) {
                        throw new RuntimeException("Problem constructing URI for " + filename + ".", err);
                    }
                } else {
                    file = new File(filename);
                    if (!file.exists()) {
                        throw new RuntimeException("The file named '" + filename + "' does not exist.");
                    }
                    FileInputStream fstream = null;
                    try {
                        fstream = new FileInputStream(filename);
                        in = new DataInputStream(fstream);
                    } catch (Exception err) {
                        throw new RuntimeException("Problem creating FileInputStream for " + filename + ".", err);
                    }
                }
                br = new BufferedReader(new InputStreamReader(in));
                JTable hdrTable = formatTable.getTableHeader().getTable();
                try {
                    String strLine;
                    int line = 0;
                    int ignorePreHdrLines = ((Integer) preheaderlines.getValue()).intValue();
                    int ignorePostHdrLines = ((Integer) postheaderlines.getValue()).intValue();
                    int numhdr = 0;
                    boolean hasHeaderLine = false;
                    if (hasHeaderLineBox.isSelected()) {
                        hasHeaderLine = true;
                    }
                    if (hasHeaderLine) {
                        numhdr = 1;
                    }
                    String FD = fieldDelimiter.getText();
                    while ((strLine = br.readLine()) != null) {
                        if (line <= (ignorePreHdrLines + numhdr + ignorePostHdrLines)) {
                            System.out.println(strLine);
                        } else {
                            String[] putative_cols = strLine.split(FD);
                            System.out.println("The number of potential columns is " + putative_cols.length);
                            String FMT = "";
                            while (formatTable.getColumnCount() > putative_cols.length) {
                                TableColumn tcol = formatTable.getColumnModel().getColumn(0);
                                formatTable.removeColumn(tcol);
                            }
                            for (int i = 0; i < putative_cols.length; i++) {
                                String fmt = "";
                                try {
                                    Double dummy = new Double(putative_cols[i]);
                                    fmt = "%f";
                                } catch (Exception err) {
                                    fmt = "%s";
                                }
                                FMT = FMT + fmt;
                                formatTable.setValueAt(fmt, 0, i);
                            }
                            System.out.println("The potential format is " + FMT);
                            formatmodel.setFormat(FMT);
                            break;
                        }
                        line++;
                    }
                    in.close();
                } catch (Exception err) {
                    throw new RuntimeException("Problem reading single line from file.", err);
                }
                for (int j = 0; j < formatTable.getColumnCount(); j++) {
                    hdrTable.getColumnModel().getColumn(j).setHeaderValue("" + (j + 1));
                }
                formatTable.repaint();
            }
        });
        Box topbox = Box.createHorizontalBox();
        topbox.add(detailsPanel);
        Box mainbox = Box.createVerticalBox();
        Box setBox = Box.createHorizontalBox();
        setBox.add(this.editPrefsButton);
        jointBox = Box.createHorizontalBox();
        label = new JLabel("Created On:");
        jointBox.add(label);
        this.createdOnText.setPreferredSize(new Dimension(50, 25));
        jointBox.add(this.createdOnText);
        setBox.add(jointBox);
        jointBox = Box.createHorizontalBox();
        label = new JLabel("Created By:");
        jointBox.add(label);
        this.createdByText.setPreferredSize(new Dimension(50, 25));
        jointBox.add(this.createdByText);
        setBox.add(jointBox);
        setBox.add(this.uploadButton);
        setBox.add(this.repositoryView);
        setBox.add(this.nicknameText);
        setBox.add(this.enabledRadio);
        mainbox.add(setBox);
        jointBox = Box.createHorizontalBox();
        label = new JLabel("Comment:");
        jointBox.add(label);
        jointBox.add(new JScrollPane(this.commentTextArea));
        mainbox.add(jointBox);
        mainbox.add(topbox);
        mainbox.add(previewPanel);
        this.add(mainbox);
        this.addSources.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                int option = chooser.showOpenDialog(null);
                File[] files = null;
                if (option == JFileChooser.APPROVE_OPTION) {
                    files = chooser.getSelectedFiles();
                    if (files.length > 10) {
                        ((DefaultTableModel) sourcemodel).setRowCount(files.length);
                    } else {
                        ((DefaultTableModel) sourcemodel).setRowCount(10);
                    }
                    for (int i = 0; i < files.length; i++) {
                        sourcemodel.setValueAt(files[i].getAbsolutePath(), i, 0);
                    }
                }
                if (anyNonEmptySources()) {
                    allowFormatParsing(true);
                } else {
                    allowFormatParsing(false);
                }
            }
        });
        this.preview.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                FlatFileDOM[] filespecs = new FlatFileDOM[map.size()];
                int k = 0;
                for (int j = 0; j < sourcemodel.getRowCount(); j++) {
                    String fn = (String) sourcemodel.getValueAt(j, 0);
                    if (map.containsKey(fn)) {
                        filespecs[k] = (FlatFileDOM) map.get(fn);
                        k++;
                    }
                }
                Vector hdrs = null;
                Vector types = null;
                for (int j = 0; j < filespecs.length; j++) {
                    DataSetReader rdr = new DataSetReader(filespecs[j]);
                    int rc = rdr.determineRowCount();
                    filespecs[j].setRowCount(rc);
                    if (j == 0) {
                        hdrs = rdr.getHeaders();
                        types = rdr.getTypes();
                    }
                    System.out.println("The number of rows is=" + rc);
                }
                System.out.println("Creating flatfileset");
                FlatFileSet dataset = new FlatFileSet(filespecs);
                System.out.println("Finished sorting!!!");
                for (int j = 0; j < hdrs.size(); j++) {
                    dataset.addColumn((String) hdrs.get(j), (Class) types.get(j));
                }
                System.out.println("Number of headers is=" + hdrs.size());
                System.out.println("The dataset rc is " + dataset.getRowCount());
                System.out.println("The dataset cc is " + dataset.getColumnCount());
                previewPanel.setDataSet(dataset);
                previewPanel.setVerticalScrollIntermittant(true);
                previewPanel.setHorizontalScrollIntermittant(true);
                previewPanel.setEditable(false);
                if (anyNonEmptySources()) {
                    allowFormatParsing(true);
                } else {
                    allowFormatParsing(false);
                }
            }
        });
        allowFormatParsing(false);
        this.formatTable.repaint();
    }


    public static void loadConfig(URL urlFile) throws CacheException {
        Document document;
        try {
            document = Utilities.getDocument(urlFile.openStream());
        } catch (IOException e) {
            throw new CacheException("Could not open '" + urlFile.getFile() + "'", e);
        } catch (JAnalyticsException e) {
            throw new CacheException("Could not open '" + urlFile.getFile() + "'", e);
        }
        Element element = (Element) document.getElementsByTagName(DOCUMENT_CACHE_ELEMENT_NAME).item(0);
        if (element != null) {
            String className = element.getAttribute(CLASSNAME_ATTRIBUTE_NAME);
            if (className != null) {
                Properties config = new Properties();
                NodeList nodes = element.getElementsByTagName(PARAM_ELEMENT_NAME);
                if (nodes != null) {
                    for (int i = 0, count = nodes.getLength(); i < count; i++) {
                        Node node = nodes.item(i);
                        if (node instanceof Element) {
                            Element n = (Element) node;
                            String name = n.getAttribute(NAME_ATTRIBUTE_NAME);
                            String value = n.getAttribute(VALUE_ATTRIBUTE_NAME);
                            config.put(name, value);
                        }
                    }
                }
                loadConfig(className, config);
            }
        }
    }


    private void openGSM() {
        try {
            GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
            int cid = gcl.getCid();
            int lac = gcl.getLac();
            int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0, 3));
            int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5));
            JSONObject holder = new JSONObject();
            holder.put("version", "1.1.0");
            holder.put("host", "maps.google.com");
            holder.put("request_address", true);
            JSONArray array = new JSONArray();
            JSONObject data = new JSONObject();
            data.put("cell_id", cid);
            data.put("location_area_code", lac);
            data.put("mobile_country_code", mcc);
            data.put("mobile_network_code", mnc);
            array.put(data);
            holder.put("cell_towers", array);
            DefaultHttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://www.google.com/loc/json");
            StringEntity se = new StringEntity(holder.toString());
            post.setEntity(se);
            HttpResponse resp = client.execute(post);
            HttpEntity entity = resp.getEntity();
            BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
            StringBuffer sb = new StringBuffer();
            String result = br.readLine();
            while (result != null) {
                sb.append(result);
                result = br.readLine();
            }
            mLat = Double.parseDouble(sb.toString().split(":")[2].split(",")[0]);
            mLon = Double.parseDouble(sb.toString().split(":")[3].split(",")[0]);
        } catch (Exception e) {
        }
        returnGeoLatLon();
    }


    public void connect() throws IOException {
        if (log.isDebugEnabled()) log.debug("Connecting to: " + HOST);
        ftpClient.connect(HOST);
        if (log.isDebugEnabled()) log.debug("\tReply: " + ftpClient.getReplyString());
        if (log.isDebugEnabled()) log.debug("Login as anonymous");
        ftpClient.login("anonymous", "");
        if (log.isDebugEnabled()) log.debug("\tReply: " + ftpClient.getReplyString());
        folder = INTACT_FOLDER;
    }


    public APIResponse delete(String id) throws Exception {
        APIResponse response = new APIResponse();
        connection = (HttpURLConnection) new URL(url + "/api/record/delete/" + id).openConnection();
        connection.setRequestMethod("DELETE");
        connection.setConnectTimeout(TIMEOUT);
        connection.connect();
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            response.setDone(true);
            response.setMessage("Record Deleted!");
        } else {
            response.setDone(false);
            response.setMessage("Delete Record Error Code: Http (" + connection.getResponseCode() + ")");
        }
        connection.disconnect();
        return response;
    }


    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        OutputStream output = getOutputStream();
        if (cachedContent != null) {
            output.write(cachedContent);
        } else {
            FileInputStream input = new FileInputStream(dfosFile);
            IOUtils.copy(input, output);
            dfosFile.delete();
            dfosFile = null;
        }
        output.close();
        cachedContent = null;
    }


    public static String getDeclaredXMLEncoding(URL url) throws IOException {
        InputStream stream = url.openStream();
        BufferedReader buffReader = new BufferedReader(new InputStreamReader(stream));
        String firstLine = buffReader.readLine();
        if (firstLine == null) {
            return SYSTEM_ENCODING;
        }
        int piStart = firstLine.indexOf("<?xml version=\"1.0\"");
        if (piStart != -1) {
            int attributeStart = firstLine.indexOf("encoding=\"");
            if (attributeStart >= 0) {
                int nextQuote = firstLine.indexOf('"', attributeStart + 10);
                if (nextQuote >= 0) {
                    String encoding = firstLine.substring(attributeStart + 10, nextQuote);
                    return encoding.trim();
                }
            }
        }
        stream.close();
        return SYSTEM_ENCODING;
    }


    public void testGetContentInputStream() {
        URL url;
        try {
            url = new URL("http://www.wurzer.org/" + "Homepage/Publikationen/Eintrage/2009/10/7_Wissen_dynamisch_organisieren_files/" + "KnowTech%202009%20-%20Wissen%20dynamisch%20organisieren.pdf");
            InputStream in = url.openStream();
            Content c = provider.getContent(in);
            assertNotNull(c);
            assertTrue(!c.getFulltext().isEmpty());
            assertTrue(c.getModificationDate() < System.currentTimeMillis());
            assertTrue(c.getAttributes().size() > 0);
            assertEquals("KnowTech 2009 - Wissen dynamisch organisieren", c.getAttributeByName("Title").getValue());
            assertEquals("Joerg Wurzer", c.getAttributeByName("Author").getValue());
            assertEquals("Pages", c.getAttributeByName("Creator").getValue());
            assertNull(c.getAttributeByName("Keywords"));
            assertTrue(c.getFulltext().startsWith("Wissen dynamisch organisieren"));
            assertTrue(c.getAttributeByName("Author").isKey());
            assertTrue(!c.getAttributeByName("Producer").isKey());
        } catch (MalformedURLException e) {
            fail("Malformed url - " + e.getMessage());
        } catch (IOException e) {
            fail("Couldn't read file - " + e.getMessage());
        }
    }


    private String sendMessage(HttpURLConnection connection, String reqMessage) throws IOException {
        if (msgLog.isTraceEnabled()) msgLog.trace("Outgoing SOAPMessage\n" + reqMessage);
        BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
        out.write(reqMessage.getBytes("UTF-8"));
        out.close();
        InputStream inputStream = null;
        if (connection.getResponseCode() < 400) inputStream = connection.getInputStream(); else inputStream = connection.getErrorStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
        IOUtils.copyStream(baos, inputStream);
        inputStream.close();
        String response = new String(baos.toByteArray(), "UTF-8");
        if (msgLog.isTraceEnabled()) msgLog.trace("Incoming Response SOAPMessage\n" + response);
        return response;
    }


    private void upgradeSchema() {
        Statement stmt = null;
        boolean updatedSchema = false;
        try {
            int i = getSchema();
            if (i < SCHEMA_VERSION) {
                conn.setAutoCommit(false);
                stmt = conn.createStatement();
                updatedSchema = true;
            }
            while (i < SCHEMA_VERSION) {
                String qry;
                switch(i) {
                    case 0:
                        qry = "CREATE TABLE settings (var VARCHAR(32) NOT NULL, val LONG VARCHAR)";
                        stmt.executeUpdate(qry);
                        qry = "INSERT INTO settings (var, val) VALUES ('schema', '1')";
                        stmt.executeUpdate(qry);
                        qry = "ALTER TABLE recordings ADD COLUMN exe LONG VARCHAR NOT NULL DEFAULT '%UNKNOWN%'";
                        stmt.executeUpdate(qry);
                        qry = "CREATE TABLE files (id INT NOT NULL, file LONG VARCHAR NOT NULL, finished INT NOT NULL)";
                        stmt.executeUpdate(qry);
                        updateFilesTable();
                        break;
                    case 1:
                        qry = "ALTER TABLE files ADD COLUMN type VARCHAR(32) NOT NULL DEFAULT '%UNKNOWN%'";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '2' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 2:
                        qry = "CREATE UNIQUE INDEX IF NOT EXISTS recordings_history ON recordings(id,type)";
                        stmt.executeUpdate(qry);
                        qry = "CREATE INDEX IF NOT EXISTS files_history ON files(id,type)";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '3' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 3:
                        qry = "CREATE TABLE log (id INTEGER PRIMARY KEY, context VARCHAR(16) NOT NULL, level VARCHAR(16) NOT NULL, time LONG INT NOT NULL, msg LONG VARCHAR NOT NULL, parent INT)";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '4' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 4:
                        qry = "CREATE UNIQUE INDEX IF NOT EXISTS log_id ON log(id)";
                        stmt.executeUpdate(qry);
                        qry = "CREATE INDEX IF NOT EXISTS log_parent ON log(parent)";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '5' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 5:
                        qry = "CREATE TABLE tmp_settings (var varchar(32) NOT NULL PRIMARY KEY, val varchar(128) NOT NULL)";
                        stmt.executeUpdate(qry);
                        qry = "INSERT INTO tmp_settings SELECT var,val FROM settings";
                        stmt.executeUpdate(qry);
                        qry = "DROP TABLE settings";
                        stmt.executeUpdate(qry);
                        qry = "ALTER TABLE tmp_settings RENAME TO settings";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '6' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 6:
                        qry = "DROP TABLE log";
                        stmt.executeUpdate(qry);
                        qry = "CREATE TABLE log (id INTEGER PRIMARY KEY, mediaid varchar(32) NOT NULL, taskid varchar(32) NOT NULL, msg LONG VARCHAR NOT NULL)";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '7' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 7:
                        qry = "CREATE TABLE client (host varchar(255) PRIMARY KEY, conf LONG VARCHAR)";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '8' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 8:
                        qry = "DROP INDEX files_history";
                        stmt.executeUpdate(qry);
                        qry = "DROP INDEX recordings_history";
                        stmt.executeUpdate(qry);
                        qry = "DROP TABLE files";
                        stmt.executeUpdate(qry);
                        qry = "CREATE TABLE new_rec (id INT NOT NULL, type VARCHAR(32) NOT NULL, start INT NOT NULL DEFAULT 0, finish INT NOT NULL DEFAULT 0, state INT NOT NULL DEFAULT 1, PRIMARY KEY(id, type))";
                        stmt.executeUpdate(qry);
                        qry = "INSERT INTO new_rec SELECT id, type, 0, 0, 1 FROM recordings";
                        stmt.executeUpdate(qry);
                        qry = "DROP TABLE recordings";
                        stmt.executeUpdate(qry);
                        qry = "CREATE TABLE recordings (id INT NOT NULL, type VARCHAR(32) NOT NULL, start INT NOT NULL DEFAULT 0, finish INT NOT NULL DEFAULT 0, state INT NOT NULL DEFAULT 1, PRIMARY KEY(id, type))";
                        stmt.executeUpdate(qry);
                        qry = "INSERT INTO recordings SELECT * FROM new_rec";
                        stmt.executeUpdate(qry);
                        qry = "DROP TABLE new_rec";
                        stmt.executeUpdate(qry);
                        qry = "CREATE UNIQUE INDEX recordings_history on recordings(id,type)";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '9' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 9:
                        qry = "ALTER TABLE recordings ADD COLUMN host VARCHAR(255) NOT NULL DEFAULT ''";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '10' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 10:
                        qry = "ALTER TABLE recordings ADD COLUMN airing VARCHAR(64) NOT NULL DEFAULT '0'";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '11' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 11:
                        PreparedStatement pstmt = null;
                        try {
                            MessageDigest msg = MessageDigest.getInstance("MD5");
                            msg.update("sjqadmin".getBytes());
                            String pwd = new String(msg.digest());
                            pstmt = conn.prepareStatement("REPLACE INTO settings (var, val) VALUES ('password', ?)");
                            pstmt.setString(1, pwd);
                            pstmt.executeUpdate();
                        } catch (NoSuchAlgorithmException e) {
                            throw new SQLException(e);
                        } finally {
                            if (pstmt != null) pstmt.close();
                        }
                        stmt.executeUpdate("UPDATE settings SET val = '12' WHERE var = 'schema'");
                        break;
                    case 12:
                        qry = "CREATE INDEX logs_for_tasks ON log(mediaid, taskid)";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '13' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 13:
                        qry = "DELETE FROM log";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '14' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 14:
                        qry = "DROP TABLE log";
                        stmt.executeUpdate(qry);
                        qry = "CREATE TABLE log (id INTEGER PRIMARY KEY, mediaid varchar(32) NOT NULL, taskid varchar(32) NOT NULL, msg LONG VARCHAR NOT NULL, ts INTEGER NOT NULL DEFAULT 0)";
                        stmt.executeUpdate(qry);
                        qry = "CREATE INDEX logs_by_date ON log(ts)";
                        stmt.executeUpdate(qry);
                        qry = "CREATE INDEX IF NOT EXISTS logs_for_tasks ON log(mediaid, taskid)";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '15' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 15:
                        qry = "DELETE FROM log WHERE mediaid = 0 AND taskid = '0'";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '16' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 16:
                        qry = "CREATE TEMPORARY TABLE rec_tmp (objtype varchar(64) NOT NULL DEFAULT 'media', id INT NOT NULL, type VARCHAR(32) NOT NULL, start INT NOT NULL DEFAULT 0, finish INT NOT NULL DEFAULT 0, state INT NOT NULL DEFAULT 1, host VARCHAR(255) NOT NULL DEFAULT '', airing VARCHAR(64) NOT NULL DEFAULT '0', PRIMARY KEY (objtype, id, type))";
                        stmt.executeUpdate(qry);
                        qry = "INSERT INTO rec_tmp SELECT 'media', id, type, start, finish, state, host, airing FROM recordings";
                        stmt.executeUpdate(qry);
                        qry = "DROP TABLE recordings";
                        stmt.executeUpdate(qry);
                        qry = "CREATE TABLE recordings (objtype varchar(64) NOT NULL DEFAULT 'media', id INT NOT NULL, type VARCHAR(32) NOT NULL, start INT NOT NULL DEFAULT 0, finish INT NOT NULL DEFAULT 0, state INT NOT NULL DEFAULT 1, host VARCHAR(255) NOT NULL DEFAULT '', airing VARCHAR(64) NOT NULL DEFAULT '0', PRIMARY KEY (objtype, id, type))";
                        stmt.executeUpdate(qry);
                        qry = "INSERT INTO recordings SELECT * FROM rec_tmp";
                        stmt.executeUpdate(qry);
                        qry = "DROP TABLE rec_tmp";
                        stmt.executeUpdate(qry);
                        qry = "ALTER TABLE log ADD COLUMN objtype varchar(64) NOT NULL DEFAULT 'media'";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '17' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                    case 17:
                        qry = "DROP INDEX logs_for_tasks";
                        stmt.executeUpdate(qry);
                        qry = "CREATE INDEX logs_for_tasks ON log(mediaid, taskid, objtype)";
                        stmt.executeUpdate(qry);
                        qry = "UPDATE settings SET val = '18' WHERE var = 'schema'";
                        stmt.executeUpdate(qry);
                        break;
                }
                i++;
            }
            if (updatedSchema) conn.commit();
        } catch (SQLException e) {
            try {
                if (updatedSchema) conn.rollback();
            } catch (SQLException x) {
                LOG.fatal("SQL error", x);
            }
            LOG.fatal("SQL error", e);
            throw new RuntimeException(e);
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (updatedSchema) conn.setAutoCommit(true);
            } catch (SQLException e) {
                LOG.fatal("SQL error", e);
                throw new RuntimeException(e);
            }
        }
    }


    private void load() throws SQLException {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = FidoDataSource.getConnection();
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            ClearData.clearTables(stmt);
            stmt.executeUpdate("insert into Objects (ObjectId, Description) values (100, 'Person')");
            stmt.executeUpdate("insert into Objects (ObjectId, Description) values (101, 'john')");
            stmt.executeUpdate("insert into Objects (ObjectId, Description) values (200, 'Dog')");
            stmt.executeQuery("select setval('objects_objectid_seq', 1000)");
            stmt.executeUpdate("insert into ClassLinkTypes (LinkName, LinkType) values ('hasa', 2)");
            stmt.executeUpdate("insert into ObjectLinks (ObjectId, LinkName, LinkToObject) values (100, 'isa', 1)");
            stmt.executeUpdate("insert into ObjectLinks (ObjectId, LinkName, LinkToObject) values (101, 'instance', 100)");
            stmt.executeUpdate("insert into ObjectLinks (ObjectId, LinkName, LinkToObject) values (200, 'isa', 1)");
            stmt.executeUpdate("insert into Dictionary (Word, SenseNumber, GrammarString, ObjectId) values ('LEFT-WALL', '1', 'AV+', 1)");
            stmt.executeUpdate("insert into Dictionary (Word, SenseNumber, GrammarString, ObjectId) values ('john', '1', 'S+ | DO-', 1)");
            stmt.executeUpdate("insert into Dictionary (Word, SenseNumber, GrammarString, ObjectId) values ('a', '1', 'D+', 1)");
            stmt.executeUpdate("insert into Dictionary (Word, SenseNumber, GrammarString, ObjectId) values ('dog', '1', '[D-] & (S+ | DO-)', 200)");
            stmt.executeUpdate("insert into Dictionary (Word, SenseNumber, GrammarString, ObjectId) values ('have', '1', 'S- & AV- & DO+', 1)");
            stmt.executeUpdate("insert into LanguageMorphologies (LanguageName, MorphologyTag, Rank, Root, Surface, Used) values " + "                                 ('English', 'third singular', 1, 'have', 'has', TRUE)");
            stmt.executeUpdate("insert into GrammarLinks (LinkName, LinkType) values ('S', 1)");
            stmt.executeUpdate("insert into GrammarLinks (LinkName, LinkType) values ('DO', 3)");
            stmt.executeUpdate("insert into GrammarLinks (LinkName, LinkType) values ('AV', 7)");
            stmt.executeUpdate("insert into GrammarLinks (LinkName, LinkType) values ('D', 10)");
            stmt.executeUpdate("insert into Articles (ArticleName, Dereference) values ('a', 2)");
            stmt.executeUpdate("insert into FrameSlots (SlotName) values ('actor')");
            stmt.executeUpdate("insert into FrameSlots (SlotName) values ('object')");
            stmt.executeUpdate("insert into Verbs (VerbName, Type, SubjectSlot, IndirectObjectSlot, PredicateNounSlot) values ('have', 1, 'actor', '', 'object')");
            stmt.executeQuery("select setval('instructions_instructionid_seq', 1)");
            stmt.executeUpdate("insert into Instructions (Type, ExecuteString, FrameSlot, Operator, LinkName, ObjectId, AttributeName) " + "values (3, 'link %actor hasa %object', null, 0, null, null, null)");
            stmt.executeQuery("select setval('transactions_transactionid_seq', 1)");
            stmt.executeUpdate("insert into Transactions (InstructionId, Description) values (2, 'have - link')");
            stmt.executeQuery("select setval('verbtransactions_verbid_seq', 1)");
            stmt.executeUpdate("insert into VerbTransactions (VerbString, MoodType, TransactionId) values ('have', 1, 2)");
            stmt.executeUpdate("insert into VerbConstraints (VerbId, FrameSlot, ObjectId) values (2, 'actor', 1)");
            stmt.executeUpdate("insert into VerbConstraints (VerbId, FrameSlot, ObjectId) values (2, 'object', 1)");
            stmt.executeUpdate("insert into ProperNouns (Noun, SenseNumber, ObjectId) values ('john', 1, 101)");
            stmt.executeUpdate("update SystemProperties set value = 'Tutorial 1 Data' where name = 'DB Data Version'");
            conn.commit();
        } catch (SQLException e) {
            if (conn != null) conn.rollback();
            throw e;
        } finally {
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        }
    }


    public static synchronized String getSequenceNumber(String SequenceName) {
        String result = "0";
        Connection conn = null;
        Statement ps = null;
        ResultSet rs = null;
        try {
            conn = TPCW_Database.getConnection();
            conn.setAutoCommit(false);
            String sql = "select num from sequence where name='" + SequenceName + "'";
            ps = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = ps.executeQuery(sql);
            long num = 0;
            while (rs.next()) {
                num = rs.getLong(1);
                result = new Long(num).toString();
            }
            num++;
            sql = "update sequence set num=" + num + " where name='" + SequenceName + "'";
            int res = ps.executeUpdate(sql);
            if (res == 1) {
                conn.commit();
            } else conn.rollback();
        } catch (Exception e) {
            System.out.println("Error Happens when trying to obtain the senquence number");
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) conn.close();
                if (rs != null) rs.close();
                if (ps != null) ps.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
        return result;
    }


    public HttpURLConnection execute(S3Bucket pBucket, S3Object pObject, S3OperationParameters pOpParams) throws S3Exception {
        S3OperationParameters opParams = pOpParams;
        if (opParams == null) opParams = new S3OperationParameters();
        HttpURLConnection result = null;
        URL url = getURL(pBucket, pObject, opParams.getQueryParameters());
        mLogger.log(Level.FINEST, "URL: " + url.toString());
        opParams.addDateHeader();
        switch(mStyle) {
            case Path:
                opParams.addHostHeader(BASE_DOMAIN);
                break;
            case Subdomain:
                if (pBucket == null) opParams.addHostHeader(BASE_DOMAIN); else opParams.addHostHeader(pBucket.getName() + "." + BASE_DOMAIN);
                break;
            case VirtualHost:
                if (pBucket == null) opParams.addHostHeader(BASE_DOMAIN); else opParams.addHostHeader(pBucket.getName());
                break;
        }
        if (opParams.isSign()) {
            StringBuilder sb = new StringBuilder();
            sb.append(opParams.getVerb().toString());
            sb.append(NEWLINE);
            sb.append(posHeader(MD5, opParams.getRequestHeaders()));
            sb.append(posHeader(TYPE, opParams.getRequestHeaders()));
            if (opParams.getQueryParameters().has(EXPIRES)) {
                sb.append(opParams.getQueryParameters().get(EXPIRES).getValue());
                sb.append(NEWLINE);
            } else {
                sb.append(posHeader(DATE, opParams.getRequestHeaders()));
            }
            sb.append(canonicalizeAmazonHeaders(opParams.getRequestHeaders()));
            try {
                sb.append("/");
                if (pBucket != null) {
                    sb.append(URLEncoder.encode(pBucket.getName(), URL_ENCODING));
                    sb.append("/");
                    if (pObject != null) {
                        sb.append(URLEncoder.encode(pObject.getKey(), URL_ENCODING));
                    }
                }
                sb.append(opParams.getQueryParameters().getAmazonSubresources().toQueryString());
                String signThis = sb.toString();
                mLogger.log(Level.FINEST, "String being signed: " + signThis);
                String sig = encode(mCredential.getMSecretAccessKey(), signThis, false);
                sb = new StringBuilder();
                sb.append("AWS ");
                sb.append(mCredential.getMAccessKeyID());
                sb.append(":");
                sb.append(sig);
                opParams.addAuthorizationHeader(sb.toString());
            } catch (UnsupportedEncodingException e) {
                throw new S3Exception("URL encoding not supported: " + URL_ENCODING, e);
            }
        }
        try {
            killHostVerifier();
            URLConnection urlConn = url.openConnection();
            if (!(urlConn instanceof HttpURLConnection)) throw new S3Exception("URLConnection is not instance of HttpURLConnection!");
            result = (HttpURLConnection) urlConn;
            result.setRequestMethod(opParams.getVerb().toString());
            mLogger.log(Level.FINEST, "HTTP Operation: " + opParams.getVerb().toString());
            if (opParams.getVerb() == HttpVerb.PUT) {
                result.setDoOutput(true);
            }
            result.setRequestProperty(TYPE, "");
            for (AWSParameter param : opParams.getRequestHeaders()) {
                result.setRequestProperty(param.getName(), param.getValue());
                mLogger.log(Level.FINEST, "Header " + param.getName() + ": " + param.getValue());
            }
        } catch (IOException e) {
            throw new S3Exception("Problem opening connection to URL: " + url.toString(), e);
        }
        return result;
    }


    public static void main(String[] args) {
        StringBuffer htmlPage;
        htmlPage = new StringBuffer();
        double min = 99999.99;
        double max = 0;
        double value = 0;
        try {
            URL url = new URL("http://search.ebay.com/" + args[0]);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String line;
            while ((line = in.readLine()) != null) {
                htmlPage.append(line);
                htmlPage.append("\n");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Pattern p = Pattern.compile("\\$([\\d\\.]+)", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(htmlPage);
        while (m.find()) {
            if (m.start(0) < m.end(0)) {
                value = Double.parseDouble(m.group(1));
                if (value < min) {
                    min = value;
                }
                if (value > max) {
                    max = value;
                }
            }
        }
        if (min == 99999.99) {
            min = 0;
        }
        System.out.println(args[0] + "," + min + "," + max);
        System.exit(0);
    }


    public static String MD5(String source) {
        logger.info(source);
        String result = null;
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(source.getBytes());
            byte[] bytes = digest.digest();
            result = EncodeUtils.hexEncode(bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        logger.info(result);
        return result;
    }


    public static String SHA256(String source) {
        logger.info(source);
        String result = null;
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            digest.update(source.getBytes());
            byte[] bytes = digest.digest();
            result = EncodeUtils.hexEncode(bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        logger.info(result);
        return result;
    }