Skip to content

Commit 10c521a

Browse files
author
Tanechka
committed
Lesson17 HW16_add_resume
1 parent dc678d6 commit 10c521a

File tree

5 files changed

+67
-16
lines changed

5 files changed

+67
-16
lines changed

src/ru/javawebinar/basejava/model/ListSection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class ListSection extends Section {
1212

1313
private static final long serialVersionUID = 1L;
1414

15+
public static final ListSection EMPTY = new ListSection("");
16+
1517
private List<String> items;
1618

1719
public ListSection() {

src/ru/javawebinar/basejava/model/Resume.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414
public class Resume implements Comparable<Resume>, Serializable {
1515
private static final long serialVersionUID = 1L;
1616

17+
public static final Resume EMPTY = new Resume();
18+
19+
static {
20+
EMPTY.setSection(SectionType.OBJECTIVE, TextSection.EMPTY);
21+
EMPTY.setSection(SectionType.PERSONAL, TextSection.EMPTY);
22+
EMPTY.setSection(SectionType.ACHIEVEMENT, ListSection.EMPTY);
23+
EMPTY.setSection(SectionType.QUALIFICATIONS, ListSection.EMPTY);
24+
EMPTY.setSection(SectionType.EXPERIENCE, new OrganizationSection(Organization.EMPTY));
25+
EMPTY.setSection(SectionType.EDUCATION, new OrganizationSection(Organization.EMPTY));
26+
}
27+
1728
// Unique identifier
1829
private String uuid;
1930

src/ru/javawebinar/basejava/model/TextSection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public class TextSection extends Section {
66

77
private static final long serialVersionUID = 1L;
88

9+
public static final TextSection EMPTY = new TextSection("");
10+
911
private String content;
1012

1113
public TextSection() {

src/ru/javawebinar/basejava/web/ResumeServlet.java

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
2929
request.setCharacterEncoding("UTF-8");
3030
String uuid = request.getParameter("uuid");
3131
String fullName = request.getParameter("fullName");
32-
Resume r = storage.get(uuid);
33-
r.setFullName(fullName);
32+
33+
final boolean isCreate = (uuid == null || uuid.length() == 0);
34+
Resume r;
35+
if (isCreate) {
36+
r = new Resume(fullName);
37+
} else {
38+
r = storage.get(uuid);
39+
r.setFullName(fullName);
40+
}
41+
3442
for (ContactType type : ContactType.values()) {
3543
String value = request.getParameter(type.name());
3644
if (HtmlUtil.isEmpty(value)) {
@@ -80,7 +88,11 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
8088
}
8189
}
8290
}
83-
storage.update(r);
91+
if (isCreate) {
92+
storage.save(r);
93+
} else {
94+
storage.update(r);
95+
}
8496
response.sendRedirect("resume");
8597
}
8698

@@ -101,21 +113,43 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
101113
case "view":
102114
r = storage.get(uuid);
103115
break;
116+
case "add":
117+
r = Resume.EMPTY;
118+
break;
104119
case "edit":
105120
r = storage.get(uuid);
106-
for (SectionType type : new SectionType[]{SectionType.EXPERIENCE, SectionType.EDUCATION}) {
107-
OrganizationSection section = (OrganizationSection) r.getSection(type);
108-
List<Organization> emptyFirstOrganizations = new ArrayList<>();
109-
emptyFirstOrganizations.add(Organization.EMPTY);
110-
if (section != null) {
111-
for (Organization org : section.getOrganizations()) {
112-
List<Organization.Position> emptyFirstPositions = new ArrayList<>();
113-
emptyFirstPositions.add(Organization.Position.EMPTY);
114-
emptyFirstPositions.addAll(org.getPositions());
115-
emptyFirstOrganizations.add(new Organization(org.getHomePage(), emptyFirstPositions));
116-
}
121+
for (SectionType type : SectionType.values()) {
122+
Section section = r.getSection(type);
123+
switch (type) {
124+
case OBJECTIVE:
125+
case PERSONAL:
126+
if (section == null) {
127+
section = TextSection.EMPTY;
128+
}
129+
break;
130+
case ACHIEVEMENT:
131+
case QUALIFICATIONS:
132+
if (section == null) {
133+
section = ListSection.EMPTY;
134+
}
135+
break;
136+
case EXPERIENCE:
137+
case EDUCATION:
138+
OrganizationSection orgSection = (OrganizationSection) section;
139+
List<Organization> emptyFirstOrganizations = new ArrayList<>();
140+
emptyFirstOrganizations.add(Organization.EMPTY);
141+
if (orgSection != null) {
142+
for (Organization org : orgSection.getOrganizations()) {
143+
List<Organization.Position> emptyFirstPositions = new ArrayList<>();
144+
emptyFirstPositions.add(Organization.Position.EMPTY);
145+
emptyFirstPositions.addAll(org.getPositions());
146+
emptyFirstOrganizations.add(new Organization(org.getHomePage(), emptyFirstPositions));
147+
}
148+
}
149+
section = new OrganizationSection(emptyFirstOrganizations);
150+
break;
117151
}
118-
r.setSection(type, new OrganizationSection(emptyFirstOrganizations));
152+
r.setSection(type, section);
119153
}
120154
break;
121155
default:

web/WEB-INF/jsp/list.jsp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
<body>
1212
<jsp:include page="fragments/header.jsp"/>
1313
<section>
14-
<table border="1" cellpadding="8" cellspacing="0">
14+
<a href="resume?action=add"><img src="img/add.png"></a>
15+
<br>
16+
<table border="1" cellpadding="8" cellspacing="0" style="margin: auto">
1517
<tr>
1618
<th>Имя</th>
1719
<th>Email</th>

0 commit comments

Comments
 (0)