Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
monsterwhat committed Aug 15, 2023
1 parent 445ef25 commit 7de1257
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 25 deletions.
27 changes: 25 additions & 2 deletions src/main/java/Controllers/ProfilesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jakarta.inject.Named;
import java.io.Serializable;
import java.util.List;
import java.util.Optional;
/**
*
* @author Al
Expand All @@ -23,6 +24,7 @@ public class ProfilesController implements Serializable{
private Profiles selectedProfile;
private Profiles newProfile;
private String selectedOption = "view";
private String generatorOption;

public ProfilesController() {
}
Expand Down Expand Up @@ -87,17 +89,29 @@ public void clearSelectedProfile(){

public void generateAndCreateRandomUsers() {
FakeUserGenerator userGenerator = new FakeUserGenerator();
// Use the selectedGenerator value to determine the generator strategy
Optional<String> generatorInput = Optional.ofNullable(generatorOption);
if (generatorInput.isPresent()) {
userGenerator.setUsernameGenerator(generatorInput.get());
}

for (int i = 0; i < 10; i++) {
Profiles newUser = userGenerator.generateFakeUserProfile();
Profiles newUser = userGenerator.generateFakeUserProfile("user");
profileService.create(newUser);
}
clearSelectedProfile();
}

public void generateAndCreateRandomAdmins() {
FakeUserGenerator userGenerator = new FakeUserGenerator();

Optional<String> generatorInput = Optional.ofNullable(generatorOption);
if (generatorInput.isPresent()) {
userGenerator.setUsernameGenerator(generatorInput.get());
}

for (int i = 0; i < 10; i++) {
Profiles newUser = userGenerator.generateFakeAdminProfile();
Profiles newUser = userGenerator.generateFakeUserProfile("admin");
profileService.create(newUser);
}
clearSelectedProfile();
Expand Down Expand Up @@ -134,6 +148,15 @@ public String getSelectedOption() {
public void setSelectedOption(String selectedOption) {
this.selectedOption = selectedOption;
}

public String getGeneratorOption() {
return generatorOption;
}

public void setGeneratorOption(String generatorOption) {
this.generatorOption = generatorOption;
}



}
4 changes: 2 additions & 2 deletions src/main/java/Services/GService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void delete(T entity) {
System.out.println("Entity not found");
}
} catch (Exception e) {
System.out.println("Error: " + e.toString());
System.out.println("Error deleting "+ getEntityClass().getSimpleName() +" : " + e.toString());
}
}

Expand All @@ -63,7 +63,7 @@ public Long count() {
TypedQuery<Long> query = em.createQuery("SELECT COUNT(e) FROM " + getEntityClass().getSimpleName() + " e", Long.class);
return query.getSingleResult();
} catch (Exception e) {
System.out.println("Error: " + e.getLocalizedMessage());
System.out.println("Error counting "+ getEntityClass().getSimpleName() +" : " + e.getLocalizedMessage());
return null;
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/Services/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,22 @@ public void create(Profiles entity) {
}
}

@Override
public void delete(Profiles entity) {
try {
if (!em.contains(entity)) {
entity = em.find(getEntityClass(), entity.getId());
}

if (entity != null) {
em.remove(entity);
} else {
System.out.println("Entity not found");
}
} catch (Exception e) {
System.out.println("Error deleting "+ getEntityClass().getSimpleName() +" : " + e.toString());
}
}


}
35 changes: 19 additions & 16 deletions src/main/java/Utils/FakeUserGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,43 @@
import com.github.javafaker.Faker;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;

public class FakeUserGenerator {
private final Faker faker;
private final Set<String> generatedUsernames;
private static Set<String> generatedUsernames = new HashSet<>();
private Supplier<String> usernameGenerator;

public FakeUserGenerator() {
faker = new Faker();
generatedUsernames = new HashSet<>();
setUsernameGenerator("dragonBall");
}

public void setUsernameGenerator(String input) {
switch (input) {
case "dragonBall" -> usernameGenerator = () -> faker.dragonBall().character();
case "harryPotter" -> usernameGenerator = () -> faker.harryPotter().character();
case "rickandMorty" -> usernameGenerator = () -> faker.rickAndMorty().character();

default -> usernameGenerator = () -> faker.hobbit().character();
}
}

private String generateUniqueUsername() {
String username;
do {
username = faker.dragonBall().character();
username = usernameGenerator.get();
} while (generatedUsernames.contains(username));

generatedUsernames.add(username);
return username;
}

public Profiles generateFakeUserProfile() {
Profiles profile = new Profiles();
profile.setUsername(generateUniqueUsername());
profile.setPassword(faker.internet().password());
profile.setGroupName("user");
return profile;
}

public Profiles generateFakeAdminProfile() {
public Profiles generateFakeUserProfile(String groupName) {
Profiles profile = new Profiles();
profile.setUsername(generateUniqueUsername());
profile.setPassword(faker.internet().password());
profile.setGroupName("admin");
profile.setGroupName(groupName);
return profile;
}
}

}
29 changes: 24 additions & 5 deletions src/main/webapp/secured/fragments/ProfilesTable.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
<div class="column is-one-quarter">
<!-- Left Tile -->
<div class="box">
<h:form>
<div class="columns">
<div class="column">
<p:outputLabel value="Select Username Generator:"/>
<p:selectOneMenu value="#{ProfilesController.generatorOption}" style="width: 100%;">
<f:selectItem itemLabel="Dragon Ball" itemValue="dragonBall"/>
<f:selectItem itemLabel="Harry Potter" itemValue="harryPotter"/>
<f:selectItem itemLabel="Rick and Morty" itemValue="rickandMorty"/>
<!-- Add more options as needed -->
</p:selectOneMenu>
</div>
</div>
</h:form>

<p:commandButton value="Fill Datatable" actionListener="#{ProfilesController.generateAndCreateRandomUsers()}" styleClass="button is-fullwidth is-success" update="dynamicContent"/>
<br/>
<p:commandButton value="Create User" actionListener="#{ProfilesController.selectCreate()}" update="dynamicContent" styleClass="button is-fullwidth is-info"/>
Expand All @@ -29,8 +43,13 @@
<div class="tile is-parent is-vertical">
<div class="tile is-child">
<h:form id="profilesTable">
<p:dataTable value="#{ProfilesController.profilesList()}" var="profile">
<p:dataTable value="#{ProfilesController.profilesList()}" var="profile"
paginator="true" rows="8" paginatorPosition="bottom"
selection="#{ProfilesController.selectedProfile}" rowKey="#{profile.id}">
<!-- Define your columns here -->
<p:column headerText="profileId" visible="false">
<h:outputText value="#{profile.id}"/>
</p:column>
<p:column headerText="Username">
<h:outputText value="#{profile.username}" />
</p:column>
Expand All @@ -42,12 +61,12 @@
</p:column>
<p:column headerText="Actions" width="13%">
<div class="buttons has-addons">
<p:commandButton icon="pi pi-pencil" actionListener="#{ProfilesController.selectEdit()}"
styleClass="button edit-button rounded-button ui-button-success" process="@this" update="dynamicContent">
<p:commandButton styleClass="button edit-button rounded-button ui-button-success" icon="pi pi-pencil"
actionListener="#{ProfilesController.selectEdit()}" process="@this" update="dynamicContent">
<f:setPropertyActionListener value="#{profile}" target="#{ProfilesController.selectedProfile}" />
</p:commandButton>
<p:commandButton class="button ui-button-warning rounded-button" icon="pi pi-trash"
process="@this" actionListener="#{ProfilesController.deleteProfile()}" update="dynamicContent">
<p:commandButton styleClass="button ui-button-warning rounded-button" icon="pi pi-trash"
actionListener="#{ProfilesController.deleteProfile()}" process="@this" update="dynamicContent">
<f:setPropertyActionListener value="#{profile}" target="#{ProfilesController.selectedProfile}" />
</p:commandButton>
</div>
Expand Down

0 comments on commit 7de1257

Please sign in to comment.