PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/it/unibz/pomodroid/models/Service.java

http://github.com/dgraziotin/openpomo
Java | 470 lines | 255 code | 40 blank | 175 comment | 10 complexity | fddc5158b53777fbcbb2a0d07be5686f MD5 | raw file
Possible License(s): GPL-3.0
  1. /**
  2. * This file is part of Pomodroid.
  3. *
  4. * Pomodroid is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * Pomodroid is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with Pomodroid. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package it.unibz.pomodroid.models;
  18. import java.util.List;
  19. import android.util.Log;
  20. import it.unibz.pomodroid.exceptions.PomodroidException;
  21. import com.db4o.ObjectSet;
  22. import com.db4o.query.Predicate;
  23. import com.db4o.query.Query;
  24. /**
  25. * A class representing a Service.
  26. *
  27. * @author Daniel Graziotin 4801 <daniel.graziotin@stud-inf.unibz.it>
  28. */
  29. public class Service {
  30. /**
  31. * User given name for the Service
  32. */
  33. private String name;
  34. /**
  35. * URL for the Service
  36. */
  37. private String url;
  38. /**
  39. * Service username
  40. */
  41. private String username;
  42. /**
  43. * Service password (if needed)
  44. */
  45. private String password;
  46. /**
  47. * Type of Service (Trac, BugZilla, ..)
  48. */
  49. private String type;
  50. /**
  51. * True if the Service does not require HTTP BASIC AUTH
  52. */
  53. private boolean isAnonymousAccess;
  54. /**
  55. * True if the User wants issues to be searched from it
  56. */
  57. private boolean isActive;
  58. /**
  59. * Default Constructor. Sets all fields to null
  60. */
  61. public Service() {
  62. this.name = null;
  63. this.url = null;
  64. this.username = null;
  65. this.password = null;
  66. this.isAnonymousAccess = false;
  67. this.setType(null);
  68. this.isActive = true;
  69. }
  70. /**
  71. * @param name
  72. * @param url
  73. * @param type
  74. * @param username
  75. * @param password
  76. * @param isAnonymousAccess
  77. */
  78. public Service(String name, String url, String type, String username, String password, boolean isAnonymousAccess) {
  79. this.name = name;
  80. this.url = url;
  81. this.setType(type);
  82. this.username = username;
  83. this.password = password;
  84. this.isAnonymousAccess = isAnonymousAccess;
  85. this.isActive = true;
  86. }
  87. /**
  88. * @param name
  89. * @param url
  90. * @param type
  91. * @param username
  92. */
  93. public Service(String name, String url, String type, String username) {
  94. this.name = name;
  95. this.url = url;
  96. this.setType(type);
  97. this.username = username;
  98. this.password = "";
  99. this.isAnonymousAccess = true;
  100. this.isActive = true;
  101. }
  102. /**
  103. * @return the name
  104. */
  105. public String getName() {
  106. return name;
  107. }
  108. /**
  109. * @param name the name to set
  110. */
  111. public void setName(String name) {
  112. this.name = name;
  113. }
  114. /**
  115. * @return the url
  116. */
  117. public String getUrl() {
  118. return url;
  119. }
  120. /**
  121. * @param url the url to set
  122. */
  123. public void setUrl(String url) {
  124. this.url = url;
  125. }
  126. /**
  127. * @return the username
  128. */
  129. public String getUsername() {
  130. return username;
  131. }
  132. /**
  133. * @param username the username to set
  134. */
  135. public void setUsername(String username) {
  136. this.username = username;
  137. }
  138. /**
  139. * @return the password
  140. */
  141. public String getPassword() {
  142. return password;
  143. }
  144. /**
  145. * @param password the password to set
  146. */
  147. public void setPassword(String password) {
  148. this.password = password;
  149. }
  150. /**
  151. * @return the isAnonymousAccess
  152. */
  153. public boolean isAnonymousAccess() {
  154. return isAnonymousAccess;
  155. }
  156. /**
  157. * @param isAnonymousAccess the isAnonymousAccess to set
  158. */
  159. public void setAnonymousAccess(boolean isAnonymousAccess) {
  160. this.isAnonymousAccess = isAnonymousAccess;
  161. }
  162. /**
  163. * @param isActive
  164. */
  165. public void setActive(boolean isActive) {
  166. this.isActive = isActive;
  167. }
  168. /**
  169. * @return
  170. */
  171. public boolean isActive() {
  172. return isActive;
  173. }
  174. /**
  175. * @param type
  176. */
  177. public void setType(String type) {
  178. this.type = type;
  179. }
  180. /**
  181. * @return
  182. */
  183. public String getType() {
  184. return type;
  185. }
  186. /**
  187. * Helper method for updating an existing Service
  188. *
  189. * @param service
  190. */
  191. public void update(Service service) {
  192. this.name = service.getName();
  193. this.url = service.getUrl();
  194. this.type = service.getType();
  195. this.username = service.getUsername();
  196. this.password = service.getPassword();
  197. this.isAnonymousAccess = service.isAnonymousAccess();
  198. this.isActive = service.isActive();
  199. }
  200. /**
  201. * Returns true if a Service with the given name is already
  202. * present in the DB
  203. *
  204. * @param name
  205. * @param dbHelper
  206. * @return true if the Service is Present
  207. * @throws PomodroidException
  208. */
  209. public static boolean isPresent(final String name,
  210. DBHelper dbHelper) throws PomodroidException {
  211. List<Service> services;
  212. try {
  213. services = dbHelper.getDatabase().query(
  214. new Predicate<Service>() {
  215. private static final long serialVersionUID = 1L;
  216. public boolean match(Service service) {
  217. return service.getName().equals(name);
  218. }
  219. });
  220. if (services.isEmpty())
  221. return false;
  222. else
  223. return true;
  224. } catch (Exception e) {
  225. Log.e("Service.isPresent()", "Problem: " + e.toString());
  226. throw new PomodroidException("ERROR in Service.isPresent():"
  227. + e.toString());
  228. }
  229. }
  230. /**
  231. * Returns true if a Service with a given URL is present
  232. * in the DB
  233. *
  234. * @param url
  235. * @param dbHelper
  236. * @return a specific Service
  237. * @throws PomodroidException
  238. */
  239. public static boolean isPresentUrl(final String url,
  240. DBHelper dbHelper) throws PomodroidException {
  241. List<Service> services;
  242. try {
  243. services = dbHelper.getDatabase().query(
  244. new Predicate<Service>() {
  245. private static final long serialVersionUID = 1L;
  246. public boolean match(Service service) {
  247. return service.getUrl().equals(url);
  248. }
  249. });
  250. if (services.isEmpty())
  251. return false;
  252. else
  253. return true;
  254. } catch (Exception e) {
  255. Log.e("Service.isPresentUrl()", "Problem: " + e.toString());
  256. throw new PomodroidException("ERROR in Service.isPresent():"
  257. + e.toString());
  258. }
  259. }
  260. /**
  261. * Saves a Service. It is also responsible for updating a Service if it is
  262. * already stored in the DB
  263. *
  264. * @param dbHelper
  265. * @return true if a Service is saved into the DB
  266. * @throws PomodroidException
  267. */
  268. public boolean save(DBHelper dbHelper) throws PomodroidException {
  269. try {
  270. if (!isPresent(this.getName(), dbHelper)) {
  271. dbHelper.getDatabase().store(this);
  272. return true;
  273. } else {
  274. return this.update(dbHelper);
  275. }
  276. } catch (PomodroidException e) {
  277. Log.e("Service.save()", "Problem: " + e.toString());
  278. Log.e("Service.save()", "Problem: " + e.getMessage());
  279. throw new PomodroidException("ERROR in Service.save():"
  280. + e.toString());
  281. } finally {
  282. dbHelper.commit();
  283. }
  284. }
  285. /**
  286. * Updates an existing Service
  287. *
  288. * @param dbHelper
  289. * @return true if a service is updated into the DB
  290. * @throws PomodroidException
  291. */
  292. private boolean update(DBHelper dbHelper) throws PomodroidException {
  293. Service oldService = Service.get(this.getName(), dbHelper);
  294. try {
  295. oldService.update(this);
  296. dbHelper.getDatabase().store(oldService);
  297. return true;
  298. } catch (Exception e) {
  299. Log.e("Service.save(update)", "Update Problem: " + e.toString());
  300. throw new PomodroidException("ERROR in Service.save(update):"
  301. + e.toString());
  302. } finally {
  303. dbHelper.commit();
  304. }
  305. }
  306. /**
  307. * Delete all Services
  308. *
  309. * @param dbHelper
  310. * @return
  311. * @throws PomodroidException
  312. */
  313. public static boolean deleteAll(DBHelper dbHelper)
  314. throws PomodroidException {
  315. ObjectSet<Service> services = null;
  316. try {
  317. services = dbHelper.getDatabase().query(new Predicate<Service>() {
  318. private static final long serialVersionUID = 1L;
  319. @Override
  320. public boolean match(Service arg0) {
  321. // TODO Auto-generated method stub
  322. return false;
  323. }
  324. });
  325. while (services.hasNext()) {
  326. dbHelper.getDatabase().delete(services.next());
  327. }
  328. return true;
  329. } catch (Exception e) {
  330. throw new PomodroidException("ERROR in Service.deleteAll():"
  331. + e.toString());
  332. } finally {
  333. dbHelper.commit();
  334. }
  335. }
  336. /**
  337. * Returns all Services
  338. *
  339. * @param dbHelper
  340. * @return a list of Services
  341. * @throws PomodroidException
  342. */
  343. public static List<Service> getAll(DBHelper dbHelper)
  344. throws PomodroidException {
  345. ObjectSet<Service> result;
  346. try {
  347. result = dbHelper.getDatabase().queryByExample(Service.class);
  348. return result;
  349. } catch (Exception e) {
  350. throw new PomodroidException("ERROR in Service.getAll()"
  351. + e.toString());
  352. }
  353. }
  354. /**
  355. * Returns all active Services
  356. *
  357. * @param dbHelper
  358. * @return a list of Services
  359. * @throws PomodroidException
  360. */
  361. public static List<Service> getAllActive(DBHelper dbHelper)
  362. throws PomodroidException {
  363. List<Service> services = null;
  364. Query query = null;
  365. try {
  366. query = dbHelper.getDatabase().query();
  367. query.constrain(Service.class);
  368. query.descend("isActive").constrain(true);
  369. services = query.execute();
  370. } catch (Exception e) {
  371. Log.e("Service.getAllActive()", "Problem: " + e.toString());
  372. throw new PomodroidException("ERROR in Service.getAllActive():"
  373. + e.toString());
  374. }
  375. if (services != null)
  376. Log.i("Active Services", "" + services.size());
  377. return services;
  378. }
  379. /**
  380. * Deletes a Service
  381. *
  382. * @param dbHelper
  383. * @throws PomodroidException
  384. */
  385. public void delete(DBHelper dbHelper) throws PomodroidException {
  386. ObjectSet<Service> result;
  387. Service toBeDeleted = null;
  388. try {
  389. toBeDeleted = Service.get(this.getName(), dbHelper);
  390. result = dbHelper.getDatabase().queryByExample(toBeDeleted);
  391. Service found = (Service) result.next();
  392. dbHelper.getDatabase().delete(found);
  393. } catch (Exception e) {
  394. Log.e("Service.delete()", "Problem: " + e.toString());
  395. throw new PomodroidException("ERROR in Service.delete():"
  396. + e.toString());
  397. } finally {
  398. dbHelper.commit();
  399. }
  400. }
  401. /**
  402. * Retrieves a Service from the DB, given its name
  403. *
  404. * @param name
  405. * @param dbHelper
  406. * @return a specific Service
  407. * @throws PomodroidException
  408. */
  409. public static Service get(final String name,
  410. DBHelper dbHelper) throws PomodroidException {
  411. List<Service> services = null;
  412. Service result = null;
  413. try {
  414. services = dbHelper.getDatabase().query(
  415. new Predicate<Service>() {
  416. private static final long serialVersionUID = 1L;
  417. public boolean match(Service service) {
  418. return service.getName().equals(name);
  419. }
  420. });
  421. if (services != null && services.size() > 0)
  422. result = services.get(0);
  423. } catch (Exception e) {
  424. Log.e("Service.getService()", "Problem: " + e.toString());
  425. throw new PomodroidException("ERROR in Service.getService():"
  426. + e.toString());
  427. }
  428. return result;
  429. }
  430. }