Last active
August 29, 2015 14:02
-
-
Save sizovs/a9cb274cae5b0aa1bb23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fm.ask.dao.user.auth; | |
import com.google.common.collect.ImmutableMap; | |
import com.rubylight.dao.ChunkQueryNotTotalMySQL; | |
import com.rubylight.dao.DAOAppException; | |
import com.rubylight.dao.DAOSysException; | |
import com.rubylight.dao.IChunkQueryDefinition; | |
import com.rubylight.dao.connection.IConnectionLookup; | |
import com.rubylight.entity.IChunkProperties; | |
import com.rubylight.entity.IListChunk; | |
import fm.ask.components.user.UserAuth; | |
import fm.ask.components.user.UserAuthsDTO; | |
import fm.ask.components.user.UserAuthsMinDTO; | |
import fm.ask.components.user.UserEmailDTO; | |
import fm.ask.dao.BaseDAOImpl; | |
import org.apache.commons.lang.StringUtils; | |
import java.util.*; | |
import static com.google.common.base.Objects.firstNonNull; | |
import static com.rubylight.entity.ListChunk.emptyListChunk; | |
import static fm.ask.components.user.UserAuthsDTO.isStateValid; | |
import static fm.ask.util.NamedAttributeExpander.expandable; | |
import static java.util.Collections.emptyList; | |
import static org.apache.commons.collections.CollectionUtils.isEmpty; | |
public class UserAuthsDAOImpl extends BaseDAOImpl<UserAuthsDTO> implements UserAuthsDAO { | |
private static final Map<String, String> orderedColumns = ImmutableMap.of("login", "login"); | |
public UserAuthsDAOImpl(IConnectionLookup connectionLookup) { | |
super(connectionLookup, UserAuthsRowReader.INSTANCE); | |
} | |
@Override | |
public String getEntityTableName() { | |
return "user_auths"; | |
} | |
@Override | |
public String getPrimaryKeyFieldName() { | |
return "id"; | |
} | |
@Override | |
public Long create(UserAuthsDTO value) throws DAOAppException, DAOSysException { | |
Date now = now(); | |
return insertAndGetGeneratedPk("INSERT INTO user_auths (login,email,state,crypted_password,salt,facebook_uid,twitter_uid,vkontakte_uid,updated_at,created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", | |
value.getLogin(), | |
value.getEmail(), | |
value.getState(), | |
value.getCryptedPassword(), | |
value.getSalt(), | |
value.getFacebookUID(), | |
value.getTwitterUID(), | |
value.getVKontakteUID(), | |
now, | |
now); | |
} | |
@Override | |
public void store(UserAuthsDTO value) throws DAOAppException, DAOSysException { | |
update("UPDATE `user_auths` SET `updated_at`=?,`email`=?,`state`=?,`crypted_password`=?,`salt`=?,`facebook_uid`=?,`twitter_uid`=?,`vkontakte_uid`=? WHERE `id`=?", | |
now(), | |
value.getEmail(), | |
value.getState(), | |
value.getCryptedPassword(), | |
value.getSalt(), | |
value.getFacebookUID(), | |
value.getTwitterUID(), | |
value.getVKontakteUID(), | |
value.getId()); | |
} | |
@Override | |
public void storeUserAuthsMin(UserAuthsMinDTO value) throws DAOSysException { | |
update("UPDATE `user_auths` SET `updated_at`=?,`email`=?,`state`=?,`facebook_uid`=?,`twitter_uid`=?,`vkontakte_uid`=? WHERE `id`=?", | |
now(), | |
value.getEmail(), | |
value.getState(), | |
value.getFacebookUID(), | |
value.getTwitterUID(), | |
value.getVKontakteUID(), | |
value.getId()); | |
} | |
@Override | |
public void storeUserAuthsMin(UserAuth value) throws DAOSysException { | |
update("UPDATE `user_auths` SET `updated_at`=?,`email`=?,`crypted_password`=?,`salt`=? WHERE `id`" + "=?", | |
now(), | |
value.getEmail(), | |
value.getCryptedPassword(), | |
value.getSalt(), | |
value.getId()); | |
} | |
@Override | |
public UserAuth getUserAuthById(Long userId) { | |
if (userId == null) { | |
return null; | |
} | |
return selectRow("select `id`,`login`,`email`,`crypted_password`,`salt` from `user_auths` where `id`=?", UserAuthRowReader.INSTANCE, userId); | |
} | |
@Override | |
public List<UserAuthsMinDTO> getUserAuthsMinByIds(Collection<Long> userIds, String state) { | |
if (isEmpty(userIds)) { | |
return emptyList(); | |
} | |
String sql; | |
if (state != null && isStateValid(state)) { | |
sql = expandable("select `id`,`login`,`email`,`state`,`facebook_uid`,`twitter_uid`,`vkontakte_uid`,`crypted_password` is not null from `user_auths` where `id` in(:ids) and `state` = ':state'") | |
.expandAsLongs("ids", userIds) | |
.expand("state", state) | |
.toString(); | |
} else { | |
sql = expandable("select `id`,`login`,`email`,`state`,`facebook_uid`,`twitter_uid`,`vkontakte_uid`,`crypted_password` is not null from `user_auths` where `id` in(:ids)") | |
.expandAsLongs("ids", userIds) | |
.toString(); | |
} | |
return firstNonNull(selectRows(sql, UserAuthsMinRowReader.INSTANCE), Collections.<UserAuthsMinDTO>emptyList()); | |
} | |
@Override | |
public IListChunk<UserAuthsMinDTO> getActiveUserAuthsMinByIds(Collection<Long> userIds, IChunkProperties props) { | |
if (isEmpty(userIds)) { | |
return emptyListChunk(); | |
} | |
String query = expandable("select `id`,`login`,`email`,`state`,`facebook_uid`,`twitter_uid`,`vkontakte_uid`,`crypted_password` is not null from `user_auths` where `state`='active' and `id` in (:ids)") | |
.expandAsLongs("ids", userIds) | |
.toString(); | |
IChunkQueryDefinition queryDef; | |
if (props.getOrderBy() == null) { | |
queryDef = new ChunkQueryNotTotalMySQL(query); | |
} else { | |
queryDef = new ChunkQueryNotTotalMySQL(query, "id", false); | |
} | |
return selectChunkRows(queryDef, UserAuthsMinRowReader.INSTANCE, orderedColumns, props); | |
} | |
@Override | |
public UserAuth getUserAuthByLogin(String login) { | |
return selectRow("select `id`,`login`,`email`,`crypted_password`,`salt` from `user_auths` where `login`=? limit 1", UserAuthRowReader.INSTANCE, login); | |
} | |
@Override | |
public UserAuthsMinDTO getUserAuthsMinByLogin(String login) { | |
if (StringUtils.isEmpty(login)) { | |
return null; | |
} | |
return selectRow("select `id`,`login`,`email`,`state`,`facebook_uid`,`twitter_uid`,`vkontakte_uid`,`crypted_password` is not null from `user_auths` where `login`=? limit 1", UserAuthsMinRowReader.INSTANCE, login); | |
} | |
@Override | |
public UserEmailDTO getUserIdEmailStateBy(String login) { | |
if (StringUtils.isEmpty(login)) { | |
return null; | |
} | |
UserEmailDTO result = selectRow("select `id`,`email`,`state` from `user_auths` where `login`=? limit 1", UserEmailRowReader.INSTANCE, login); | |
if (result != null) { | |
result.setLogin(login); | |
} | |
return result; | |
} | |
@Override | |
public UserAuthsMinDTO getMinByFacebookId(Long facebookUId) { | |
return selectRow("select `id`,`login`,`email`,`state`,`facebook_uid`,`twitter_uid`,`vkontakte_uid`,`crypted_password` is not null from `user_auths` where `facebook_uid`=? limit 1", UserAuthsMinRowReader.INSTANCE, facebookUId); | |
} | |
@Override | |
public UserAuthsMinDTO getMinByTwitterId(Long twitterUId) { | |
return selectRow("select `id`,`login`,`email`,`state`,`facebook_uid`,`twitter_uid`,`vkontakte_uid`,`crypted_password` is not null from `user_auths` where `twitter_uid`=? limit 1", UserAuthsMinRowReader.INSTANCE, twitterUId); | |
} | |
@Override | |
public UserAuthsMinDTO getMinByVkontakteId(Long vkontakteUId) { | |
return selectRow("select `id`,`login`,`email`,`state`,`facebook_uid`,`twitter_uid`,`vkontakte_uid`,`crypted_password` is not null from `user_auths` where `vkontakte_uid`=? limit 1", UserAuthsMinRowReader.INSTANCE, vkontakteUId); | |
} | |
@Override | |
public UserAuthsMinDTO getUserAuthsMinById(Long userId) { | |
if (userId == null) { | |
return null; | |
} | |
return selectRow("select `id`,`login`,`email`,`state`,`facebook_uid`,`twitter_uid`,`vkontakte_uid`,`crypted_password` is not null from `user_auths` where `id`=?", UserAuthsMinRowReader.INSTANCE, userId); | |
} | |
@Override | |
public List<UserAuthsMinDTO> getUserAuthsMinByLogins(List<String> logins) { | |
if (isEmpty(logins)) { | |
return emptyList(); | |
} | |
String sql = expandable("select `id`,`login`,`email`,`state`,`facebook_uid`,`twitter_uid`,`vkontakte_uid`,`crypted_password` is not null from `user_auths` where `login` in (:logins) limit :count") | |
.expandAsStrings("logins", logins) | |
.expand("count", logins.size()) | |
.toString(); | |
return firstNonNull(selectRows(sql, UserAuthsMinRowReader.INSTANCE), Collections.<UserAuthsMinDTO>emptyList()); | |
} | |
@Override | |
public UserAuth getUserAuthByEmail(String email) { | |
if (StringUtils.isEmpty(email)) { | |
return null; | |
} | |
return selectRow("select `id`,`login`,`email`,`state`,`crypted_password`,`salt`,`facebook_uid`,`twitter_uid`,`vkontakte_uid` from `user_auths` where `email`=? limit 1", UserAuthRowReader.INSTANCE, email); | |
} | |
@Override | |
public UserAuthsMinDTO getUserAuthMinByEmail(String email) { | |
return selectRow("select `id`,`login`,`email`,`state`,`facebook_uid`,`twitter_uid`,`vkontakte_uid`,`crypted_password` is not null from `user_auths` where `email`=? limit 1", UserAuthsMinRowReader.INSTANCE, email); | |
} | |
@Override | |
public List<Long> getUserIdsByEmails(Collection<String> emails) { | |
String sql = expandable("select `id` from `user_auths` where `email` in (:emails) limit :count") | |
.expandAsStrings("emails", emails) | |
.expand("count", emails.size()) | |
.toString(); | |
return firstNonNull(selectRows(sql, getPrimaryKeyReader()), Collections.<Long>emptyList()); | |
} | |
@Override | |
public List<Long> getUserIdsByTwitterUIDs(Collection<Long> twitterUIDs) { | |
if (twitterUIDs.isEmpty()) { | |
return emptyList(); | |
} | |
String query = expandable("select `id` from `user_auths` where `twitter_uid` in (:twitterIds) limit :count") | |
.expandAsLongs("twitterIds", twitterUIDs) | |
.expand("count", twitterUIDs.size()) | |
.toString(); | |
return firstNonNull(selectRows(query, getPrimaryKeyReader()), Collections.<Long>emptyList()); | |
} | |
@Override | |
public List<Long> getUserIdsByFacebookUIDs(Collection<Long> facebookUIDs) { | |
if (facebookUIDs.isEmpty()) { | |
return emptyList(); | |
} | |
String query = expandable("select `id` from `user_auths` where `facebook_uid` in (:facebookIds) limit :count") | |
.expandAsLongs("facebookIds", facebookUIDs) | |
.expand("count", facebookUIDs.size()) | |
.toString(); | |
return firstNonNull(selectRows(query, getPrimaryKeyReader()), Collections.<Long>emptyList()); | |
} | |
@Override | |
public List<Long> getUserIdsByVKontakteUIDs(Collection<Long> vkontakteUIDs) { | |
if (vkontakteUIDs.isEmpty()) { | |
return emptyList(); | |
} | |
String query = expandable("select `id` from `user_auths` where `vkontakte_uid` in (:vkIds) limit :count") | |
.expandAsLongs("vkIds", vkontakteUIDs) | |
.expand("count", vkontakteUIDs.size()) | |
.toString(); | |
return firstNonNull(selectRows(query, getPrimaryKeyReader()), Collections.<Long>emptyList()); | |
} | |
@Override | |
public boolean isUserHashPassword(Long userId) { | |
Long result = selectRow("select `id` from `user_auths` where `id`=? and `crypted_password` is not null", getPrimaryKeyReader(), userId); | |
return result != null && result > 0; | |
} | |
@Override | |
public void updateStateById(Long userId, String state) { | |
update("UPDATE `user_auths` SET `updated_at`=?,`state`=? WHERE `id`=?", now(), state, userId); | |
} | |
@Override | |
public boolean updatePassword(UserAuth auth, byte[] oldCryptedPassword) { | |
String sql; | |
if (oldCryptedPassword == null) { | |
sql = "UPDATE `user_auths` SET `updated_at`=?,`crypted_password`=?,`salt`=? WHERE `id`=? AND `crypted_password` IS NULL"; | |
return update(sql, now(), auth.getCryptedPassword(), auth.getSalt(), auth.getId()) > 0; | |
} else { | |
sql = "UPDATE `user_auths` SET `updated_at`=?,`crypted_password`=?,`salt`=? WHERE `id`=? AND `crypted_password`=?"; | |
return update(sql, now(), auth.getCryptedPassword(), auth.getSalt(), auth.getId(), oldCryptedPassword) > 0; | |
} | |
} | |
@Override | |
public void updateEmail(Long userId, String newEmail) { | |
update("UPDATE `user_auths` SET `updated_at`=?,`email`=? WHERE `id`=?", now(), newEmail, userId); | |
} | |
@Override | |
public int updateFacebookUID(Long userId, Long facebookUId) { | |
return update("UPDATE `user_auths` SET `updated_at`=?,`facebook_uid`=? WHERE `id`=?", now(), facebookUId, userId); | |
} | |
@Override | |
public int updateTwitterUID(long userId, Long twitterUID) { | |
return update("UPDATE `user_auths` SET `updated_at`=?,`twitter_uid`=? WHERE `id`=?", now(), twitterUID, userId); | |
} | |
@Override | |
public int updateVKontakteUID(long userId, Long vkontakteUID) { | |
return update("UPDATE `user_auths` SET `updated_at`=?,`vkontakte_uid`=? WHERE `id`=?", now(), vkontakteUID, userId); | |
} | |
@Override | |
public int resetFacebookUID(Long userId) { | |
return update("UPDATE `user_auths` SET `updated_at`=?,`facebook_uid`=NULL WHERE `id`=? AND (`vkontakte_uid` IS NOT NULL OR `twitter_uid` IS NOT NULL OR `crypted_password` IS NOT NULL)", now(), userId); | |
} | |
@Override | |
public int resetTwitterUID(long userId) { | |
return update("UPDATE `user_auths` SET `updated_at`=?,`twitter_uid`=NULL WHERE `id`=? AND (`facebook_uid` IS NOT NULL OR `vkontakte_uid` IS NOT NULL OR `crypted_password` IS NOT NULL)", now(), userId); | |
} | |
@Override | |
public int resetVKontakteUID(long userId) { | |
return update("UPDATE `user_auths` SET `updated_at`=?,`vkontakte_uid`=NULL WHERE `id`=? AND (`facebook_uid` IS NOT NULL OR `twitter_uid` IS NOT NULL OR `crypted_password` IS NOT NULL)", now(), userId); | |
} | |
private Date now() { | |
return new Date(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment