Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified AnimalDB.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Typography,
} from '@mui/material';
import { useState } from 'react';
import { DatePicker } from '../library';
import { DatePicker } from '@/components';
import { CloseRounded } from '@mui/icons-material';

export interface AddAnimalModalProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataGrid, GridDeleteIcon } from '@mui/x-data-grid';
import { Animal } from './examples.types';
import { Animal } from './animal.types';
import { IconButton } from '@mui/material';

export interface AnimalListProps {
Expand All @@ -18,11 +18,12 @@ export const AnimalList = ({
loading,
onDelete,
}: AnimalListProps) => {
console.log(animalList);
return (
<DataGrid
loading={loading}
columns={[
{ field: 'animal_id', headerName: 'ID', type: 'number' },
{ field: 'animal_id', headerName: 'ID', type: 'string' },
{ field: 'animal_name', headerName: 'Name', type: 'string' },
{ field: 'animal_type', headerName: 'Type', type: 'string' },
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConfirmationDialog } from '../library';
import { Animal } from './examples.types';
import { ConfirmationDialog } from '@/components';
import { Animal } from './animal.types';
import { useSettingPixel } from '@/hooks';
import { Button } from '@mui/material';

Expand Down Expand Up @@ -28,8 +28,9 @@ export const DeleteAnimalModal = ({
* Functions
*/
const handleSubmitClick = async () => {
runPixel(`DeleteAnimal(${animalToDelete.animal_id})`, () =>
onClose(true),
runPixel(
`DeleteAnimal(${JSON.stringify(animalToDelete.animal_id)})`,
() => onClose(true),
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Animal {
animal_id: number;
animal_id: string; // UUID
animal_name: string;
animal_type: string;
date_of_birth: string; // YYYY-MM-DD
Expand Down
4 changes: 4 additions & 0 deletions client/src/components/examples/database/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './animal.types';
export * from './AddAnimalModal';
export * from './DeleteAnimalModal';
export * from './AnimalList';
5 changes: 1 addition & 4 deletions client/src/components/examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
export * from './AnimalList';
export * from './examples.types';
export * from './AddAnimalModal';
export * from './DeleteAnimalModal';
export * from './database';
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package domain.examples;
package domain.examples.database;

import java.util.Map;
import prerna.date.SemossDate;

public class AnimalData {
private String animalId;
private String animalType;
private String animalName;
private String dateOfBirth;
private SemossDate dateOfBirth;

public AnimalData(String animalId, String animalType, String animalName, String dateOfBirth) {
public AnimalData(String animalId, String animalType, String animalName, SemossDate dateOfBirth) {
this.animalId = animalId;
this.animalType = animalType;
this.animalName = animalName;
Expand All @@ -29,11 +32,11 @@ public void setAnimalId(String animalId) {
this.animalId = animalId;
}

public String getDateOfBirth() {
public SemossDate getDateOfBirth() {
return dateOfBirth;
}

public void setDateOfBirth(String dateOfBirth) {
public void setDateOfBirth(SemossDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}

Expand All @@ -44,4 +47,12 @@ public String getAnimalType() {
public void setAnimalType(String animalType) {
this.animalType = animalType;
}

public Map<String, Object> toMap() {
return Map.of(
"animal_id", animalId,
"animal_type", animalType,
"animal_name", animalName,
"date_of_birth", dateOfBirth);
}
}
24 changes: 0 additions & 24 deletions java/src/reactors/examples/GetAnimalByIdReactor.java

This file was deleted.

32 changes: 0 additions & 32 deletions java/src/reactors/examples/GetAnimalsReactor.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package reactors.examples;
package reactors.examples.database;

import domain.base.ErrorCode;
import domain.base.ProjectException;
import domain.examples.database.AnimalData;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import prerna.date.SemossDate;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import reactors.AbstractProjectReactor;
import util.Constants;
import util.HelperMethods;

// Add an animal to the database!
public class AddAnimalReactor extends AbstractProjectReactor {

public AddAnimalReactor() {
Expand All @@ -30,7 +34,14 @@ protected NounMetadata doExecute() {
ErrorCode.BAD_REQUEST, "Animal name, type, and date of birth cannot be empty");
}

HelperMethods.addAnimal(database, animalName, animalType, dateOfBirth);
AnimalData animalData =
new AnimalData(
UUID.randomUUID().toString(),
animalType,
animalName,
new SemossDate(dateOfBirth, "yyyy-MM-dd"));

HelperMethods.addAnimal(database, animalData);

return new NounMetadata(true, PixelDataType.BOOLEAN);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reactors.examples;
package reactors.examples.database;

import domain.base.ErrorCode;
import domain.base.ProjectException;
Expand All @@ -10,6 +10,7 @@
import util.Constants;
import util.HelperMethods;

// Remove an animal from the database!
public class DeleteAnimalReactor extends AbstractProjectReactor {

public DeleteAnimalReactor() {
Expand Down
38 changes: 38 additions & 0 deletions java/src/reactors/examples/database/GetAnimalByIdReactor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package reactors.examples.database;

import domain.base.ErrorCode;
import domain.base.ProjectException;
import java.util.List;
import java.util.Map;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import reactors.AbstractProjectReactor;
import util.Constants;
import util.HelperMethods;

// Get an animal by its ID!
public class GetAnimalByIdReactor extends AbstractProjectReactor {

public GetAnimalByIdReactor() {
this.keysToGet = new String[] {Constants.ANIMAL_ID};
this.keyRequired = new int[] {1};
}

@Override
protected NounMetadata doExecute() {
String animalId = this.keyValue.get(Constants.ANIMAL_ID);
List<Map<String, Object>> animalData = HelperMethods.getAnimalById(database, animalId);
if (animalData.isEmpty()) {
throw new ProjectException(ErrorCode.NOT_FOUND, "Animal not found");
}

if (animalData.size() > 1) {
throw new ProjectException(
ErrorCode.INTERNAL_SERVER_ERROR, "Multiple animals found with that id");
}

Map<String, Object> animal = animalData.get(0);

return new NounMetadata(animal, PixelDataType.MAP);
}
}
20 changes: 20 additions & 0 deletions java/src/reactors/examples/database/GetAnimalsReactor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package reactors.examples.database;

import java.util.List;
import java.util.Map;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import reactors.AbstractProjectReactor;
import util.HelperMethods;

// Get all the animals in the database!
public class GetAnimalsReactor extends AbstractProjectReactor {

@Override
protected NounMetadata doExecute() {

List<Map<String, Object>> animals = HelperMethods.getAnimals(database);

return new NounMetadata(animals, PixelDataType.VECTOR);
}
}
31 changes: 16 additions & 15 deletions java/src/util/HelperMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import domain.base.ErrorCode;
import domain.base.ProjectException;
import domain.examples.database.AnimalData;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Expand All @@ -23,40 +24,40 @@ public static String getUserId(User user) {

public static List<Map<String, Object>> getAnimals(RDBMSNativeEngine database) {
SelectQueryStruct qs = new SelectQueryStruct();
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_ID", "animalId"));
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_NAME", "animalName"));
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_TYPE", "animalType"));
qs.addSelector(new QueryColumnSelector("ANIMAL__DATE_OF_BIRTH", "dateOfBirth"));
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_ID", "animal_id"));
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_NAME", "animal_name"));
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_TYPE", "animal_type"));
qs.addSelector(new QueryColumnSelector("ANIMAL__DATE_OF_BIRTH", "date_of_birth"));

return QueryExecutionUtility.flushRsToMap(database, qs);
}

public static List<Map<String, Object>> getAnimalById(
RDBMSNativeEngine database, String animalId) {
SelectQueryStruct qs = new SelectQueryStruct();
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_ID", "animalId"));
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_NAME", "animalName"));
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_TYPE", "animalType"));
qs.addSelector(new QueryColumnSelector("ANIMAL__DATE_OF_BIRTH", "dateOfBirth"));
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_ID", "animal_id"));
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_NAME", "animal_name"));
qs.addSelector(new QueryColumnSelector("ANIMAL__ANIMAL_TYPE", "animal_type"));
qs.addSelector(new QueryColumnSelector("ANIMAL__DATE_OF_BIRTH", "date_of_birth"));
qs.addExplicitFilter(SimpleQueryFilter.makeColToValFilter("ANIMAL__ANIMAL_ID", "==", animalId));

return QueryExecutionUtility.flushRsToMap(database, qs);
}

public static void addAnimal(
RDBMSNativeEngine database, String animalName, String animalType, String dateOfBirth) {
public static void addAnimal(RDBMSNativeEngine database, AnimalData animalData) {

Connection con = null;
try {
con = database.getConnection();
try (PreparedStatement ps =
con.prepareStatement(
"INSERT INTO ANIMAL (ANIMAL_NAME, ANIMAL_TYPE, DATE_OF_BIRTH)\n"
+ "VALUES (?, ?, ?);")) {
"INSERT INTO ANIMAL (ANIMAL_ID, ANIMAL_NAME, ANIMAL_TYPE, DATE_OF_BIRTH)\n"
+ "VALUES (?, ?, ?, ?);")) {
int parameterIndex = 1;
ps.setString(parameterIndex++, animalName);
ps.setString(parameterIndex++, animalType);
ps.setString(parameterIndex++, dateOfBirth);
ps.setString(parameterIndex++, animalData.getAnimalId());
ps.setString(parameterIndex++, animalData.getAnimalName());
ps.setString(parameterIndex++, animalData.getAnimalType());
ps.setObject(parameterIndex++, animalData.getDateOfBirth().getFormattedDate());
ps.execute();
} catch (SQLException e) {
throw new ProjectException(ErrorCode.INTERNAL_SERVER_ERROR);
Expand Down