Android Open Source - swazam Transaction Service






From Project

Back to project page swazam.

License

The source code is released under:

MIT License

If you think the Android project swazam listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ac.tuwien.sa13.service;
//from  w w w . j a v  a  2 s .c  o m
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import ac.tuwien.sa13.dao.IRequestDAO;
import ac.tuwien.sa13.dao.ITransactionDAO;
import ac.tuwien.sa13.dao.IUserDAO;
import ac.tuwien.sa13.entity.Request;
import ac.tuwien.sa13.entity.Transaction;
import ac.tuwien.sa13.entity.User;

@Transactional(propagation= Propagation.REQUIRED, readOnly=false)
@Service("transactionService")
public class TransactionService implements ITransactionService {
    
  private static final int REQUEST_COST = 1;

  private static final int REWARD = 0;

  @Autowired
  private SessionFactory sessionFactory;
  
  @Autowired
    private ITransactionDAO transactionDAO;
  
  @Autowired
  private IUserService userService;
  
  public void add(Transaction entity) {
    transactionDAO.add(entity);    
  }

  public void update(Transaction entity) {
    transactionDAO.update(entity);    
  }

  public void remove(Transaction entity) {
    transactionDAO.remove(entity);    
  }

  public Transaction find(Long key) {
    return transactionDAO.find(key);
  }

  public List<Transaction> list() {
    return transactionDAO.list();
  }

  /**
   * This method checks if a user is allowed to make a request (enough tokens on his account)
   * At the same time the method subtracts the amount of token for one request, given he
   * can afford it.
   */
  @Override
  public Transaction allowRequest(User user, Request request) {
    if(user != null && user.getTokens() > 0){
      int temp = user.getTokens() - REQUEST_COST;
      user.setTokens(temp);
      userService.update(user);
      Transaction transaction = new Transaction();
      transaction.setIsReward(false);
      transaction.setUser(user);
      transaction.setRequest(request);
      return transaction;
    }
    return null;
  }
  
  /**
   * This method shall be called when a peer delivers a result for a request. The user
   * with whom the peer is authenticated is rewarded with the defined reward.
   * @param user
   * @param request
   * @return The transaction (contains user and request info) but has to be saved! Call userService.add(transaction)
   */
  @Override
  public Transaction rewardUser(User user, Request request){
    Transaction transaction = new Transaction();
    transaction.setUser(user);
    transaction.setRequest(request);
    int temp = user.getTokens();
    user.setTokens(temp + REWARD);
    userService.update(user);
    return transaction;
  }
  @Override
  public List<Transaction> getTransactions(User user) {
    Query query = sessionFactory.getCurrentSession().createQuery("FROM Transaction WHERE user_id= :user_id");
    query.setParameter("user_id", user.getId());
    
    List<Transaction> l = query.list();
    
    if (l.isEmpty()) {
      return null;
    }
    return  l;
  }

}




Java Source Code List

ac.tuwien.sa13.beans.HibernateAwareObjectMapper.java
ac.tuwien.sa13.controller.HomeController.java
ac.tuwien.sa13.controller.LoginController.java
ac.tuwien.sa13.controller.RequestController.java
ac.tuwien.sa13.controller.TransactionController.java
ac.tuwien.sa13.controller.api.UserRestController.java
ac.tuwien.sa13.dao.HibernateDAO.java
ac.tuwien.sa13.dao.IGenericDAO.java
ac.tuwien.sa13.dao.IRequestDAO.java
ac.tuwien.sa13.dao.ITransactionDAO.java
ac.tuwien.sa13.dao.IUserDAO.java
ac.tuwien.sa13.dao.RequestDAO.java
ac.tuwien.sa13.dao.TransactionDAO.java
ac.tuwien.sa13.dao.UserDAO.java
ac.tuwien.sa13.entity.Request.java
ac.tuwien.sa13.entity.Transaction.java
ac.tuwien.sa13.entity.User.java
ac.tuwien.sa13.service.IGenericService.java
ac.tuwien.sa13.service.IRequestService.java
ac.tuwien.sa13.service.ITransactionService.java
ac.tuwien.sa13.service.IUserService.java
ac.tuwien.sa13.service.PeerManager.java
ac.tuwien.sa13.service.RequestService.java
ac.tuwien.sa13.service.TransactionService.java
ac.tuwien.sa13.service.UserService.java
ac.tuwien.sa13.utilities.SessionIdentifierGenerator.java
ac.tuwien.sa13.utilities.Token.java
ac.tuwien.sa13.validator.LoginValidator.java
at.ac.tuwien.swa13.swazam.NetworkConnection.java
at.ac.tuwien.swa13.swazam.Peer.java
at.ac.tuwien.swa13.swazam.fingerprint.IFingerPrintCreator.java
at.ac.tuwien.swa13.swazam.fingerprint.impl.FingerPrintCreator.java
at.ac.tuwien.swa13.swazam.library.ILibraryManager.java
at.ac.tuwien.swa13.swazam.library.ISong.java
at.ac.tuwien.swa13.swazam.library.impl.LibraryManager.java
at.ac.tuwien.swa13.swazam.library.impl.Song.java
at.ac.tuwien.swa13.swazam.p2p.PeerConnector.java
at.saws2013.szazam.App.java
at.saws2013.szazam.BaseActivity.java
at.saws2013.szazam.HistoryActivity.java
at.saws2013.szazam.LoginActivity.java
at.saws2013.szazam.RecognitionActivity.java
at.saws2013.szazam.SettingsActivity.java
at.saws2013.szazam.entities.IListItem.java
at.saws2013.szazam.entities.ListItemTypes.java
at.saws2013.szazam.entities.Request.java
at.saws2013.szazam.entities.Transaction.java
at.saws2013.szazam.fingerprint.IFingerPrintSystem.java
at.saws2013.szazam.fingerprint.impl.FingerPrintCreator.java
at.saws2013.szazam.fragments.SettingsFragment.java
at.saws2013.szazam.media.IAudioFilePicker.java
at.saws2013.szazam.media.IAudioRecorder.java
at.saws2013.szazam.media.impl.AudioFilePicker.java
at.saws2013.szazam.media.impl.AudioRecorder.java
at.saws2013.szazam.store.IAuthStore.java
at.saws2013.szazam.store.IRequestStore.java
at.saws2013.szazam.store.ITransactionStore.java
at.saws2013.szazam.store.impl.AuthStore.java
at.saws2013.szazam.store.impl.RequestStore.java
at.saws2013.szazam.store.impl.TransactionStore.java
at.saws2013.szazam.ui.CustomListAdapter.java
at.saws2013.szazam.ui.ViewTools.java
at.saws2013.szazam.volley.CustomVolleyStringRequest.java
de.passsy.holocircularprogressbar.HoloCircularProgressBar.java