I'm creating an electronic diary app, using java spring, but I faced with a small problem. I decided to create role system with a state pattern. So the logic of this system should look like this:
- In controller, after accepting request, it creates the object of context class.
- Constructor of context class gets concrete RoleClass in depends to user role.(it uses the factory)
- In controller, we call the method of context we want to be executed
- In context, we call the method of role we want to be executed.
- In role class we call the method of service we want to be executed.
But here's the problem. When I build the project, it shows me such error:
java: cannot find symbol
symbol: method getRole()
location: variable user of type com.diary.diary.entity.UserEntity
I can't understand why it's happening, because all the methods are declared in UserEntity class.
So here's all the code:
UserController method
@GetMapping("/marks")
public ResponseEntity<Object> getMarks() {
try {
UserContext userContext = new UserContext();
return ResponseEntity.ok(userContext.getMarks());
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
}
UserContext
public class UserContext {
private final UserRole userRole;
public UserContext() throws UserNotFoundException {
UserEntity user = new UserService().getCurrentUser();
userRole = RoleFactory.getUserRole(user.getRole().getName());
}
public List<HomeworkGetModel> getHomework() throws UserNotFoundException {
return userRole.getHomework();
}
public List<MarkGetModel> getMarks() throws UserNotFoundException {
return userRole.getMarks();
}
}
RoleFactory
public class RoleFactory {
private static UserRole userRole;
public static UserRole getUserRole(String roleName) {
if(userRole == null) {
userRole = buildUserRole(roleName);
}
return userRole;
}
private static UserRole buildUserRole(String roleString) {
switch (roleString) {
case RoleNames.DEFAULT -> userRole = new DefaultRole();
case RoleNames.STUDENT -> userRole = new StudentRole();
case RoleNames.TEACHER -> userRole = new TeacherRole();
case RoleNames.ADMIN -> userRole = new AdminRole();
}
return userRole;
}
}
UserEntity
@Entity
@Table(name = "users", schema = "working_schema")
@Getter @Setter
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String login;
private String password;
private String phoneNumber;
private String email;
private String name;
private String surname;
private String patronymic;
@ManyToOne
@JoinColumn(name = "roleid", nullable = false)
private RoleEntity role;
@ManyToOne
@JoinColumn(name = "classid")
private ClassEntity userClass;
@ManyToOne
@JoinColumn(name = "schoolid")
private SchoolEntity school;
@OneToMany(mappedBy = "student", cascade = CascadeType.ALL)
private List<MarkEntity> marks;
public UserEntity() {
}
}
RoleEntity
@Entity
@Table(name = "roles", schema = "working_schema")
@Getter @Setter
public class RoleEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String name;
@OneToMany(mappedBy = "role", cascade = CascadeType.ALL)
private List<UserEntity> users;
public RoleEntity() {
}
}
UserRole interface
public interface UserRole {
default UserGetModel getUser(long id) throws UserNotFoundException {
throw new NotImplementedException();
}
default List<UserGetModel> getAllUsers() {
throw new NotImplementedException();
}
default UserEntity updateUser(UserUpdateModel newUserData) throws UserNotFoundException {
throw new NotImplementedException();
}
default UserEntity deleteUser() throws UserNotFoundException {
throw new NotImplementedException();
}
default List<MarkGetModel> getMarks() throws UserNotFoundException {
throw new NotImplementedException();
}
default List<MarkGetModel> getMarksByDate(String date) throws UserNotFoundException, ParseException {
throw new NotImplementedException();
}
default List<MarkGetModel> getMarksBySubject(String subjectName) throws UserNotFoundException {
throw new NotImplementedException();
}
default List<MarkGetModel> getMarksByDateAndSubject(DateAndSubjectModel dateAndSubject) throws UserNotFoundException, ParseException {
throw new NotImplementedException();
}
default SubjectGetModel getSubject(long id) throws SubjectNotFoundException {
throw new NotImplementedException();
}
default SubjectGetModel getSubject(String name) throws SubjectNotFoundException {
throw new NotImplementedException();
}
default SchoolGetModel getSchoolById(long schoolId) throws SchoolNotFoundException {
throw new NotImplementedException();
}
default SchoolGetModel getSchoolByNumber(int schoolNumber) throws SchoolNotFoundException {
throw new NotImplementedException();
}
default List<SchoolGetModel> getSchools() {
throw new NotImplementedException();
}
default ClassGetModel getSchoolClass(ClassGetByNumberModel classData) throws com.diary.diary.exception.school_class.ClassNotFoundException, SchoolNotFoundException {
throw new NotImplementedException();
}
default List<ClassGetModel> getClasses() {
throw new NotImplementedException();
}
default List<HomeworkGetModel> getHomework() throws UserNotFoundException {
throw new NotImplementedException();
}
default List<HomeworkGetModel> getHomeworkByDate(String date) throws UserNotFoundException, ParseException {
throw new NotImplementedException();
}
default List<HomeworkGetModel> getHomeworkBySubject(String subjectName) {
throw new NotImplementedException();
}
default ClassEntity addClass(ClassAddModel classData) {
throw new NotImplementedException();
}
default ClassEntity addUserToClass(AdminAddUserToClassModel userAndClassModel) {
throw new NotImplementedException();
}
default ClassEntity deleteClass(long id) {
throw new NotImplementedException();
}
default UserEntity removeUserFromClass(AdminRemoveUserFromClassModel userClassModel) {
throw new NotImplementedException();
}
default SchoolEntity addSchool(SchoolAddModel schoolData) {
throw new NotImplementedException();
}
default SchoolEntity deleteSchool(long id) {
throw new NotImplementedException();
}
default UserEntity removeUserFromSchool(AdminRemoveUserFromSchoolModel userSchoolModel) {
throw new NotImplementedException();
}
default SubjectEntity addSubject(SubjectAddModel subjectData) {
throw new NotImplementedException();
}
default SubjectEntity updateSubject(SubjectUpdateModel newSubjectModel) {
throw new NotImplementedException();
}
default SubjectEntity deleteSubject(SubjectDeleteModel subjectDeleteData) {
throw new NotImplementedException();
}
default TimetableEntity addTimetable(TimetableAddModel timetableData) {
throw new NotImplementedException();
}
default TimetableEntity updateTimetable(TimeTableUpdateModel newTimetableData) {
throw new NotImplementedException();
}
default TimetableEntity deleteTimetable(long id) {
throw new NotImplementedException();
}
default ClassEntity addTimetableToClass(TimetableClassModel timetableClass) {
throw new NotImplementedException();
}
default ClassEntity deleteTimetableFromClass(long classId) {
throw new NotImplementedException();
}
}
CodePudding user response:
If you use maven you should add to pom.xml in plugins:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</path>
</configuration>
</plugin>
</plugins>
</build>
you used @Getter and @Setter, what means you already have a dependency
You need plugin to regenerate classes with lombok annotations to create getters, setters, contructors etc.
CodePudding user response:
Well, I don't know why it happened, but after reinstalling Lombok Plugin, I rebuilt my project and now everything is working.