SHARE
TWEET

Untitled

a guest Aug 18th, 2018 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package lv.lu.fileshare.svc;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.logging.Logger;
  7.  
  8. import lv.lu.fileshare.UploadedFile;
  9. import lv.lu.fileshare.UserInfo;
  10. import lv.lu.fileshare.Utils;
  11.  
  12. import com.google.appengine.api.datastore.DatastoreService;
  13. import com.google.appengine.api.datastore.DatastoreServiceFactory;
  14. import com.google.appengine.api.datastore.Entity;
  15. import com.google.appengine.api.datastore.EntityNotFoundException;
  16. import com.google.appengine.api.datastore.FetchOptions;
  17. import com.google.appengine.api.datastore.Key;
  18. import com.google.appengine.api.datastore.KeyFactory;
  19. import com.google.appengine.api.datastore.PreparedQuery;
  20. import com.google.appengine.api.datastore.Query;
  21. import com.google.appengine.api.datastore.Transaction;
  22. import com.google.appengine.api.datastore.TransactionOptions;
  23. import com.google.appengine.api.datastore.Query.FilterOperator;
  24. import com.google.appengine.api.datastore.Query.SortDirection;
  25.  
  26. public class GaeDatastoreDataService implements DataService {
  27.     private static final String KIND_USER = "User";
  28.     private static final String KIND_USER_GRP = "UserGroup";
  29.     private static final String KIND_NICK_GRP = "NickGroup";
  30.     private static final String KIND_FILE = "File";
  31.     private static final String KIND_NICK = "Nick";
  32.    
  33.     private final static Logger logger = Logger.getLogger(GaeDatastoreDataService.class.getName());
  34.    
  35.     private Transaction t;
  36.    
  37.     DatastoreService datastore;
  38.    
  39.     public GaeDatastoreDataService() {
  40.         datastore =
  41.                 DatastoreServiceFactory.getDatastoreService();
  42.     }
  43.    
  44.     public void beginTransaction() {
  45.         t = null;
  46.         logger.info("beginTransaction" + this);
  47.         t = datastore.beginTransaction(TransactionOptions.Builder.withDefaults().setXG(true));
  48.     }
  49.    
  50.     public void endTransaction() {
  51.         logger.info("endTransaction" + this);
  52.         if (t!= null && t.isActive()) {
  53.             t.rollback();
  54.         }
  55.         t = null;
  56.     }
  57.    
  58.     public void commitTransaction() {
  59.         logger.info("commitTransaction" + this);
  60.         t.commit();
  61.         t = null;
  62.     }
  63.    
  64.     public UserInfo loginCheck(String email, String pass, boolean ignorePass) {
  65.         Query query = null;
  66.        
  67.         query = new Query(KIND_USER_GRP).addFilter("name", FilterOperator.EQUAL, calcUserGroupName(email));
  68.  
  69.         Entity userGrpEnt = datastore.prepare(query).asSingleEntity();
  70.        
  71.         if (userGrpEnt == null) {
  72.             throw new RuntimeException("user group missing");
  73.         }
  74.        
  75.         query = new Query(KIND_USER).addFilter("email", FilterOperator.EQUAL, email).setAncestor(userGrpEnt.getKey());
  76.        
  77.         Entity userEnt = datastore.prepare(query).asSingleEntity();
  78.        
  79.         if (userEnt == null) return null;
  80.        
  81.         final String realPass = (String)userEnt.getProperty("pass");
  82.        
  83.         if (ignorePass || Utils.calcMd5(pass).equals(realPass)) { // ignorePass - logojoties caur google
  84.             return new UserInfo(email, (String)userEnt.getProperty("nick"), userEnt.getKey());
  85.         } else {
  86.             return null;
  87.         }
  88.     }
  89.    
  90.     // be sure that email&nick is unused before call this
  91.     public void registerAccount(String email, String nick, String pass) {
  92.         Query query = null;
  93.        
  94.         query = new Query(KIND_USER_GRP).addFilter("name", FilterOperator.EQUAL, calcUserGroupName(email));
  95.  
  96.         Entity userGrpEnt = datastore.prepare(query).asSingleEntity();
  97.        
  98.         Entity usrEnt = new Entity(KIND_USER, userGrpEnt.getKey());
  99.         usrEnt.setProperty("nick", nick);
  100.         usrEnt.setProperty("email", email);
  101.         usrEnt.setProperty("pass", pass == null ? null : Utils.calcMd5(pass));
  102.        
  103.         final Key nickGrpKey = KeyFactory.createKey(KIND_NICK_GRP, calcNickGroupName(nick));
  104.         Entity nickEnt = new Entity(KIND_NICK, nickGrpKey);
  105.         nickEnt.setProperty("name", nick);
  106.        
  107.         datastore.put(usrEnt);
  108.         datastore.put(nickEnt);
  109.     }
  110.    
  111.     public void editProfile(String newNick, String oldNick, String newPass, String emailRo) {
  112.         // saving changes & reserving nick
  113.        
  114.         final Key userGrpKey = KeyFactory.createKey(KIND_USER_GRP, calcUserGroupName(emailRo));
  115.         Query query = new Query(KIND_USER).addFilter("email", FilterOperator.EQUAL, emailRo).setAncestor(userGrpKey);
  116.         Entity userEnt = datastore.prepare(query).asSingleEntity();
  117.        
  118.         if (newPass != null && !newPass.equals("")) {
  119.             userEnt.setProperty("pass", Utils.calcMd5(newPass));
  120.         }
  121.         userEnt.setProperty("nick", newNick);
  122.        
  123.         // userEnt != NULL always
  124.        
  125.         datastore.put(userEnt);
  126.        
  127.         if (!newNick.equals(oldNick)) {
  128.             final Key nickGrpKey = KeyFactory.createKey(KIND_NICK_GRP, calcNickGroupName(newNick));
  129.             Entity nickEnt = new Entity(KIND_NICK, nickGrpKey);
  130.             nickEnt.setProperty("name", newNick);
  131.             datastore.put(nickEnt);
  132.            
  133.             final Key oldNickGrpKey = KeyFactory.createKey(KIND_NICK_GRP, calcNickGroupName(oldNick));
  134.             query = new Query(KIND_NICK).addFilter("name", FilterOperator.EQUAL, oldNick).setAncestor(oldNickGrpKey);
  135.             Entity oldNickEnt = datastore.prepare(query).asSingleEntity();
  136.            
  137.             // oldNickEnt!=null always
  138.             // release old nick
  139.             datastore.delete(oldNickEnt.getKey());
  140.         }
  141.     }
  142.    
  143.     @Override
  144.     public boolean emailAvailable(String email) {
  145.         Query query = null;
  146.        
  147.         query = new Query(KIND_USER_GRP).addFilter("name", FilterOperator.EQUAL, calcUserGroupName(email));
  148.  
  149.         Entity userGrpEnt = datastore.prepare(query).asSingleEntity();
  150.        
  151.         if (userGrpEnt == null) {
  152.             throw new RuntimeException("user group missing");
  153.         }
  154.        
  155.         query = new Query(KIND_USER).addFilter("email", FilterOperator.EQUAL, email).setAncestor(userGrpEnt.getKey());
  156.        
  157.         Entity userEnt = datastore.prepare(query).asSingleEntity();
  158.         return userEnt == null;
  159.     }
  160.    
  161.     private String calcUserGroupName(String email) {
  162.         // TODO: implementēt dalīšanu vairākās kastēs
  163.         return "BigBox";
  164.     }
  165.    
  166.     private String calcNickGroupName(String nick) {
  167.         // TODO: implementēt dalīšanu vairākās kastēs
  168.         return "BigNickBox";
  169.     }
  170.  
  171.     @Override
  172.     public void install() {
  173.         Entity emailBox = new Entity(KIND_USER_GRP, "BigBox");
  174.         emailBox.setProperty("name", "BigBox");
  175.         datastore.put(emailBox);
  176.        
  177.         Entity nickBox = new Entity(KIND_NICK_GRP, "BigNickBox");
  178.         nickBox.setProperty("name", "BigNickBox");
  179.         datastore.put(nickBox);
  180.     }
  181.    
  182.     public boolean nickAvailable(String nick) {
  183.         final Key nickBoxKey = KeyFactory.createKey(KIND_NICK_GRP, calcNickGroupName(nick));
  184.        
  185.         Query query;
  186.         query = new Query(KIND_NICK).addFilter("name", FilterOperator.EQUAL, nick).setAncestor(nickBoxKey);
  187.        
  188.         Entity nickEnt = datastore.prepare(query).asSingleEntity();
  189.         return nickEnt == null;
  190.     }
  191.    
  192.     @Override
  193.     public void registerFile(Key userKey, String filename, long filesize) {
  194.         final Entity file = new Entity(KIND_FILE, userKey);
  195.         file.setProperty("filename", filename);
  196.         file.setProperty("filesize", filesize);
  197.         file.setProperty("filedate", new Date());
  198.        
  199.         datastore.put(file);
  200.     }
  201.  
  202.     @Override
  203.     public List<UploadedFile> getUserFiles(final Key userKey) {
  204.         Query query = null;
  205.        
  206.         query = new Query(KIND_FILE).setAncestor(userKey);
  207.         query.addSort("filedate", SortDirection.DESCENDING);
  208.        
  209.         final PreparedQuery pq = datastore.prepare(query);
  210.         final List<Entity> fileEnts = pq.asList(FetchOptions.Builder.withLimit(1000)); // TODO: pa lapām
  211.        
  212.         final List<UploadedFile> result = new ArrayList<UploadedFile>();
  213.        
  214.         for (Entity ent : fileEnts) {
  215.             result.add(new UploadedFile((String)ent.getProperty("filename"), (Long)ent.getProperty("filesize"), (Date)ent.getProperty("filedate")));
  216.         }
  217.        
  218.         return result;
  219.     }
  220.  
  221.     @Override
  222.     public List<UploadedFile> getAllFiles() {
  223.         Query query = null;
  224.  
  225.         query = new Query(KIND_FILE);
  226.         query.addSort("filedate", SortDirection.DESCENDING);
  227.  
  228.         final PreparedQuery pq = datastore.prepare(query);
  229.         final List<Entity> fileEnts = pq.asList(FetchOptions.Builder.withLimit(1000)); // TODO: pa lapām
  230.  
  231.         final List<UploadedFile> result = new ArrayList<UploadedFile>();
  232.  
  233.         for (Entity ent : fileEnts) {
  234.             final Key uploaderKey = ent.getParent();
  235.  
  236.             Entity uploaderEnt;
  237.             try {
  238.                 uploaderEnt = datastore.get(uploaderKey);
  239.             } catch (EntityNotFoundException e) {
  240.                 uploaderEnt = null;
  241.             }
  242.  
  243.             UploadedFile f = new UploadedFile((String)ent.getProperty("filename"), (Long)ent.getProperty("filesize"), (Date)ent.getProperty("filedate"));
  244.  
  245.             f.setUploader(uploaderEnt == null ? "" : (String) uploaderEnt.getProperty("nick"));
  246.  
  247.             result.add(f);
  248.         }
  249.  
  250.         return result;
  251.     }
  252.  
  253.     @Override
  254.     public void test() {       
  255.     }
  256. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top