marioosh.net

mini blog / notes

update column with random string

DECLARE @v TABLE (x VARCHAR(50)) INSERT INTO @v VALUES — list of strings that column will be updated with (’37845840’),(’34879335’),(’33879336’),(’37855840’);   DECLARE cursor1 CURSOR FOR SELECT <TABLE_ID> FROM <SOME_TABLE>; DECLARE @id INT;   OPEN cursor1 FETCH NEXT FROM cursor1 INTO @id   WHILE @@FETCH_STATUS=0 BEGIN UPDATE <SOME_TABLE> SET <UPDATED_COLUMN> = ( SELECT x FROM ( Read more about update column with random string[…]

Native query with Enum as parameter

public interface ItemRepository extends JpaRepository<Item, Long> { @Query(value = "select * from items where type = :#{#type?.name()}", nativeQuery = true) List<Item> findByType(@Param("type") ItemType type); }   public enum ItemType { NORMAL, LARGE };public interface ItemRepository extends JpaRepository<Item, Long> { @Query(value = "select * from items where type = :#{#type?.name()}", nativeQuery = true) List<Item> findByType(@Param("type") ItemType Read more about Native query with Enum as parameter[…]

Optional @Value

// default: 60 @Value("${test-value:60}") private int minutes;   // default: null @Value("${test-value2:#{null}}") private String text;// default: 60 @Value("${test-value:60}") private int minutes; // default: null @Value("${test-value2:#{null}}") private String text;

Map<String,String> in JPA Entity

@Entity @Table(name="product") public class Product extends AbstractEntity {   @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id;   @ElementCollection(fetch = FetchType.EAGER) @JoinTable(name="product_attribute", joinColumns=@JoinColumn(name="product_id")) @MapKeyColumn(name="key") @Column(name="value") private Map<String, String> attributes = new HashMap<String, String>();   // …   }@Entity @Table(name="product") public class Product extends AbstractEntity { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @ElementCollection(fetch = FetchType.EAGER) @JoinTable(name="product_attribute", joinColumns=@JoinColumn(name="product_id")) @MapKeyColumn(name="key") @Column(name="value") private Read more about Map<String,String> in JPA Entity[…]

Spring Data JPA: LIKE with OR query

@RepositoryRestResource public interface ProductRepository extends JpaRepository<Product, String> { List<Product> findByTitleContainingIgnoreCaseOrIdContainingIgnoreCase( @Param("q") String name, @Param("q") String id); }@RepositoryRestResource public interface ProductRepository extends JpaRepository<Product, String> { List<Product> findByTitleContainingIgnoreCaseOrIdContainingIgnoreCase( @Param("q") String name, @Param("q") String id); } Request path: GET https://localhost:8443/api/products/search/findByTitleContainingIgnoreCaseOrIdContainingIgnoreCase?q=…GET https://localhost:8443/api/products/search/findByTitleContainingIgnoreCaseOrIdContainingIgnoreCase?q=… If we need shorter request path: // GET https://localhost:8443/api/products/search/custom?q=… @RestResource(path = "custom") List<Product> findByTitleContainingIgnoreCaseOrIdContainingIgnoreCase( @Param("q") String name, Read more about Spring Data JPA: LIKE with OR query[…]

Scroll two divs simultaneously

On example of diff2html result let sides = this.diffResult.nativeElement.querySelectorAll(’.d2h-file-side-diff’); sides.forEach((sideDiv,idx) => { sideDiv.addEventListener(’scroll’, (event) => { let scrollLeft = event.target.scrollLeft; sides[idx==0?1:0].scrollLeft = scrollLeft; }); });let sides = this.diffResult.nativeElement.querySelectorAll(‘.d2h-file-side-diff’); sides.forEach((sideDiv,idx) => { sideDiv.addEventListener(‘scroll’, (event) => { let scrollLeft = event.target.scrollLeft; sides[idx==0?1:0].scrollLeft = scrollLeft; }); });

Active Directory/LDAP authentication with Spring Boot

package net.marioosh.ldapdemo;   import java.util.Arrays; import java.util.Collection;   import org.springframework.context.annotation.Configuration; import org.springframework.ldap.core.DirContextOperations; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;   @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {   @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest() .fullyAuthenticated() .and().formLogin(); }   @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { Read more about Active Directory/LDAP authentication with Spring Boot[…]

XML – check is well formed

public boolean isValid(String xmlFilePath) throws FileNotFoundException { FileInputStream in = new FileInputStream(xmlFilePath); try { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader staxXmlReader = factory.createXMLStreamReader(in); while(staxXmlReader.next() != XMLStreamConstants.END_DOCUMENT) {} return true; } catch (XMLStreamException e) { e.printStackTrace(); return false; } }public boolean isValid(String xmlFilePath) throws FileNotFoundException { FileInputStream in = new FileInputStream(xmlFilePath); try { XMLInputFactory factory = XMLInputFactory.newInstance(); Read more about XML – check is well formed[…]