Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.attender.service.remoting; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.validator.EmailValidator; import org.directwebremoting.annotations.RemoteProxy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.seajas.search.attender.service.attender.AttenderService; import com.seajas.search.attender.service.mail.MailService; import com.seajas.search.bridge.profiler.wsdl.taxonomy.TaxonomyNode; import com.seajas.search.profiler.wsdl.taxonomy.ITaxonomyListing; import com.seajas.search.utilities.spring.security.service.ExtendedUserDetailsService; /** * Remoting service. * * @author Jasper van Veghel <jasper.van.veghel@codec-it.nl */ @RemoteProxy @Service public class RemotingService { /** * User details service. */ @Autowired private ExtendedUserDetailsService userDetailsService; /** * Attender service. */ @Autowired private AttenderService attenderService; /** * Mail service. */ @Autowired private MailService mailService; /** * Taxonomy listing service. */ @Autowired private ITaxonomyListing taxonomyListingService; /** * Retrieve a given set of child nodes (or the root node if null). * * @param id * @return List<TaxonomyNode> */ public List<TaxonomyNode> getTaxonomyChildren(final Integer id) { return taxonomyListingService.retrieveNodesFromParent(id); } /** * Retrieve a set of parent node IDs for the given node ID (inclusive). * * @param id * @return List<Integer> */ public List<Integer> getTaxonomyParentIds(final Integer id) { return taxonomyListingService.retrieveParentIds(id); } /** * Determine which job statuses have been modified. * * @param current * @return Map<String, String> */ public Map<String, String> getModifiedJobs(final Map<String, String> current) { Map<String, String> jobs = attenderService.getSchedulerJobs(); Map<String, String> result = new HashMap<String, String>(); for (Entry<String, String> entry : jobs.entrySet()) if (current.containsKey(entry.getKey()) && !entry.getValue().equals(current.get(entry.getKey()))) result.put(entry.getKey(), entry.getValue()); return result; } /** * Start a given job. * * @param jobName */ public void startSchedulerJob(final String jobName) { attenderService.startSchedulerJob(jobName); } /** * Stop a given job. * * @param jobName */ public void stopSchedulerJob(final String jobName) { attenderService.stopSchedulerJob(jobName); } /** * Determine whether a given email is valid. * * @param email * @return boolean */ public boolean validEmail(final String email) { return EmailValidator.getInstance().isValid(email); } /** * Determine whether a given username already exists. * * @param username * @return boolean */ public boolean existsUsername(final String username) { return userDetailsService.existsUsername(username); } /** * Update the mail settings. * * @param hostname * @param useSSL * @param username * @param password * @param sender * @return boolean */ public boolean updateMailServerDetails(final String hostname, final Boolean useSSL, final String username, final String password, final String sender) { String hostnameWithPort = hostname.indexOf(":") != -1 ? hostname : hostname + ":" + (useSSL ? 465 : 25); String hostnameAsUri = (useSSL ? "smtps" : "smtp") + "://" + hostnameWithPort; if (attenderService.validateMailServerDetails(hostnameAsUri, username, password, sender)) attenderService.updateMailServerDetails(hostnameWithPort, username, password, useSSL ? "smtps" : "smtp", sender); else return false; // Just to double-check return attenderService.validateCurrentMailServerDetails(); } /** * Send a test e-mail to test the mail service. * * @param toAddress * @return boolean */ public boolean sendTestMail(final String toAddress) { mailService.updateWorkingMailServer(); if (mailService.hasWorkingMailServer()) return mailService.sendMessage(toAddress, "Search Attender test e-mail", "If you can read this e-mail, the mail server was configured correctly.", "<html><head /><body><p>If you can read this e-mail, the mail server was configured correctly.</p></body></html>"); else return false; } }