Last active
December 27, 2015 15:09
-
-
Save oscarguindzberg/7345332 to your computer and use it in GitHub Desktop.
Samples for Arc90
This file contains 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 my.kalos.service; | |
import java.util.Collection; | |
import my.kalos.model.AutocompleteCampo; | |
import my.kalos.model.Obra; | |
public interface AutocompleteService { | |
/** | |
* Lista las opciones para un determinado texto | |
* @param texto ej "Mar" | |
* @param autocompleteCampo ej LIBRO_AUTOR | |
* @return Una colleccion de Strings. Ej: ["Marcelo Birmajer", "Gabriel Garcia Marquez"] | |
*/ | |
public Collection<String> listarAutocompletes(String texto, AutocompleteCampo autocompleteCampo); | |
/** | |
* Agrega como opciones de autocomplete (si es que no existen) | |
* @param commaSeparatedValues ej "tag1, tag2, tag3," | |
* @param autocompleteCampo ej OPERA_ORQUESTA | |
* @param obraCreadora la obra que está generando que se grabe este autocomplete | |
*/ | |
public void agregarAutocompletes(String commaSeparatedValues, AutocompleteCampo autocompleteCampo, | |
Obra obraCreadora); | |
} | |
This file contains 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 my.kalos.service; | |
import java.util.Collection; | |
import java.util.List; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import javax.persistence.Query; | |
import my.kalos.model.Autocomplete; | |
import my.kalos.model.AutocompleteCampo; | |
import my.kalos.model.Obra; | |
import my.kalos.util.StringUtils; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Transactional; | |
@Service | |
public class AutocompleteServiceImpl implements AutocompleteService { | |
@PersistenceContext | |
EntityManager em; | |
@Transactional | |
public Collection<String> listarAutocompletes(String texto, AutocompleteCampo autocompleteCampo) { | |
Query q = em.createQuery(" SELECT aut.label FROM Autocomplete aut" + | |
" WHERE unaccent_string(lower(aut.label)) like unaccent_string(lower('%' || :texto || '%')) " + | |
" and :autocompleteCampo = aut.autocompleteCampo"); | |
q.setParameter("texto", texto); | |
q.setParameter("autocompleteCampo", autocompleteCampo); | |
return q.getResultList(); | |
} | |
@Transactional | |
public void agregarAutocompletes(String commaSeparatedValues, AutocompleteCampo autocompleteCampo, Obra obraCreadora) { | |
if (commaSeparatedValues==null || commaSeparatedValues.isEmpty() ) return; | |
String[] values = StringUtils.commaSeparatedToStringArray(commaSeparatedValues); | |
for (int i = 0; i < values.length; i++) { | |
Query q = em.createQuery(" SELECT aut FROM Autocomplete aut " + | |
" WHERE lower(aut.label) = lower(:texto)" + | |
" and :autocompleteCampo = aut.autocompleteCampo"); | |
q.setParameter("texto", values[i]); | |
q.setParameter("autocompleteCampo", autocompleteCampo); | |
List<Autocomplete> list = q.getResultList(); | |
if (list.isEmpty()){ | |
Autocomplete autocomplete = new Autocomplete(); | |
autocomplete.setLabel(values[i]); | |
autocomplete.setAutocompleteCampo(autocompleteCampo); | |
autocomplete.setObraCreadoraId(obraCreadora.getId()); | |
em.persist(autocomplete); | |
} | |
} | |
} | |
} |
This file contains 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 my.kalos.util; | |
import java.util.Comparator; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import java.util.Set; | |
import java.util.SortedSet; | |
import java.util.TreeMap; | |
import java.util.TreeSet; | |
/** | |
* Map implementation useful to keep a counter on each key. | |
*/ | |
public class CounterMap<K extends Comparable<K>> extends TreeMap<K, Long> { | |
public Long getCounter(K key) { | |
if (!containsKey(key)) { | |
return 0l; | |
} else { | |
return get(key); | |
} | |
} | |
public void add(K key, Long value) { | |
put(key, getCounter(key) + value); | |
} | |
public void increment(K key) { | |
add(key, 1l); | |
} | |
} |
This file contains 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 my.kalos.social; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import my.kalos.controller.UsuarioLogineadoProvider; | |
import my.kalos.model.Usuario; | |
import my.kalos.model.UsuarioRole; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.security.web.savedrequest.RequestCache; | |
import org.springframework.security.web.savedrequest.SavedRequest; | |
import org.springframework.social.connect.ConnectionFactoryLocator; | |
import org.springframework.social.connect.ConnectionRepository; | |
import org.springframework.social.connect.web.ConnectController; | |
import org.springframework.web.context.request.NativeWebRequest; | |
import org.springframework.web.servlet.view.RedirectView; | |
public class KalosConnectController extends ConnectController { | |
@Autowired | |
private UsuarioLogineadoProvider usuarioLogineadoProvider; | |
@Autowired | |
private RequestCache requestCache; | |
public KalosConnectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) { | |
super(connectionFactoryLocator, connectionRepository); | |
} | |
protected RedirectView connectionStatusRedirect(String providerId, NativeWebRequest request) { | |
HttpServletRequest httpServletRequest = (HttpServletRequest)request.getNativeRequest(); | |
HttpServletResponse httpServletResponse = (HttpServletResponse)request.getNativeResponse(); | |
SavedRequest savedRequest = requestCache.getRequest(httpServletRequest, httpServletResponse); | |
String targetUrl; | |
if (savedRequest == null) { | |
Usuario usuario = usuarioLogineadoProvider.getUsuarioLogineado(); | |
if (usuario.getRole().equals(UsuarioRole.ROLE_ESPECTADOR)) { | |
targetUrl = "/perfil/mostrarFormularioConfiguracion"; | |
} else { | |
targetUrl = "/admin/configuracion-productora/"; | |
} | |
} else { | |
targetUrl = savedRequest.getRedirectUrl(); | |
} | |
return new RedirectView(targetUrl, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment