To access the filtered results of the pipe just define it as a variable via the as keyword

*ngFor="let bookmark of bookmarks | bookmarkFilter: filterText as filteredBookmarks"

In the full example the bookmarkFilter pipe filters the list of bookmarks based on some text search. Further I need to check whether the filtered results contain only one element [showMoreText] = "filteredBookmarks.length === 1 ", to then display the full content of the bookmark:

<div class="mt-3" *ngFor="let bookmark of bookmarks | bookmarkFilter: filterText as filteredBookmarks">
    <app-bookmark-list-element
        [showMoreText] = "filteredBookmarks.length === 1 || bookmarks.length === 1"
        [bookmark]="bookmark"
        [userData$]="userData$"
        [queryText]="queryText"
        [filterText]="filterText"
        [isSearchResultsPage]="isSearchResultsPage"
    >
    </app-bookmark-list-element>
</div>

See it in action at www.codever.dev:

Copy-to-clipboard-demo


The source code for Codever is available on Github


Shared with from Codever. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Codever is open source on Github ⭐🙏

Use the click event to pass the text to the handling function, in this case copyToClipboard(bookmark.location)

<span class="btn-light btn-sm" (click)="copyToClipboard(bookmark.location)" title="Copy link to clipboard">
  <i class="far fa-copy copy-link"></i><span class="copy-btn-text"></span>
</span>

To programmatically copy a string use the Clipboard which service copies text to the user’s clipboard. We use a setTimeout to visually inform the user for a brief moment that the copy was successful

import { Clipboard } from '@angular/cdk/clipboard';

export class BookmarkListElementComponent {

copyLinkButtonText = '';

  constructor(private router: Router,
              private clipboard: Clipboard) {}

  copyToClipboard(location: string) {
    const copied = this.clipboard.copy(location);
    if (copied) {
      this.copyLinkButtonText = ' Copied';
      setTimeout(() => this.copyLinkButtonText = '', 1300);
    }

  }
}

See it in action at www.codever.dev:

Copy-to-clipboard-demo

See the reference link (official docs) how to use it for longer texts.

Reference - https://material.angular.io/cdk/clipboard/overview


Shared with from Codever. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Codever is open source on Github ⭐🙏

Use DefaultValue annotation parameter (accepts strings) alongside the QueryParam annotation

@GET
@Path("/bookmarks")
@Produces(MediaType.APPLICATION_JSON)
@Operation(
    summary = "Return bookmarks from repository",
    description = "Return bookmarks from repository")
@ApiResponses({
    @ApiResponse(responseCode = "200", description = "OK"),
    @ApiResponse(responseCode = "403", description = "Forbidden")
})
@RolesAllowed(ADMIN_ROLE)
public void getAllBookmarks(
    @Parameter(description = "max number of returned bookmarks")
    @DefaultValue(Integer.MAX_VALUE + "")
    @QueryParam("maxResult") Integer maxResult) {
  bookmarksService.getBookmarks(maxResult);
}

Shared with from Codever. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Codever is open source on Github ⭐🙏

Given the following Superhero class:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import lombok.Getter;
import lombok.Setter;

@XmlRootElement(name = "super-hero")
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
class SuperHero {
  @XmlElement(name = "name")
  String name;

  @XmlElement(name = "super-power")
  String superPower;
}

You can convert an XML String to a SuperHero instance with the following code:

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
class TestXmlStringToObjectUnmarshalling {

  static final String superHeroXml =
      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
          + "<super-hero>"
          + "  <name>Superman</name>"
          + "  <super-power>Flight</super-power>"
          + "</super-hero>";

  @Test
  void testXmlUnmarshalling() throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(SuperHero.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    StringReader reader = new StringReader(superHeroXml);
    SuperHero superHero = (SuperHero) unmarshaller.unmarshal(reader);

    assertEquals("Flight", superHero.getSuperPower());
  }
}

Note:

  • create a JAXBContext which includes the SuperHero class - JAXBContext.newInstance(SuperHero.class)
  • create a JAXB Unmarshaller and apply the unmarshal method to a StringReader wrapping the text (it could be also a FileReader or any other Reader for that matter)

Shared with from Codever. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Codever is open source on Github ⭐🙏

Use the executeUpdate method of the Query interface. It will return the number of deleted entries:

public int deleteOldMessages(int daysBack) {
  var query = em.createQuery("delete from Message m where createdAt < :givenTimestamp");
  query.setParameter(Message.GIVEN_TIMESTAMP, LocalDateTime.now().minusDays(daysBack));

  return query.executeUpdate();
}

Shared with from Codever. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Codever is open source on Github ⭐🙏