-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39dc622
commit bb80e35
Showing
10 changed files
with
443 additions
and
16 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
src/main/java/org/openlmis/referencedata/domain/PriceChange.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* This program is part of the OpenLMIS logistics management information system platform software. | ||
* Copyright © 2017 VillageReach | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Affero General Public License as published by the Free Software Foundation, either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Affero General Public License for more details. You should have received a copy of | ||
* the GNU Affero General Public License along with this program. If not, see | ||
* http://www.gnu.org/licenses. For additional information contact [email protected]. | ||
*/ | ||
|
||
package org.openlmis.referencedata.domain; | ||
|
||
import java.time.ZonedDateTime; | ||
import javax.persistence.CascadeType; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.hibernate.annotations.Type; | ||
import org.joda.money.Money; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@Table(name = "price_changes") | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@EqualsAndHashCode(callSuper = false) | ||
public class PriceChange extends BaseEntity { | ||
|
||
@ManyToOne(cascade = {CascadeType.ALL}) | ||
@JoinColumn(name = "programOrderableId") | ||
private ProgramOrderable programOrderable; | ||
|
||
@Type(type = "org.openlmis.referencedata.util.CustomSingleColumnMoneyUserType") | ||
private Money price; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "authorId", nullable = false) | ||
private User author; | ||
|
||
@Column(columnDefinition = "timestamp with time zone", nullable = false) | ||
private ZonedDateTime occurredDate; | ||
|
||
/** | ||
* Creates new instance based on data from {@link PriceChange.Importer}. | ||
* | ||
* @param importer instance of {@link PriceChange.Importer} | ||
* @return new instance of PriceChange. | ||
*/ | ||
public static PriceChange newInstance(PriceChange.Importer importer, User author) { | ||
PriceChange priceChange = new PriceChange(); | ||
priceChange.setPrice(importer.getPrice()); | ||
priceChange.setOccurredDate(importer.getOccurredDate()); | ||
priceChange.setAuthor(author); | ||
|
||
return priceChange; | ||
} | ||
|
||
/** | ||
* Export this object to the specified exporter (DTO). | ||
* | ||
* @param exporter exporter to export to | ||
*/ | ||
public void export(PriceChange.Exporter exporter) { | ||
exporter.setPrice(price); | ||
exporter.setAuthor(author); | ||
exporter.setOccurredDate(occurredDate); | ||
} | ||
|
||
public interface Exporter { | ||
|
||
void setPrice(Money price); | ||
|
||
void setAuthor(User author); | ||
|
||
void setOccurredDate(ZonedDateTime occurredDate); | ||
|
||
} | ||
|
||
public interface Importer { | ||
|
||
Money getPrice(); | ||
|
||
ZonedDateTime getOccurredDate(); | ||
|
||
} | ||
|
||
} |
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
87 changes: 87 additions & 0 deletions
87
src/main/java/org/openlmis/referencedata/dto/PriceChangeDto.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* This program is part of the OpenLMIS logistics management information system platform software. | ||
* Copyright © 2017 VillageReach | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Affero General Public License as published by the Free Software Foundation, either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Affero General Public License for more details. You should have received a copy of | ||
* the GNU Affero General Public License along with this program. If not, see | ||
* http://www.gnu.org/licenses. For additional information contact [email protected]. | ||
*/ | ||
|
||
package org.openlmis.referencedata.dto; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import java.time.ZonedDateTime; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import org.joda.money.Money; | ||
import org.openlmis.referencedata.domain.PriceChange; | ||
import org.openlmis.referencedata.domain.User; | ||
import org.openlmis.referencedata.serializer.MoneyDeserializer; | ||
import org.openlmis.referencedata.serializer.MoneySerializer; | ||
import org.openlmis.referencedata.web.BaseController; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@EqualsAndHashCode(callSuper = true) | ||
@ToString | ||
public final class PriceChangeDto extends BaseDto implements PriceChange.Exporter, | ||
PriceChange.Importer { | ||
|
||
@JsonSerialize(using = MoneySerializer.class) | ||
@JsonDeserialize(using = MoneyDeserializer.class) | ||
private Money price; | ||
private ZonedDateTime occurredDate; | ||
private UserObjectReferenceDto author; | ||
|
||
/** | ||
* Create new List containing PriceChangeDto based on given a set of {@link PriceChange}. | ||
* | ||
* @param priceChanges Price changes. | ||
* @return a list containing dtos for all price changes. | ||
*/ | ||
public static List<PriceChangeDto> newInstance(List<PriceChange> priceChanges) { | ||
if (priceChanges == null) { | ||
return Collections.emptyList(); | ||
} | ||
return priceChanges.stream() | ||
.map(PriceChangeDto::newInstance) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* Create new PriceChangeDto based on a given {@link PriceChange}. | ||
* | ||
* @param priceChange Price change. | ||
* @return a price change object converted to dto. | ||
*/ | ||
public static PriceChangeDto newInstance(PriceChange priceChange) { | ||
PriceChangeDto dto = new PriceChangeDto(); | ||
priceChange.export(dto); | ||
return dto; | ||
} | ||
|
||
@Override | ||
public void setAuthor(User author) { | ||
this.author = new UserObjectReferenceDto("referencedata", | ||
BaseController.API_PATH + "/users", author.getId()); | ||
this.author.setFirstName(author.getFirstName()); | ||
this.author.setLastName(author.getLastName()); | ||
} | ||
|
||
} |
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
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
9 changes: 9 additions & 0 deletions
9
src/main/resources/db/migration/20231111221730515__add_price_changes_table.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE TABLE price_changes ( | ||
id UUID PRIMARY KEY, | ||
programOrderableId UUID, | ||
price numeric(19,2) NOT NULL, | ||
authorId uuid NOT NULL, | ||
occurredDate TIMESTAMP WITH TIME ZONE NOT NULL, | ||
CONSTRAINT fk_program_orderable FOREIGN KEY (programOrderableId) REFERENCES program_orderables(id), | ||
CONSTRAINT fk_author FOREIGN KEY (authorId) REFERENCES users(id) | ||
); |
Oops, something went wrong.